From d80113201a8c70cc72ef7620c4b3ba63920fa1ca Mon Sep 17 00:00:00 2001 From: Carlos Saito Date: Wed, 10 Jul 2024 13:06:37 +0200 Subject: [PATCH] Rename stuff --- src/canvasApi.test.ts | 21 ++++++++++++++------- src/canvasApi.ts | 41 +++++++++++++++++++++++------------------ src/canvasApiError.ts | 16 +++++++++++++--- src/index.ts | 2 +- 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/src/canvasApi.test.ts b/src/canvasApi.test.ts index d0a9999..1cb94fc 100644 --- a/src/canvasApi.test.ts +++ b/src/canvasApi.test.ts @@ -147,7 +147,7 @@ describe("`get` can parse JSON response", () => { "json": { "hello": "world", }, - "text": null, + "text": "{ "hello" : "world" }", } `); }); @@ -158,7 +158,7 @@ describe("`get` can parse JSON response", () => { expect({ json, text }).toMatchInlineSnapshot(` { - "json": null, + "json": undefined, "text": "This is not a { json", } `); @@ -193,12 +193,15 @@ describe("CanvasApiResponseError", () => { expect(error?.name).toEqual("CanvasApiResponseError"); expect(error?.response).toMatchInlineSnapshot(` { + "body": { + "message": "Missing parameters", + }, "headers": {}, "json": { "message": "Missing parameters", }, "statusCode": 400, - "text": null, + "text": "{"message": "Missing parameters"}", } `); }); @@ -211,12 +214,15 @@ describe("CanvasApiResponseError", () => { expect(error?.name).toEqual("CanvasApiResponseError"); expect(error?.response).toMatchInlineSnapshot(` { + "body": { + "message": "Method not allowed", + }, "headers": {}, "json": { "message": "Method not allowed", }, "statusCode": 405, - "text": null, + "text": "{ "message": "Method not allowed" }", } `); }); @@ -229,8 +235,9 @@ describe("CanvasApiResponseError", () => { expect(error?.name).toEqual("CanvasApiResponseError"); expect(error?.response).toMatchInlineSnapshot(` { + "body": undefined, "headers": {}, - "json": null, + "json": undefined, "statusCode": 418, "text": "I am a teapot and invalid JSON )", } @@ -267,7 +274,7 @@ describe("method-level timeout", () => { .catch((e) => e); const t2 = Date.now(); - expect(error?.name).toEqual("CanvasApiRequestError"); + expect(error?.name).toEqual("CanvasApiTimeoutError"); expect(error?.stack).toMatch(/canvasApi\.test\.ts/g); expect(t2 - t1).toBeLessThan(120); }); @@ -284,7 +291,7 @@ describe("method-level timeout", () => { .catch((e) => e); const t2 = Date.now(); - expect(error?.name).toEqual("CanvasApiRequestError"); + expect(error?.name).toEqual("CanvasApiTimeoutError"); expect(error?.stack).toMatch(/canvasApi\.test\.ts/g); expect(t2 - t1).toBeLessThan(120); }); diff --git a/src/canvasApi.ts b/src/canvasApi.ts index e3b7db3..7d02d33 100644 --- a/src/canvasApi.ts +++ b/src/canvasApi.ts @@ -3,8 +3,9 @@ import { request, FormData } from "undici"; import type { Dispatcher } from "undici"; import { CanvasApiPaginationError, - CanvasApiRequestError, + CanvasApiConnectionError, CanvasApiResponseError, + CanvasApiTimeoutError, } from "./canvasApiError"; import { ExtendedGenerator } from "./extendedGenerator"; @@ -16,12 +17,14 @@ export type CanvasApiResponse = { headers: Record; /** Parsed body. `undefined` if the response cannot be parsed` */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any json: any; /** * Alias for `json`. * @deprecated. Use `json` or `text` instead */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any body: any; /** Body without parsing */ @@ -47,7 +50,7 @@ export function normalizeBody(obj: unknown) { try { return JSON.stringify(obj); } catch (err) { - throw new CanvasApiRequestError(); + throw new CanvasApiConnectionError(); } } @@ -132,8 +135,12 @@ export class CanvasApi { signal: mergedOptions.timeout ? AbortSignal.timeout(mergedOptions.timeout) : null, - }).catch(() => { - throw new CanvasApiRequestError(); + }).catch((err) => { + if (err instanceof DOMException && err.name === "TimeoutError") { + throw new CanvasApiTimeoutError(); + } + + throw new CanvasApiConnectionError(); }); if (response.statusCode >= 300) { @@ -141,26 +148,24 @@ export class CanvasApi { } const text = await response.body.text(); + const result = { + statusCode: response.statusCode, + headers: response.headers, + body: undefined, + json: undefined, + text, + }; try { const json = JSON.parse(text); - return { - statusCode: response.statusCode, - headers: response.headers, - body: json, - json, - text, - }; + result.json = json; + result.body = json; } catch (e) { - return { - statusCode: response.statusCode, - headers: response.headers, - body: undefined, - json: undefined, - text, - }; + // Do not do anything } + + return result; } /** Performs a GET request to a given endpoint */ diff --git a/src/canvasApiError.ts b/src/canvasApiError.ts index b10d501..67b9139 100644 --- a/src/canvasApiError.ts +++ b/src/canvasApiError.ts @@ -53,7 +53,9 @@ export class CanvasApiResponseError extends CanvasApiError { const json = await JSON.parse(text); error.response.json = json; error.response.body = json; - } catch (err) {} + } catch (err) { + // Don't do anything + } return error; } @@ -62,11 +64,19 @@ export class CanvasApiResponseError extends CanvasApiError { /** * Thrown when there was some error before reaching Canvas */ -export class CanvasApiRequestError extends CanvasApiError { +export class CanvasApiConnectionError extends CanvasApiError { constructor() { // TODO super("Canvas API request error"); - this.name = "CanvasApiRequestError"; + this.name = "CanvasApiConnectionError"; + } +} + +/** Thrown when a request times out before getting any response */ +export class CanvasApiTimeoutError extends CanvasApiError { + constructor() { + super("Canvas API timeout error"); + this.name = "CanvasApiTimeoutError"; } } diff --git a/src/index.ts b/src/index.ts index 93db77e..a72a210 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,5 +2,5 @@ export { CanvasApi } from "./canvasApi"; export { CanvasApiError, CanvasApiResponseError, - CanvasApiRequestError, + CanvasApiConnectionError, } from "./canvasApiError";