Handle errors better

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-10-13 13:56:24 +02:00
parent 57f0b5dad1
commit 33e51a05ec
13 changed files with 214 additions and 78 deletions

View File

@@ -11,6 +11,8 @@ import { isServerError } from '@/types/apollo';
import { REFRESH_TOKEN } from '@/graphql/auth';
import { AUTH_ACCESS_TOKEN, AUTH_REFRESH_TOKEN } from '@/constants';
import { logout, saveTokenData } from '@/utils/auth';
import { SnackbarProgrammatic as Snackbar } from 'buefy';
import { defaultError, errors, IError, refreshSuggestion } from '@/utils/errors';
// Install the vue plugin
Vue.use(VueApollo);
@@ -87,14 +89,29 @@ const errorLink = onError(({ graphQLErrors, networkError, forward, operation })
}
if (graphQLErrors) {
graphQLErrors.forEach(({ message, locations, path }) =>
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`),
);
graphQLErrors.forEach(({ message, locations, path }) => {
Snackbar.open({ message: computeErrorMessage(message), type: 'is-danger', position: 'is-bottom' });
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
});
}
if (networkError) console.log(`[Network error]: ${networkError}`);
if (networkError) {
console.log(`[Network error]: ${networkError}`);
Snackbar.open({ message: computeErrorMessage(networkError), type: 'is-danger', position: 'is-bottom' });
}
});
const computeErrorMessage = (message) => {
const error: IError = errors.reduce((acc, error) => {
if (RegExp(error.match).test(message)) {
return error;
}
return acc;
}, defaultError);
return error.suggestRefresh === false ? error.value : `${error.value}<br>${refreshSuggestion}`;
};
const link = authMiddleware
.concat(errorLink)
.concat(uploadLink);