1 export class ApiError extends Error {
10 constructor(message: string, status: number, name: string) {
12 Object.setPrototypeOf(this, ApiError.prototype);
18 export const createApiError = (name: string, response: Response, config: any, data?: any) => {
19 const { statusText, status } = response;
21 const error = new ApiError(statusText, status, name);
23 error.response = response;
25 error.config = config;
30 export const serializeApiErrorData = (error: ApiError) => {
32 * We are only interested in the data here, so we strip almost everything else. In particular:
33 * * error.response is typically not serializable
34 * * error.config might not be serializable either (for instance it can include (aborted) abort controllers)
40 statusText: error.response?.statusText || error.message,
45 export const deserializeApiErrorData = ({
50 }: ReturnType<typeof serializeApiErrorData>) => {
51 const error = new ApiError(statusText, status, name);
58 export enum CUSTOM_FETCH_ERROR_STATUS_CODE {
59 NO_NETWORK_CONNECTION = 0,
63 export const createOfflineError = (config: any) => {
64 const error = new ApiError(
65 'No network connection',
66 CUSTOM_FETCH_ERROR_STATUS_CODE.NO_NETWORK_CONNECTION,
69 error.config = config;
73 export const createTimeoutError = (config: any) => {
74 const error = new ApiError('Request timed out', CUSTOM_FETCH_ERROR_STATUS_CODE.TIMEOUT, 'TimeoutError');
75 error.config = config;