import { AxiosError } from 'axios' ; import { ErrorResponse } from 'types/api' ; import { ErrorStatusCode } from 'types/common' ; export const ErrorResponseHandler = ( error ) : ErrorResponse => { if ( error . response ) { const statusCode = error . response . status as ErrorStatusCode ; if ( statusCode >= 400 && statusCode < 500 ) { const { data } = error . response ; if ( statusCode === 404 ) { return { statusCode , payload : null , error : 'Not Found' , message : null , } ; } return { statusCode , payload : null , error : data . error , message : null , } ; } return { statusCode , payload : null , error : 'Something went wrong' , message : null , } ; } if ( error . request ) { console . error ( 'client never received a response, or request never left' ) ; return { statusCode : 500 , payload : null , error : 'Something went wrong' , message : null , } ; } console . error ( 'any' ) ; return { statusCode : 500 , payload : null , error : error . toString ( ) , message : null , } ; } ;