add APIError class
All checks were successful
All checks were successful
This commit is contained in:
parent
73be118d1c
commit
da035bf5c4
5 changed files with 51 additions and 1 deletions
21
src/core/errors/APIError.ts
Normal file
21
src/core/errors/APIError.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
export interface APIErrorStruct {
|
||||||
|
msg: string;
|
||||||
|
statusCode: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class APIError extends Error {
|
||||||
|
public statusCode: number;
|
||||||
|
|
||||||
|
constructor(msg?: string, statusCode?: number, options?: ErrorOptions) {
|
||||||
|
super(msg, options);
|
||||||
|
|
||||||
|
this.statusCode = statusCode || 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
toStruct(): APIErrorStruct {
|
||||||
|
return {
|
||||||
|
msg: this.message,
|
||||||
|
statusCode: this.statusCode,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
import { afterEach, beforeEach, expect, test } from "bun:test";
|
import { afterEach, beforeEach, expect, test } from "bun:test";
|
||||||
import PgDatabase from "../../database/PgDatabase";
|
import PgDatabase from "../../databases/PgDatabase";
|
||||||
|
|
||||||
let db: PgDatabase;
|
let db: PgDatabase;
|
||||||
const host = Bun.env.DB_HOST;
|
const host = Bun.env.DB_HOST;
|
29
src/core/tests/errors/APIError.test.ts
Normal file
29
src/core/tests/errors/APIError.test.ts
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
import { expect, test } from "bun:test";
|
||||||
|
import APIError from "../../errors/APIError";
|
||||||
|
|
||||||
|
test("test APIError extends Error", () => {
|
||||||
|
const apiError = new APIError("Unknow error", 500);
|
||||||
|
|
||||||
|
expect(apiError instanceof Error).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("test APIError status code", () => {
|
||||||
|
const apiError = new APIError("Bad request", 400);
|
||||||
|
|
||||||
|
expect(apiError.statusCode).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("test APIError default status code", () => {
|
||||||
|
const apiError = new APIError(":-(");
|
||||||
|
|
||||||
|
expect(apiError.statusCode).toBe(500);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("test APIError toStruct", () => {
|
||||||
|
const apiError = new APIError("Bad request", 400);
|
||||||
|
|
||||||
|
expect(apiError.toStruct()).toEqual({
|
||||||
|
msg: "Bad request",
|
||||||
|
statusCode: 400,
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue