1 import { c } from 'ttag';
3 import { API_CODES } from '../../constants';
4 import { HTTP_ERROR_CODES } from '../../errors';
6 export const isNotExistError = (error: any) => {
7 const notExistErrorCodes = [
8 API_CODES.INVALID_ID_ERROR,
9 API_CODES.NOT_FOUND_ERROR,
11 * Mail Specific: Conversation does not exist
16 return Boolean(notExistErrorCodes.includes(error.data?.Code));
19 export const getApiError = (e?: any) => {
23 const { data, status } = e;
31 const { Error: errorMessage, Code: errorCode, Details: errorDetails } = data;
42 message: errorMessage,
43 details: errorDetails,
47 export const getIs401Error = (e: any) => {
51 return e.name === 'InactiveSession' || e.status === 401;
54 export const getIsOfflineError = (e: any) => {
58 return e.name === 'OfflineError';
61 export const getIsUnreachableError = (e: any) => {
65 // On 503, even if there's response, it's an unreachable error
66 if (e.status === HTTP_ERROR_CODES.SERVICE_UNAVAILABLE) {
69 // On 502 or 504, we verify that the API did not actually give a response. It can return these codes when acting as a proxy (e.g. email-list unsubscribe).
71 [HTTP_ERROR_CODES.BAD_GATEWAY, HTTP_ERROR_CODES.GATEWAY_TIMEOUT].includes(e.status) &&
72 e.data?.Code === undefined
76 export const getIsNetworkError = (e: any) => {
80 return e.name === 'NetworkError' || e.message?.toLowerCase() === 'network error';
83 export const getIsTimeoutError = (e: any) => {
87 return e.name === 'TimeoutError';
90 export const getIsConnectionIssue = (e: any) => {
91 return getIsOfflineError(e) || getIsUnreachableError(e) || getIsNetworkError(e) || getIsTimeoutError(e);
94 export const getApiErrorMessage = (e: any): string | undefined => {
95 const { message } = getApiError(e);
96 if (getIs401Error(e)) {
97 return message || c('Info').t`Session timed out`;
99 if (getIsOfflineError(e)) {
100 return message || c('Info').t`Internet connection lost`;
102 if (getIsUnreachableError(e)) {
103 return message || c('Info').t`Servers are unreachable. Please try again in a few minutes`;
105 if (getIsTimeoutError(e)) {
106 return message || c('Error').t`Request timed out`;