-
-
Notifications
You must be signed in to change notification settings - Fork 522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global mutation error handler (interceptor)? #670
Comments
solved. const errorHandler = onError(({ networkError, graphQLErrors }) => {
console.log({ graphQLErrors, networkError})
if (networkError && networkError.statusCode === 401) {
window.location = '/login'
}
})
const client = new ApolloClient({
link: from([
// order matters
acceptJsonMiddleware,
errorHandler,
httpLink,
]),
cache,
}); |
Thanks, @zaidbarghouthi! Was struggling with this. Now, I can see the errors logged in the console, from the global handler, but how do I access these errors from within a component? I am using Any ideas? |
It seems it works fine for But, I can see the errors logged in console, for queries and mutations. |
Sorry for bothering in this closed issue, but I wonder if there is or will be a global error handler for mutations? The error link solution is sure a way to do that, however since there is already a |
@zaidbarghouthi Where did you get this function onError? I tried to implment and get onError is not defined |
import { onError } from 'apollo-link-error' |
@zaidbarghouthi I was trying to use your solution but have
Update: I found solution on page https://www.apollographql.com/docs/react/data/error-handling/ Thanks for your hint. |
I use
|
where do you add this code and from where did you import |
import Vue from 'vue'
import VueApollo from 'vue-apollo'
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { ApolloLink, from } from 'apollo-link'
import { onError } from 'apollo-link-error'
import { InMemoryCache } from 'apollo-cache-inmemory'
Vue.use(VueApollo)
const acceptJsonMiddleware = new ApolloLink((operation, forward) => {
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
accept: 'application/json',
},
}))
return forward(operation)
})
const errorHandler = onError(({ networkError }) => {
if (networkError) {
if (networkError.statusCode === 401) window.location = '/auth/login'
if (networkError.statusCode === 403 && networkError.result.message === 'ACCOUNT_SUSPENDED' ) window.location = '/suspended'
}
})
const cache = new InMemoryCache()
const httpLink = new createHttpLink({
uri: '/gql/client',
credentials: 'include',
})
const apolloClient = new ApolloClient({
link: from([
acceptJsonMiddleware,
errorHandler,
httpLink,
]),
cache,
})
export default new VueApollo({
defaultClient: apolloClient,
defaultOptions: {
$query: {
fetchPolicy: 'cache-and-network',
},
},
}) This is my apollo.js which exports VueApollo that is used in the Vue instance. Please note that this code is not in production as the project was abandoned more that a year ago. Somethings might have changed. |
@zaidbarghouthi thanks |
Hey, what about interceptor in request and not a error handling? Before request been executed? Like axios.interceptors.request.use function Like use a header's parameter dinamically in request interceptor |
this error handler doesn't intercept mutation errors
am I doing something wrong? what is the right way? is there such a thing?
The text was updated successfully, but these errors were encountered: