Add error codes to our custom errors (#959)

This commit is contained in:
Fábio Rosado
2022-11-25 17:04:10 +00:00
committed by GitHub
parent 30e31a86ef
commit b062efcf17
20 changed files with 306 additions and 67 deletions

View File

@@ -0,0 +1,87 @@
import { describe, it, expect, jest } from '@jest/globals'
import { FetchError, ErrorCode } from "../../src/exceptions"
import { robustFetch } from "../../src/fetch"
import { Response } from 'node-fetch';
describe("robustFetch", () => {
it("should return a response object", async () => {
global.fetch = jest.fn(() => (Promise.resolve(new Response(status="200", "Hello World"))));
const response = await robustFetch("https://pyscript.net");
expect(response).toBeInstanceOf(Response);
expect(response.status).toBe(200);
})
it('receiving a 404 when fetching should throw FetchError with the right errorCode', async () => {
global.fetch = jest.fn(() => (Promise.resolve(new Response("Not Found", {status: 404}))));
const url = "https://pyscript.net/non-existent-page"
const expectedError = new FetchError(
ErrorCode.FETCH_NOT_FOUND_ERROR,
`Fetching from URL ${url} failed with error 404 (Not Found).`
)
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
})
it('receiving a 404 when fetching should throw FetchError with the right errorCode', async () => {
global.fetch = jest.fn(() => (Promise.resolve(new Response("Not Found", {status: 404}))));
const url = "https://pyscript.net/non-existent-page"
const expectedError = new FetchError(
ErrorCode.FETCH_NOT_FOUND_ERROR,
`Fetching from URL ${url} failed with error 404 (Not Found).`
)
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
})
it('receiving a 401 when fetching should throw FetchError with the right errorCode', async () => {
global.fetch = jest.fn(() => (Promise.resolve(new Response("", {status: 401}))));
const url = "https://pyscript.net/protected-page"
const expectedError = new FetchError(
ErrorCode.FETCH_UNAUTHORIZED_ERROR,
`Fetching from URL ${url} failed with error 401 (Unauthorized).`
)
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
})
it('receiving a 403 when fetching should throw FetchError with the right errorCode', async () => {
global.fetch = jest.fn(() => (Promise.resolve(new Response("", {status: 403}))));
const url = "https://pyscript.net/secret-page"
const expectedError = new FetchError(
ErrorCode.FETCH_FORBIDDEN_ERROR,
`Fetching from URL ${url} failed with error 403 (Forbidden).`
)
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
})
it('receiving a 500 when fetching should throw FetchError with the right errorCode', async () => {
global.fetch = jest.fn(() => (Promise.resolve(new Response("Not Found", {status: 500}))));
const url = "https://pyscript.net/protected-page"
const expectedError = new FetchError(
ErrorCode.FETCH_SERVER_ERROR,
`Fetching from URL ${url} failed with error 500 (Internal Server Error).`
)
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
})
it('receiving a 503 when fetching should throw FetchError with the right errorCode', async () => {
global.fetch = jest.fn(() => (Promise.resolve(new Response("Not Found", {status: 503}))));
const url = "https://pyscript.net/protected-page"
const expectedError = new FetchError(
ErrorCode.FETCH_UNAVAILABLE_ERROR,
`Fetching from URL ${url} failed with error 503 (Service Unavailable).`
)
expect(() => robustFetch(url)).rejects.toThrow(expectedError);
})
});