apollo: Catch query error
I am working with the node-advanced
boilerplate (https://github.com/graphql-boilerplates/node-graphql-server/tree/master/advanced), currently I want to catch the error thrown server-side when I check if the user is logged in. Therefore I have the following query:
apollo: {
feed: gql`{feed {title text id createdAt updatedAt isPublished}}`,
loggedIn: {
query: gql`{me{id}}`,
update(data) {
// We are authenticated (logged in) when we get some data
return true
},
error(error) {
// Catch the error when we are not authenticated
this.authenticateResult = error.message
}
}
}
The error(error){...}
function is called, which is good. So I am able to know that there was an error. Right now the error(error){...}
function is called after the error appears in the console. However, I want to catch this error so that it is not thrown in the console. Is there a way to do this?
With mutations I can succesfully catch the error:
this.$apollo.mutate({
mutation: LOGIN_MUTATION,
variables: {
email: this.email,
password: this.password
}
}).then(data => {
// localStorage.setItem('GC_AUTH_TOKEN',data.login.token)
// this.token = localStorage.getItem('GC_AUTH_TOKEN')
this.authenticateResult = data
this.authenticated = true
}).catch(error => {
this.authenticated = false
this.authenticateResult = error.message
})
Is there a way to really catch the errors when performing a query, not a mutation?
Thanks in advance!
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 16 (4 by maintainers)
Hello, maybe I’m missing something but how can i fix this? I’ve this code
I’m still getting back the printed out message instead of the error object that contains the data that i need to analyze… any help please?
I think this issue should be reopened. On queries, even if we have an error handler, the console still gets the error printed. I don’t believe this is an apollo-client problem.
Looking at the vue-apollo code in
smart-apollo.js
, it appears to be manually outputting the error withconsole.error
: https://github.com/Akryum/vue-apollo/blob/66504309599ce9d4a31feb8fd5f065d790349c86/src/smart-apollo.js#L138-L156The library is correctly catching the error and passing it to the callback, but it’s also pushing the error to the console.
I think we should have an option (or the default) to suppress errors if
this.options.error
set and handled. If I’m handling the error, I don’t want it also pushed to the client’s console.FYI for anyone else that runs into this, your error handler has to return a truthy value if you want to stop the error propagation.
See line 163 and 165 here:
https://github.com/Akryum/vue-apollo/blob/d88e6ace17dba94f84b3111d41894da5e19a240e/src/smart-apollo.js#L160-L169
Example:
Hey @Akryum , you say that “it’s already released” but as @itapingu I’m still seeing printed out messages. What has been released?
If anyone reads this issue and is using
graphql-ruby
withvue-apollo
, you shouldn’traise
errors through Ruby because as mentioned above you need truthy value / not a crash. You should rather useGraphQL::ExecutionError.new('My error')
which can be easily serialized afterwards:Hope it helps 😃
Errors array located in response error js object. You may find it in “Error.response”.
i have the last version with the “fix” but error still shows as “Error: GraphQL error: Unable to to be identified …” not a proper json