From 8f40ae29ed05afe14b8222e4f7a569dba84961ad Mon Sep 17 00:00:00 2001 From: Mordechai Dror Date: Sat, 16 Nov 2024 21:47:01 +0200 Subject: [PATCH] unify dal tables into db, add method overloads to error-or --- src/bl/chains/chains.bl.ts | 2 +- src/bl/users/users.bl.ts | 2 +- src/dal/{chains => db}/chains.table.ts | 0 src/dal/{users => db}/users.table.ts | 0 src/shared/lib/error-or.spec.ts | 90 ------------------- src/shared/lib/error-or/catch-error.spec.ts | 82 +++++++++++++++++ .../{error-or.ts => error-or/catch-error.ts} | 50 +++++------ src/shared/lib/error-or/error-or.ts | 5 ++ .../lib/error-or/re-throw-error.spec.ts | 3 + src/shared/lib/error-or/re-throw-error.ts | 40 +++++++++ 10 files changed, 154 insertions(+), 120 deletions(-) rename src/dal/{chains => db}/chains.table.ts (100%) rename src/dal/{users => db}/users.table.ts (100%) delete mode 100644 src/shared/lib/error-or.spec.ts create mode 100644 src/shared/lib/error-or/catch-error.spec.ts rename src/shared/lib/{error-or.ts => error-or/catch-error.ts} (50%) create mode 100644 src/shared/lib/error-or/error-or.ts create mode 100644 src/shared/lib/error-or/re-throw-error.spec.ts create mode 100644 src/shared/lib/error-or/re-throw-error.ts diff --git a/src/bl/chains/chains.bl.ts b/src/bl/chains/chains.bl.ts index 4d04312..b37b352 100644 --- a/src/bl/chains/chains.bl.ts +++ b/src/bl/chains/chains.bl.ts @@ -1,4 +1,4 @@ -import { findAllChains } from "../../dal/chains/chains.table.ts"; +import { findAllChains } from "../../dal/db/chains.table.ts"; import type { Chain } from "../../shared/schema/chains.ts"; export async function findChains(): Promise> { diff --git a/src/bl/users/users.bl.ts b/src/bl/users/users.bl.ts index 5350595..286c2e2 100644 --- a/src/bl/users/users.bl.ts +++ b/src/bl/users/users.bl.ts @@ -1,7 +1,7 @@ import { createUser, findUserByTelegramChatId, -} from "../../dal/users/users.table.ts"; +} from "../../dal/db/users.table.ts"; import type { User } from "../../shared/schema/users.ts"; export async function findOrCreateUserByTelegramChatId( diff --git a/src/dal/chains/chains.table.ts b/src/dal/db/chains.table.ts similarity index 100% rename from src/dal/chains/chains.table.ts rename to src/dal/db/chains.table.ts diff --git a/src/dal/users/users.table.ts b/src/dal/db/users.table.ts similarity index 100% rename from src/dal/users/users.table.ts rename to src/dal/db/users.table.ts diff --git a/src/shared/lib/error-or.spec.ts b/src/shared/lib/error-or.spec.ts deleted file mode 100644 index 86d6046..0000000 --- a/src/shared/lib/error-or.spec.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; -import { catchError, catchErrorAsync } from "./error-or.ts"; - -describe("catchError", () => { - it("should return value if no error was thrown", () => { - const result = "123"; - const callback = () => result; - - expect(catchError(callback)).toEqual([undefined, result]); - }); - - it("should throw if unknown error was thrown", () => { - const callback = () => { - throw new Error("test"); - }; - - expect(() => catchError(callback, [CustomError])).toThrowError("test"); - }); - - it("should return error if known error was thrown", () => { - const error = new CustomError(); - const callback = () => { - throw error; - }; - - const consoleSpy = vi.spyOn(console, "error"); - - expect(catchError(callback, [CustomError])).toEqual([error]); - expect(consoleSpy, "should log error to console").toBeCalledWith(error); - }); - - it("should return error if no known errors were passed", () => { - const error = new CustomError(); - const callback = () => { - throw error; - }; - - const consoleSpy = vi.spyOn(console, "error"); - - expect(catchError(callback)).toEqual([error]); - expect(consoleSpy, "should log error to console").toBeCalledWith(error); - }); -}); - -describe.todo("reThrowError"); - -describe("catchErrorAsync", () => { - it("should return value if no error was thrown", async () => { - const result = "123"; - const promise = Promise.resolve(result); - - expect(await catchErrorAsync(promise)).toEqual([undefined, result]); - }); - - it("should throw if unknown error was thrown", async () => { - const promise = Promise.reject("test"); - - await expect( - async () => await catchErrorAsync(promise, [CustomError]), - ).rejects.toThrowError("test"); - }); - - it("should return error if known error was thrown", async () => { - const error = new CustomError(); - const promise = Promise.reject(error); - - const consoleSpy = vi.spyOn(console, "error"); - - expect(await catchErrorAsync(promise, [CustomError])).toEqual([error]); - expect(consoleSpy, "should log error to console").toBeCalledWith(error); - }); - - it("should return error if no known errors were passed", async () => { - const error = new CustomError(); - const promise = Promise.reject(error); - - const consoleSpy = vi.spyOn(console, "error"); - - expect(await catchErrorAsync(promise)).toEqual([error]); - expect(consoleSpy, "should log error to console").toBeCalledWith(error); - }); -}); - -describe.todo("reThrowErrorAsync"); - -class CustomError extends Error { - constructor() { - super("test"); - } -} diff --git a/src/shared/lib/error-or/catch-error.spec.ts b/src/shared/lib/error-or/catch-error.spec.ts new file mode 100644 index 0000000..117b3b7 --- /dev/null +++ b/src/shared/lib/error-or/catch-error.spec.ts @@ -0,0 +1,82 @@ +import { expect, it, vi } from "vitest"; +import { catchError } from "./catch-error.ts"; + +it("should return value if no error was thrown", () => { + const result = "123"; + const callback = () => result; + + expect(catchError(callback)).toEqual([undefined, result]); +}); + +it("should throw if unknown error was thrown", () => { + const callback = () => { + throw new Error("test"); + }; + + expect(() => catchError(callback, [CustomError])).toThrowError("test"); +}); + +it("should return error if known error was thrown", () => { + const error = new CustomError(); + const callback = () => { + throw error; + }; + + const consoleSpy = vi.spyOn(console, "error"); + + expect(catchError(callback, [CustomError])).toEqual([error]); + expect(consoleSpy, "should log error to console").toBeCalledWith(error); +}); + +it("should return error if no known errors were passed", () => { + const error = new CustomError(); + const callback = () => { + throw error; + }; + + const consoleSpy = vi.spyOn(console, "error"); + + expect(catchError(callback)).toEqual([error]); + expect(consoleSpy, "should log error to console").toBeCalledWith(error); +}); + +it("should resolve value if no error was thrown", async () => { + const result = "123"; + const promise = Promise.resolve(result); + + expect(await catchError(promise)).toEqual([undefined, result]); +}); + +it("should reject if unknown error was thrown", async () => { + const promise = Promise.reject("test"); + + await expect( + async () => await catchError(promise, [CustomError]), + ).rejects.toThrowError("test"); +}); + +it("should resolve error if known error was thrown", async () => { + const error = new CustomError(); + const promise = Promise.reject(error); + + const consoleSpy = vi.spyOn(console, "error"); + + expect(await catchError(promise, [CustomError])).toEqual([error]); + expect(consoleSpy, "should log error to console").toBeCalledWith(error); +}); + +it("should resolve error if no known errors were passed", async () => { + const error = new CustomError(); + const promise = Promise.reject(error); + + const consoleSpy = vi.spyOn(console, "error"); + + expect(await catchError(promise)).toEqual([error]); + expect(consoleSpy, "should log error to console").toBeCalledWith(error); +}); + +class CustomError extends Error { + constructor() { + super("test"); + } +} diff --git a/src/shared/lib/error-or.ts b/src/shared/lib/error-or/catch-error.ts similarity index 50% rename from src/shared/lib/error-or.ts rename to src/shared/lib/error-or/catch-error.ts index 1e11659..0713459 100644 --- a/src/shared/lib/error-or.ts +++ b/src/shared/lib/error-or/catch-error.ts @@ -1,10 +1,26 @@ -import type { Constructor } from "./constructor.ts"; - -export type ErrorOr> = - | [undefined, T] - | [InstanceType]; +import type { Constructor } from "../constructor.ts"; +import type { ErrorOr } from "./error-or.ts"; export function catchError>( + promise: Promise, + errorsTypesToCatch?: Array, +): Promise>; +export function catchError>( + callback: () => T, + errorsTypesToCatch?: Array, +): ErrorOr; +export function catchError>( + promiseOrCallback: Promise | (() => T), + errorsTypesToCatch?: Array, +): Promise> | ErrorOr { + if (promiseOrCallback instanceof Promise) { + return catchErrorAsync(promiseOrCallback, errorsTypesToCatch); + } + + return catchErrorSync(promiseOrCallback, errorsTypesToCatch); +} + +function catchErrorSync>( callback: () => T, errorsTypesToCatch?: Array, ): ErrorOr { @@ -25,18 +41,7 @@ export function catchError>( } } -export function reThrowError>( - result: ErrorOr, -): T { - const [error, data] = result; - if (error) { - throw error; - } - - return data as T; -} - -export async function catchErrorAsync>( +async function catchErrorAsync>( promise: Promise, errorsTypesToCatch?: Array, ): Promise> { @@ -56,14 +61,3 @@ export async function catchErrorAsync>( throw e; } } - -export async function reThrowErrorAsync>( - result: Promise>, -): Promise { - const [error, data] = await result; - if (error) { - throw error; - } - - return data as T; -} diff --git a/src/shared/lib/error-or/error-or.ts b/src/shared/lib/error-or/error-or.ts new file mode 100644 index 0000000..73fad1f --- /dev/null +++ b/src/shared/lib/error-or/error-or.ts @@ -0,0 +1,5 @@ +import type { Constructor } from "../constructor.ts"; + +export type ErrorOr> = + | [undefined, T] + | [InstanceType]; diff --git a/src/shared/lib/error-or/re-throw-error.spec.ts b/src/shared/lib/error-or/re-throw-error.spec.ts new file mode 100644 index 0000000..cb52ac0 --- /dev/null +++ b/src/shared/lib/error-or/re-throw-error.spec.ts @@ -0,0 +1,3 @@ +import { describe } from "vitest"; + +describe.todo("reThrowError"); diff --git a/src/shared/lib/error-or/re-throw-error.ts b/src/shared/lib/error-or/re-throw-error.ts new file mode 100644 index 0000000..3294c9b --- /dev/null +++ b/src/shared/lib/error-or/re-throw-error.ts @@ -0,0 +1,40 @@ +import type { Constructor } from "../constructor.ts"; +import type { ErrorOr } from "./error-or.ts"; + +export async function reThrowError>( + result: Promise>, +): Promise; +export function reThrowError>( + result: ErrorOr, +): T; +export function reThrowError>( + result: Promise> | ErrorOr, +): Promise | T { + if (result instanceof Promise) { + return reThrowErrorAsync(result); + } + + return reThrowErrorSync(result); +} + +function reThrowErrorSync>( + result: ErrorOr, +): T { + const [error, data] = result; + if (error) { + throw error; + } + + return data as T; +} + +async function reThrowErrorAsync>( + result: Promise>, +): Promise { + const [error, data] = await result; + if (error) { + throw error; + } + + return data as T; +}