Skip to content

Commit

Permalink
unify dal tables into db, add method overloads to error-or
Browse files Browse the repository at this point in the history
  • Loading branch information
vorant94 committed Nov 16, 2024
1 parent a0d3211 commit 8f40ae2
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/bl/chains/chains.bl.ts
Original file line number Diff line number Diff line change
@@ -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<Array<Chain>> {
Expand Down
2 changes: 1 addition & 1 deletion src/bl/users/users.bl.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
File renamed without changes.
File renamed without changes.
90 changes: 0 additions & 90 deletions src/shared/lib/error-or.spec.ts

This file was deleted.

82 changes: 82 additions & 0 deletions src/shared/lib/error-or/catch-error.spec.ts
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import type { Constructor } from "./constructor.ts";

export type ErrorOr<T, E extends Constructor<Error>> =
| [undefined, T]
| [InstanceType<E>];
import type { Constructor } from "../constructor.ts";
import type { ErrorOr } from "./error-or.ts";

export function catchError<T, E extends Constructor<Error>>(
promise: Promise<T>,
errorsTypesToCatch?: Array<E>,
): Promise<ErrorOr<T, E>>;
export function catchError<T, E extends Constructor<Error>>(
callback: () => T,
errorsTypesToCatch?: Array<E>,
): ErrorOr<T, E>;
export function catchError<T, E extends Constructor<Error>>(
promiseOrCallback: Promise<T> | (() => T),
errorsTypesToCatch?: Array<E>,
): Promise<ErrorOr<T, E>> | ErrorOr<T, E> {
if (promiseOrCallback instanceof Promise) {
return catchErrorAsync(promiseOrCallback, errorsTypesToCatch);
}

return catchErrorSync(promiseOrCallback, errorsTypesToCatch);
}

function catchErrorSync<T, E extends Constructor<Error>>(
callback: () => T,
errorsTypesToCatch?: Array<E>,
): ErrorOr<T, E> {
Expand All @@ -25,18 +41,7 @@ export function catchError<T, E extends Constructor<Error>>(
}
}

export function reThrowError<T, E extends Constructor<Error>>(
result: ErrorOr<T, E>,
): T {
const [error, data] = result;
if (error) {
throw error;
}

return data as T;
}

export async function catchErrorAsync<T, E extends Constructor<Error>>(
async function catchErrorAsync<T, E extends Constructor<Error>>(
promise: Promise<T>,
errorsTypesToCatch?: Array<E>,
): Promise<ErrorOr<T, E>> {
Expand All @@ -56,14 +61,3 @@ export async function catchErrorAsync<T, E extends Constructor<Error>>(
throw e;
}
}

export async function reThrowErrorAsync<T, E extends Constructor<Error>>(
result: Promise<ErrorOr<T, E>>,
): Promise<T> {
const [error, data] = await result;
if (error) {
throw error;
}

return data as T;
}
5 changes: 5 additions & 0 deletions src/shared/lib/error-or/error-or.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Constructor } from "../constructor.ts";

export type ErrorOr<T, E extends Constructor<Error>> =
| [undefined, T]
| [InstanceType<E>];
3 changes: 3 additions & 0 deletions src/shared/lib/error-or/re-throw-error.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { describe } from "vitest";

describe.todo("reThrowError");
40 changes: 40 additions & 0 deletions src/shared/lib/error-or/re-throw-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Constructor } from "../constructor.ts";
import type { ErrorOr } from "./error-or.ts";

export async function reThrowError<T, E extends Constructor<Error>>(
result: Promise<ErrorOr<T, E>>,
): Promise<T>;
export function reThrowError<T, E extends Constructor<Error>>(
result: ErrorOr<T, E>,
): T;
export function reThrowError<T, E extends Constructor<Error>>(
result: Promise<ErrorOr<T, E>> | ErrorOr<T, E>,
): Promise<T> | T {
if (result instanceof Promise) {
return reThrowErrorAsync(result);
}

return reThrowErrorSync(result);
}

function reThrowErrorSync<T, E extends Constructor<Error>>(
result: ErrorOr<T, E>,
): T {
const [error, data] = result;
if (error) {
throw error;
}

return data as T;
}

async function reThrowErrorAsync<T, E extends Constructor<Error>>(
result: Promise<ErrorOr<T, E>>,
): Promise<T> {
const [error, data] = await result;
if (error) {
throw error;
}

return data as T;
}

0 comments on commit 8f40ae2

Please sign in to comment.