1 import { useCallback } from 'react';
3 import { c } from 'ttag';
5 import { getApiErrorMessage } from '@proton/shared/lib/api/helpers/apiErrorHelper';
6 import { traceError } from '@proton/shared/lib/helpers/sentry';
8 import useNotifications from './useNotifications';
10 const ignoreErrors = ['InactiveSession', 'AppVersionBadError', 'AbortError'];
12 const useErrorHandler = () => {
13 const { createNotification } = useNotifications();
15 return useCallback((error: any, { notify = true, trace = true }: { notify?: boolean; trace?: boolean } = {}) => {
20 const apiErrorMessage = getApiErrorMessage(error);
21 const errorMessage = error.message || c('Error').t`Unknown error`;
23 // Bad app version and unreachable errors are handled in a top banner
24 const shouldNotify = notify && !error.cancel && !ignoreErrors.includes(error.name);
26 createNotification({ type: 'error', text: apiErrorMessage || errorMessage });
29 const shouldTrace = trace && error.trace !== false && !apiErrorMessage;
36 export default useErrorHandler;