Skip to content

Commit 418dce2

Browse files
committed
🎉 Initial commit with error handling logic
0 parents  commit 418dce2

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/node_modules
2+
/dist
3+
*.log
4+
package-lock.json

README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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.

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "axios-error-handler",
3+
"version": "1.0.0",
4+
"description": "A simple helper to handle errors in Axios requests with customizable messages.",
5+
"main": "src/errorHandler.ts",
6+
"types": "src/errorHandler.ts",
7+
"scripts": {
8+
"build": "tsc"
9+
},
10+
"keywords": [
11+
"axios",
12+
"error",
13+
"handler",
14+
"helper",
15+
"typescript"
16+
],
17+
"author": "Jonas Szigeti",
18+
"license": "MIT",
19+
"dependencies": {
20+
"axios": "^1.7.9"
21+
},
22+
"devDependencies": {
23+
"typescript": "^5.7.2"
24+
}
25+
}

src/errorHandler.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { AxiosError } from "axios";
2+
3+
export const handleError = (
4+
error: unknown,
5+
errorCodesToCheck: number | number[],
6+
errorMessages: string | Record<number, string>
7+
): string => {
8+
if (typeof errorMessages === "string") {
9+
return errorMessages;
10+
}
11+
const codesToCheck = Array.isArray(errorCodesToCheck)
12+
? errorCodesToCheck
13+
: [errorCodesToCheck];
14+
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.";
24+
};

tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2016",
4+
"lib": ["es2016", "dom"],
5+
"module": "commonjs",
6+
"moduleResolution": "node",
7+
"strict": true,
8+
"esModuleInterop": true,
9+
"skipLibCheck": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"outDir": "./dist",
12+
"rootDir": "./src",
13+
"declaration": true,
14+
"sourceMap": true
15+
},
16+
"include": ["src/**/*.ts"],
17+
"exclude": ["node_modules"]
18+
}

0 commit comments

Comments
 (0)