Skip to content

Commit

Permalink
Rename stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
exacs committed Jul 10, 2024
1 parent 33f4423 commit d801132
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 29 deletions.
21 changes: 14 additions & 7 deletions src/canvasApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe("`get` can parse JSON response", () => {
"json": {
"hello": "world",
},
"text": null,
"text": "{ "hello" : "world" }",
}
`);
});
Expand All @@ -158,7 +158,7 @@ describe("`get` can parse JSON response", () => {

expect({ json, text }).toMatchInlineSnapshot(`
{
"json": null,
"json": undefined,
"text": "This is not a { json",
}
`);
Expand Down Expand Up @@ -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"}",
}
`);
});
Expand All @@ -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" }",
}
`);
});
Expand All @@ -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 )",
}
Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down
41 changes: 23 additions & 18 deletions src/canvasApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -16,12 +17,14 @@ export type CanvasApiResponse = {
headers: Record<string, string | string[] | undefined>;

/** 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 */
Expand All @@ -47,7 +50,7 @@ export function normalizeBody(obj: unknown) {
try {
return JSON.stringify(obj);
} catch (err) {
throw new CanvasApiRequestError();
throw new CanvasApiConnectionError();
}
}

Expand Down Expand Up @@ -132,35 +135,37 @@ 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) {
throw await CanvasApiResponseError.fromResponse(response);
}

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 */
Expand Down
16 changes: 13 additions & 3 deletions src/canvasApiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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";
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export { CanvasApi } from "./canvasApi";
export {
CanvasApiError,
CanvasApiResponseError,
CanvasApiRequestError,
CanvasApiConnectionError,
} from "./canvasApiError";

0 comments on commit d801132

Please sign in to comment.