diff --git a/src/core/database/IDatabase.ts b/src/core/databases/IDatabase.ts similarity index 100% rename from src/core/database/IDatabase.ts rename to src/core/databases/IDatabase.ts diff --git a/src/core/database/PgDatabase.ts b/src/core/databases/PgDatabase.ts similarity index 100% rename from src/core/database/PgDatabase.ts rename to src/core/databases/PgDatabase.ts diff --git a/src/core/errors/APIError.ts b/src/core/errors/APIError.ts new file mode 100644 index 0000000..e808f3f --- /dev/null +++ b/src/core/errors/APIError.ts @@ -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, + }; + } +} diff --git a/src/core/tests/database/PgDatabase.test.ts b/src/core/tests/databases/PgDatabase.test.ts similarity index 96% rename from src/core/tests/database/PgDatabase.test.ts rename to src/core/tests/databases/PgDatabase.test.ts index 98ceec0..dd87ed3 100644 --- a/src/core/tests/database/PgDatabase.test.ts +++ b/src/core/tests/databases/PgDatabase.test.ts @@ -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; diff --git a/src/core/tests/errors/APIError.test.ts b/src/core/tests/errors/APIError.test.ts new file mode 100644 index 0000000..6a30b99 --- /dev/null +++ b/src/core/tests/errors/APIError.test.ts @@ -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, + }); +});