Skip to content

Commit 5c11ed5

Browse files
committed
🔧 Added configurable options to the Axios error handler function
1 parent 8b47848 commit 5c11ed5

File tree

2 files changed

+80
-41
lines changed

2 files changed

+80
-41
lines changed

README.md

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,99 +4,122 @@ A simple and flexible error handler for Axios requests, allowing customizable er
44

55
## Installation
66

7-
To install `axios-error-handler`, you can use npm or yarn:
7+
To install `axios-error-handler-ts`, you can use npm or yarn:
88

99
### Using npm:
1010

1111
```bash
12-
npm install axios-error-handler
12+
npm install axios-error-handler-ts
1313
```
1414

1515
### Using yarn:
1616

1717
```bash
18-
yarn add axios-error-handler
18+
yarn add axios-error-handler-ts
1919
```
2020

2121
## Usage
2222

23-
You can use `axios-error-handler` to handle errors in your Axios requests by customizing error messages based on HTTP status codes.
23+
You can use `axios-error-handler-ts` to handle errors in your Axios requests by customizing error messages based on HTTP status codes.
2424

2525
### Basic Usage (Single Status Code)
2626

2727
```typescript
28-
import { handleError } from "axios-error-handler";
28+
import { handleError } from "axios-error-handler-ts";
2929

3030
// Example function
3131
handleError(error, 403, "Email or password incorrect.");
32-
// if AxiosError and status === 403: "Email or password incorrect."
32+
// If AxiosError and status === 403: 'Email or password incorrect.'
3333
```
3434

3535
### Advanced Usage (Multiple Status Codes)
3636

3737
You can also provide custom error messages for multiple HTTP status codes:
3838

3939
```typescript
40-
import { handleError } from "axios-error-handler";
40+
import { handleError } from "axios-error-handler-ts";
4141

4242
// Example function
43-
handleError(error, [403, 404], {
43+
handleError(error, {
4444
403: "You are not allowed to access this resource.",
4545
404: "Group is not found.",
4646
});
47-
// if AxiosError and status === 403: "You are not allowed to access this resource."
48-
// if AxiosError and status === 404: "Group is not found."
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+
### Global Usage (Only Message Provided)
52+
53+
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.
54+
55+
```typescript
56+
import { handleError } from "axios-error-handler-ts";
57+
58+
// Example function
59+
handleError(error, "You are not allowed to access this resource.");
60+
// If AxiosError and status === 403: 'You are not allowed to access this resource.'
61+
// If AxiosError and status === 500: 'You are not allowed to access this resource.'
4962
```
5063

5164
### Server Error Handling
5265

5366
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.
5467

5568
```typescript
56-
import { handleError } from "axios-error-handler";
69+
import { handleError } from "axios-error-handler-ts";
5770

5871
// Example function
5972
handleError(error, 403, "Email or password incorrect.");
60-
// If AxiosError and status === 500: "A server error occurred. Please try again later."
73+
// If AxiosError and status === 500: 'A server error occurred. Please try again later.'
6174
```
6275

6376
### Default Error Handling
6477

6578
If the error status doesn't match any of the provided codes, the handler will return a default error message.
6679

6780
```typescript
68-
import { handleError } from "axios-error-handler";
81+
import { handleError } from "axios-error-handler-ts";
6982

7083
// Example function
7184
handleError(error, 403, "Email or password incorrect.");
72-
// If AxiosError and status !== 403 | status > 500: "An unknown error occurred."
73-
// If Error : error.message
85+
// If AxiosError and status !== 403 | status > 500: 'An unknown error occurred.'
86+
// If generic Error : error.message
7487
// If unknown error : 'An unknown error occurred.'
7588
```
7689

7790
### Options
7891

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-
8492
#### `errorMessages` (required)
8593

8694
- **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.
95+
- **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.
96+
97+
If passing a `Record<number, string>`, must not fill `errorCodesToCheck`, the object keys represent status codes and values represent corresponding error messages.
98+
99+
#### `errorCodeToCheck` (optional)
100+
101+
- **Type**: `number`
102+
- **Description**: The specific status code to check for in the Axios error response. Must be specified if `errorMessages` is a string.
103+
104+
#### `serverErrorMessage` (optional)
105+
106+
- **Type**: `string`
107+
- **Description**: The message to show for server errors (status codes >= 500). Default is 'A server error occurred. Please try again later.'
108+
109+
#### `unknownErrorMessage` (optional)
88110

89-
If passing a `Record<number, string>`, the object keys represent status codes and values represent corresponding error messages.
111+
- **Type**: `string`
112+
- **Description**: The message to show when the error type is unknown. Default is 'An unknown error occurred.'
90113

91114
## Example
92115

93116
```typescript
94-
import { handleError } from "axios-error-handler";
117+
import { handleError } from "axios-error-handler-ts";
95118

96119
try {
97120
// Axios request here
98121
} catch (error: unknown) {
99-
const errorMessage = handleError(error, [403, 404], {
122+
const errorMessage = handleError(error, {
100123
403: "You are not authorized to access this resource.",
101124
404: "Requested resource not found.",
102125
});

src/errorHandler.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
import { AxiosError } from "axios";
22

3+
/**
4+
* Handles errors by checking the error type and status code.
5+
* @param error - The error object, which could be an AxiosError or a generic Error.
6+
* @param errorMessages - A string or an object mapping status codes to specific error messages. Must be an object if errorCodeToCheck is not provided.
7+
* @param errorCodeToCheck - Optionally specify a single error code to check against. Must be specified if errorMessages is a string.
8+
* @param serverErrorMessage - The message to show for server errors (status codes >= 500) (optional).
9+
* @param unknownErrorMessage - The message to show when the error type is unknown (optional).
10+
*/
11+
312
export const handleError = (
413
error: unknown,
5-
errorCodesToCheck: number | number[],
6-
errorMessages: string | Record<number, string>
14+
errorMessages: string | Record<number, string>,
15+
errorCodeToCheck?: number,
16+
serverErrorMessage: string = "A server error occurred. Please try again later.",
17+
unknownErrorMessage: string = "An unknown error occurred."
718
): string => {
8-
if (typeof errorMessages === "string") {
9-
return errorMessages;
10-
}
11-
const codesToCheck = Array.isArray(errorCodesToCheck)
12-
? errorCodesToCheck
13-
: [errorCodesToCheck];
1419
if (error instanceof AxiosError && error.response) {
15-
const status = error.response.status;
16-
if (codesToCheck.includes(status) && errorMessages[status])
17-
return errorMessages[status];
18-
if (status >= 500)
19-
return "A server error occurred. Please try again later.";
20-
return "An unknown error occurred.";
21-
}
22-
if (error instanceof Error) return error.message;
23-
return "An unknown error occurred.";
20+
if (
21+
typeof errorMessages === "string" &&
22+
!errorCodeToCheck &&
23+
error.response.status < 500
24+
)
25+
return errorMessages;
26+
const codesToCheck: number[] = errorCodeToCheck
27+
? [errorCodeToCheck]
28+
: Object.keys(errorMessages).map(Number);
29+
if (codesToCheck.includes(error.response.status)) {
30+
if (
31+
typeof errorMessages === "object" &&
32+
errorMessages[error.response.status]
33+
)
34+
return errorMessages[error.response.status];
35+
else if (typeof errorMessages === "string") return errorMessages;
36+
} else if (error.response.status >= 500) return serverErrorMessage;
37+
return unknownErrorMessage;
38+
} else if (error instanceof Error) return error.message;
39+
return unknownErrorMessage;
2440
};

0 commit comments

Comments
 (0)