A simple and flexible error handler for Axios requests, allowing customizable error messages based on HTTP status codes. It helps manage API error responses by providing clear and customized messages depending on the status code returned by the server.
To install axios-error-handler-ts, you can use npm or yarn:
npm install axios-error-handler-tsyarn add axios-error-handler-tsYou can use axios-error-handler-ts to handle errors in your Axios requests by customizing error messages based on HTTP status codes.
import { handleError } from 'axios-error-handler-ts';
// Example function
handleError(error, 403, 'Email or password incorrect.');
// If AxiosError and status === 403: 'Email or password incorrect.'You can also provide custom error messages for multiple HTTP status codes:
import { handleError } from 'axios-error-handler-ts';
// Example function
handleError(error, {
403: 'You are not allowed to access this resource.',
404: 'Group is not found.',
});
// If AxiosError and status === 403: 'You are not allowed to access this resource.'
// If AxiosError and status === 404: 'Group is not found.'You can only provide a string and if the error is AxiosInstance and not related to a server one, the handler will return the global message.
import { handleError } from 'axios-error-handler-ts';
// Example function
handleError(error, 'You are not allowed to access this resource.');
// If AxiosError and status === 401: 'You are not allowed to access this resource.'
// If AxiosError and status === 403: 'You are not allowed to access this resource.'
// If AxiosError and status === 500: 'A server error occurred. Please try again later.'If the error status doesn't match any of the provided codes and is related to a server error (status 500+), the handler will return a default server error message.
import { handleError } from 'axios-error-handler-ts';
// Example function
handleError(error, 403, 'Email or password incorrect.');
// If AxiosError and status === 500: 'A server error occurred. Please try again later.'If the error status doesn't match any of the provided codes, the handler will return a default error message.
import { handleError } from 'axios-error-handler-ts';
// Example function
handleError(error, 403, 'Email or password incorrect.');
// If AxiosError and status !== 403 | status > 500: 'An unknown error occurred.'
// If generic Error : error.message
// If unknown error : 'An unknown error occurred.'- Type:
string | Record<number, string> - Description: The custom error message(s) for the specified status code(s). If you pass a single string, it will be used for all errors. If passing a
Record<number, string>, the object keys represent status codes, and values represent corresponding error messages. If passing aRecord<number, string>, must not fillerrorCodesToCheck, the object keys represent status codes and values represent corresponding error messages.
- Type:
number - Description: The specific status code to check for in the Axios error response. Must be specified if
errorMessagesis a string.
- Type:
string - Description: The message to show for server errors (status codes >= 500). Default is 'A server error occurred. Please try again later.'
- Type:
string - Description: The message to show when the error type is unknown. Default is 'An unknown error occurred.'
import { handleError } from 'axios-error-handler-ts';
try {
// Axios request here
} catch (error: unknown) {
const errorMessage = handleError(error, {
403: 'You are not authorized to access this resource.',
404: 'Requested resource not found.',
});
console.log(errorMessage);
// Output the error message :
// If AxiosError and status === 403: 'You are not authorized to access this resource.'
// If AxiosError and status === 404: 'Requested resource not found.'
// If Axios Error and status >= 500: 'A server error occurred. Please try again later.'
// If Axios Error and status < 500 | status !== 403 | status !== 404: 'An unknown error occurred.'
// If Error : error.message
// If unknown error : 'An unknown error occurred.'
}See CONTRIBUTING.md for development guidelines.
This project is licensed under the MIT License.