-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
259 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Test changes | ||
|
||
on: | ||
pull_request: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: 'latest' | ||
registry-url: 'https://registry.npmjs.org/' | ||
|
||
- name: Install dependencies | ||
run: yarn install | ||
|
||
- name: Test project | ||
run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,10 @@ | |
"version": "1.0.3", | ||
"description": "An useful error handler for axios network requests in a React application", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/vboechat/network-error-handling" | ||
}, | ||
"author": { | ||
"name": "Victor Ribeiro Boechat", | ||
"email": "[email protected]", | ||
|
@@ -15,12 +19,12 @@ | |
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsc" | ||
}, | ||
"dependencies": { | ||
"axios": "^1.6.8" | ||
"build": "tsc", | ||
"test": "vitest" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.4.5" | ||
"axios": "^1.6.8", | ||
"typescript": "^5.4.5", | ||
"vitest": "^1.6.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { describe, it, expect, vi } from "vitest"; | ||
import { AxiosError } from "axios"; | ||
import { networkErrorHandling, ToastFunction } from "./network-error-handling"; | ||
import { StatusCode } from "./status-codes"; | ||
|
||
describe("networkErrorHandling", () => { | ||
it("should add an error and handle it without toast", () => { | ||
const axiosError = { | ||
response: { status: 404 }, | ||
} as AxiosError; | ||
|
||
const handler = networkErrorHandling(axiosError).addError( | ||
StatusCode.NOT_FOUND, | ||
"Not Found", | ||
"The requested resource was not found" | ||
); | ||
|
||
const callback = vi.fn(); | ||
handler.addError( | ||
StatusCode.INTERNAL_SERVER_ERROR, | ||
"Server Error", | ||
"Internal server error", | ||
callback | ||
); | ||
|
||
handler.handle(); | ||
|
||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should add an error and handle it with toast", () => { | ||
const axiosError = { | ||
response: { status: 404 }, | ||
} as AxiosError; | ||
|
||
const toast: ToastFunction = vi.fn(); | ||
|
||
const handler = networkErrorHandling(axiosError) | ||
.addError( | ||
StatusCode.NOT_FOUND, | ||
"Not Found", | ||
"The requested resource was not found" | ||
) | ||
.withToast(toast); | ||
|
||
handler.handle(); | ||
|
||
expect(toast).toHaveBeenCalledWith( | ||
"Not Found", | ||
"The requested resource was not found" | ||
); | ||
}); | ||
|
||
it("should add multiple errors and handle them correctly", () => { | ||
const axiosError = { | ||
response: { status: 500 }, | ||
} as AxiosError; | ||
|
||
const toast: ToastFunction = vi.fn(); | ||
const callback = vi.fn(); | ||
|
||
const handler = networkErrorHandling(axiosError) | ||
.addError( | ||
StatusCode.NOT_FOUND, | ||
"Not Found", | ||
"The requested resource was not found" | ||
) | ||
.addError( | ||
StatusCode.INTERNAL_SERVER_ERROR, | ||
"Server Error", | ||
"Internal server error", | ||
callback | ||
) | ||
.withToast(toast); | ||
|
||
handler.handle(); | ||
|
||
expect(toast).toHaveBeenCalledWith("Server Error", "Internal server error"); | ||
expect(callback).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should not handle error if status is not in the map", () => { | ||
const axiosError = { | ||
response: { status: 403 }, | ||
} as AxiosError; | ||
|
||
const toast: ToastFunction = vi.fn(); | ||
const callback = vi.fn(); | ||
|
||
const handler = networkErrorHandling(axiosError) | ||
.addError( | ||
StatusCode.NOT_FOUND, | ||
"Not Found", | ||
"The requested resource was not found" | ||
) | ||
.addError( | ||
StatusCode.INTERNAL_SERVER_ERROR, | ||
"Server Error", | ||
"Internal server error", | ||
callback | ||
) | ||
.withToast(toast); | ||
|
||
handler.handle(); | ||
|
||
expect(toast).not.toHaveBeenCalled(); | ||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should handle error without status", () => { | ||
const axiosError = { | ||
response: {}, | ||
} as AxiosError; | ||
|
||
const handler = networkErrorHandling(axiosError).addError( | ||
StatusCode.NOT_FOUND, | ||
"Not Found", | ||
"The requested resource was not found" | ||
); | ||
|
||
handler.handle(); | ||
|
||
expect(true).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export const StatusCode = { | ||
OK: 200, | ||
CREATED: 201, | ||
ACCEPTED: 202, | ||
NO_CONTENT: 204, | ||
MOVED_PERMANENTLY: 301, | ||
FOUND: 302, | ||
SEE_OTHER: 303, | ||
NOT_MODIFIED: 304, | ||
TEMPORARY_REDIRECT: 307, | ||
PERMANENT_REDIRECT: 308, | ||
BAD_REQUEST: 400, | ||
UNAUTHORIZED: 401, | ||
FORBIDDEN: 403, | ||
NOT_FOUND: 404, | ||
METHOD_NOT_ALLOWED: 405, | ||
NOT_ACCEPTABLE: 406, | ||
CONFLICT: 409, | ||
GONE: 410, | ||
LENGTH_REQUIRED: 411, | ||
PRECONDITION_FAILED: 412, | ||
PAYLOAD_TOO_LARGE: 413, | ||
URI_TOO_LONG: 414, | ||
UNSUPPORTED_MEDIA_TYPE: 415, | ||
RANGE_NOT_SATISFIABLE: 416, | ||
EXPECTATION_FAILED: 417, | ||
UPGRADE_REQUIRED: 426, | ||
INTERNAL_SERVER_ERROR: 500, | ||
NOT_IMPLEMENTED: 501, | ||
BAD_GATEWAY: 502, | ||
SERVICE_UNAVAILABLE: 503, | ||
GATEWAY_TIMEOUT: 504, | ||
HTTP_VERSION_NOT_SUPPORTED: 505, | ||
} as const; |
This file was deleted.
Oops, something went wrong.