|
| 1 | +# axios-error-handler |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install `axios-error-handler`, you can use npm or yarn: |
| 8 | + |
| 9 | +### Using npm: |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install axios-error-handler |
| 13 | +``` |
| 14 | + |
| 15 | +### Using yarn: |
| 16 | + |
| 17 | +```bash |
| 18 | +yarn add axios-error-handler |
| 19 | +``` |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +You can use `axios-error-handler` to handle errors in your Axios requests by customizing error messages based on HTTP status codes. |
| 24 | + |
| 25 | +### Basic Usage (Single Status Code) |
| 26 | + |
| 27 | +```typescript |
| 28 | +import { handleError } from "axios-error-handler"; |
| 29 | + |
| 30 | +// Example function |
| 31 | +handleError(error, 403, "Email or password incorrect."); |
| 32 | +// if AxiosError and status === 403: "Email or password incorrect." |
| 33 | +``` |
| 34 | + |
| 35 | +### Advanced Usage (Multiple Status Codes) |
| 36 | + |
| 37 | +You can also provide custom error messages for multiple HTTP status codes: |
| 38 | + |
| 39 | +```typescript |
| 40 | +import { handleError } from "axios-error-handler"; |
| 41 | + |
| 42 | +// Example function |
| 43 | +handleError(error, [403, 404], { |
| 44 | + 403: "You are not allowed to access this resource.", |
| 45 | + 404: "Group is not found.", |
| 46 | +}); |
| 47 | +// if AxiosError and status === 403: "You are not allowed to access this resource." |
| 48 | +// if AxiosError and status === 404: "Group is not found." |
| 49 | +``` |
| 50 | + |
| 51 | +### Server Error Handling |
| 52 | + |
| 53 | +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. |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { handleError } from "axios-error-handler"; |
| 57 | + |
| 58 | +// Example function |
| 59 | +handleError(error, 403, "Email or password incorrect."); |
| 60 | +// If AxiosError and status === 500: "A server error occurred. Please try again later." |
| 61 | +``` |
| 62 | + |
| 63 | +### Default Error Handling |
| 64 | + |
| 65 | +If the error status doesn't match any of the provided codes, the handler will return a default error message. |
| 66 | + |
| 67 | +```typescript |
| 68 | +import { handleError } from "axios-error-handler"; |
| 69 | + |
| 70 | +// Example function |
| 71 | +handleError(error, 403, "Email or password incorrect."); |
| 72 | +// If AxiosError and status !== 403 | status > 500: "An unknown error occurred." |
| 73 | +// If Error : error.message |
| 74 | +// If unknown error : 'An unknown error occurred.' |
| 75 | +``` |
| 76 | + |
| 77 | +### Options |
| 78 | + |
| 79 | +#### `errorCodesToCheck` (required) |
| 80 | + |
| 81 | +- **Type**: `number | number[]` |
| 82 | +- **Description**: The status code(s) to check for in the Axios error response. You can pass a single status code or an array of status codes. |
| 83 | + |
| 84 | +#### `errorMessages` (required) |
| 85 | + |
| 86 | +- **Type**: `string | Record<number, string>` |
| 87 | +- **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. |
| 88 | + |
| 89 | +If passing a `Record<number, string>`, the object keys represent status codes and values represent corresponding error messages. |
| 90 | + |
| 91 | +## Example |
| 92 | + |
| 93 | +```typescript |
| 94 | +import { handleError } from "axios-error-handler"; |
| 95 | + |
| 96 | +try { |
| 97 | + // Axios request here |
| 98 | +} catch (error: unknown) { |
| 99 | + const errorMessage = handleError(error, [403, 404], { |
| 100 | + 403: "You are not authorized to access this resource.", |
| 101 | + 404: "Requested resource not found.", |
| 102 | + }); |
| 103 | + |
| 104 | + console.log(errorMessage); |
| 105 | + // Output the error message : |
| 106 | + // If AxiosError and status === 403: 'You are not authorized to access this resource.' |
| 107 | + // If AxiosError and status === 404: 'Requested resource not found.' |
| 108 | + // If Axios Error and status >= 500: 'A server error occurred. Please try again later.' |
| 109 | + // If Axios Error and status < 500 | status !== 403 | status !== 404: 'An unknown error occurred.' |
| 110 | + // If Error : error.message |
| 111 | + // If unknown error : 'An unknown error occurred.' |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Development |
| 116 | + |
| 117 | +To contribute to the project, clone the repository and install dependencies: |
| 118 | + |
| 119 | +```bash |
| 120 | +git clone https://github.com/Jszigeti/axios-error-handler.git |
| 121 | +cd axios-error-handler |
| 122 | +npm install |
| 123 | +``` |
| 124 | + |
| 125 | +### Building |
| 126 | + |
| 127 | +To compile the TypeScript code into JavaScript, run: |
| 128 | + |
| 129 | +```bash |
| 130 | +npm run build |
| 131 | +``` |
| 132 | + |
| 133 | +This will generate the compiled code in the `dist` directory. |
| 134 | + |
| 135 | +## License |
| 136 | + |
| 137 | +This project is licensed under the MIT License. |
0 commit comments