Open applyinnovations opened 2 years ago
Hey @applyinnovations, DataStore only invokes the custom error handler when it encounters an AppSync mutation error. That's the current expected behavior. The custom error handler's purpose is to allow you to write business logic for handling various AppSync error scenarios and adjust the locally-persisted records accordingly.
For example:
For catching client-side errors - such as field validation - we recommend using try...catch
blocks when calling DataStore.save
and DataStore.delete
.
Hey @iartemiev, thanks for the clarification. When performing a mutation with missing fields, DataStore still creates the data locally, however does not throw the custom error handler. Wouldnt this be a prime example of when to use the error handler to remove the locally created data?
Are you seeing a network request error in that case?
@iartemiev There is no network error, however the network response contains a graphql error.
Request Body:
query: "mutation operation($input: CreateCategoryInput!) {\n createCategory(input: $input) {\n id\n name\n color\n createdAt\n updatedAt\n _version\n _lastChangedAt\n _deleted\n }\n}\n"
variables: {input: {id: "6d55ca74-92dc-4a44-9248-9d3e9d43597e"}}
input: {id: "6d55ca74-92dc-4a44-9248-9d3e9d43597e"}
id: "6d55ca74-92dc-4a44-9248-9d3e9d43597e"
Response (Status code 200) Body:
{
"data":null,
"errors":[
{
"path":null,
"locations":[
{
"line":1,
"column":20,
"sourceName":null
}
],
"message":"Variable 'input' has coerced Null value for NonNull type 'String!'"
}
]
}
Debug logs:
[Log] [DEBUG] 01:17.731 RestClient - POST – "https://sn4fnspmfbdm5dwl5okhzqigce.appsync-api.ap-southeast-2.amazonaws.com/graphql"
[Warning] [WARN] 01:17.879 DataStore - failed to execute errorHandler – Error: Field name is required
Error: Field name is required
[Log] [DEBUG] 01:17.880 DataStore - Mutation sent successfully with authMode: AMAZON_COGNITO_USER_POOLS
As you can see in the screenshot, the local state contains the data, however the graphql server rejected the change but because our custom errorHandler provided in DataStore.configure is not used, we cannot fix the local state.
Similar to #7860
On a similar note, how are we expected to handle authorisation errors? They do not throw the default handler or custom handler.
{
"data":{
"deletePlaceBenefit":null
},
"errors":[
{
"path":[
"deletePlaceBenefit"
],
"data":null,
"errorType":"Unauthorized",
"errorInfo":null,
"locations":[
{
"line":2,
"column":3,
"sourceName":null
}
],
"message":"Not Authorized to access deletePlaceBenefit on type PlaceBenefit"
}
]
}
However DataStore commits the change locally and the user is unaware the mutation failed.
@iartemiev what is the recommended method of fixing the local DataStore state in both of these scenarios? The response is thrown by the server not the client, wouldn't this be an ideal case to use the handler specified in DataStore.configure?
I was able to reproduce the issue. Although this is currently expected behavior, I'm going to label this as a feature request that needs discussion because I do see the inconvenience in having to validate the result of the DataStore.save
operation in order to check whether or not the GraphQL mutation was successful.
Thank you @chrisbonifacio for following up on this. I believe using the error handler to repair local state is an essential feature, however it is useless unless it covers all mutation events!
We also have this issue. I can reproduce it for authorization errors (similar to that https://github.com/aws-amplify/amplify-js/issues/8891#issuecomment-922582761). From what I understood there isn't any workaround at the moment, right?
Also, is there a default error handler in the Datastore? If I add the code below, am I overriding a default handler or just adding one? What I mean is, if I add that code it shouldn't cause any issue right?
DataStore.configure({
errorHandler: (error) => {
console.log(error);
},
});
@chrisbonifacio could you please give an update on this? We cannot release our new version to production until this issue is resolved
Before opening, please confirm:
JavaScript Framework
React, React Native
Amplify APIs
DataStore
Amplify Categories
api
Environment information
Describe the bug
DataStore uses default errorHandler for some errors, and uses my custom error handler for other errors.
I have implemented a custom error handler by calling
DataStore.configure
in my app entry point (App.tsx).When submitting a DataStore.save() with a model containing invalid data: Our AppSync API returns an error to the DataStore client and our custom error handler is used.
When submitting a DataStore.save() with an empty model: DataStore encounters the error
Error: Field name is required
and uses the default error handler. Resulting in the following being logged to the console:[WARN] 38:37.438 DataStore - failed to execute errorHandler – Error: Field name is required
Expected behavior
The expected behaviour is to catch/route all errors to my custom errorHandler
Reproduction steps
type Model { number: Int }
DataStore.configure({ errorHandler: (error) => alert(
Custom handler ${error.message}) });
DataStore.save(new Model({ number: 1 }))
-> no errorDataStore.save(new Model({ number: "not a number" }))
-> custom error handlerDataStore.save(new Model({}))
-> default error handlerCode Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response