add APIError class
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test/1 Pipeline was successful
ci/woodpecker/push/test/2 Pipeline was successful
ci/woodpecker/push/test/3 Pipeline was successful

This commit is contained in:
qpismont 2024-12-30 20:14:37 +00:00
parent 73be118d1c
commit da035bf5c4
5 changed files with 51 additions and 1 deletions

View 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,
};
}
}

View file

@ -1,5 +1,5 @@
import { afterEach, beforeEach, expect, test } from "bun:test";
import PgDatabase from "../../database/PgDatabase";
import PgDatabase from "../../databases/PgDatabase";
let db: PgDatabase;
const host = Bun.env.DB_HOST;

View 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,
});
});