From 6e1d53e28a056e429c54e1e6af45eaa7939daa41 Mon Sep 17 00:00:00 2001 From: Kush Thaker Date: Wed, 31 Jul 2024 10:56:40 +0530 Subject: queues so far Co-authored-by: Dhravya Shah --- apps/cf-ai-backend/src/errors/baseError.ts | 46 ++++++++++++++++++++++++++++++ apps/cf-ai-backend/src/errors/results.ts | 28 ++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 apps/cf-ai-backend/src/errors/baseError.ts create mode 100644 apps/cf-ai-backend/src/errors/results.ts (limited to 'apps/cf-ai-backend/src/errors') diff --git a/apps/cf-ai-backend/src/errors/baseError.ts b/apps/cf-ai-backend/src/errors/baseError.ts new file mode 100644 index 00000000..2723d45b --- /dev/null +++ b/apps/cf-ai-backend/src/errors/baseError.ts @@ -0,0 +1,46 @@ +export class BaseHttpError extends Error { + public status: number; + public message: string; + + constructor(status: number, message: string) { + super(message); + this.status = status; + this.message = message; + Object.setPrototypeOf(this, new.target.prototype); // Restore prototype chain + } + } + + + export class BaseError extends Error { + type: string; + message: string; + source: string; + ignoreLog: boolean; + + constructor( + type: string, + message?: string, + source?: string, + ignoreLog = false + ) { + super(); + + Object.setPrototypeOf(this, new.target.prototype); + + this.type = type; + this.message = + message ?? + "An unknown error occurred. If this persists, please contact us."; + this.source = source ?? "unspecified"; + this.ignoreLog = ignoreLog; + } + + toJSON(): Record { + return { + type: this.type, + message: this.message, + source: this.source, + }; + } + } + \ No newline at end of file diff --git a/apps/cf-ai-backend/src/errors/results.ts b/apps/cf-ai-backend/src/errors/results.ts new file mode 100644 index 00000000..87ea0c63 --- /dev/null +++ b/apps/cf-ai-backend/src/errors/results.ts @@ -0,0 +1,28 @@ +import { BaseError } from "./baseError"; + +export type Result = + | { ok: true; value: T } + | { ok: false; error: E }; + +export const Ok = (data: T): Result => { + return { ok: true, value: data }; +}; + +export const Err = (error: E): Result => { + return { ok: false, error }; +}; + +export async function wrap( + p: Promise, + errorFactory: (err: Error) => E, +): Promise> { + try { + return Ok(await p); + } catch (e) { + return Err(errorFactory(e as Error)); + } +} + +export function isErr(result: Result): result is { ok: false; error: E } { + return !result.ok; + } \ No newline at end of file -- cgit v1.2.3 From e4fd7f5aacc3c9f7f000e1858248d49aa4d3410e Mon Sep 17 00:00:00 2001 From: Kush Thaker Date: Mon, 5 Aug 2024 21:25:11 +0530 Subject: move limit to backend and thread service binding --- apps/cf-ai-backend/src/errors/baseError.ts | 2 +- apps/cf-ai-backend/src/errors/results.ts | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) (limited to 'apps/cf-ai-backend/src/errors') diff --git a/apps/cf-ai-backend/src/errors/baseError.ts b/apps/cf-ai-backend/src/errors/baseError.ts index 2723d45b..0dcc2203 100644 --- a/apps/cf-ai-backend/src/errors/baseError.ts +++ b/apps/cf-ai-backend/src/errors/baseError.ts @@ -7,7 +7,7 @@ export class BaseHttpError extends Error { this.status = status; this.message = message; Object.setPrototypeOf(this, new.target.prototype); // Restore prototype chain - } + } } diff --git a/apps/cf-ai-backend/src/errors/results.ts b/apps/cf-ai-backend/src/errors/results.ts index 87ea0c63..ccce1396 100644 --- a/apps/cf-ai-backend/src/errors/results.ts +++ b/apps/cf-ai-backend/src/errors/results.ts @@ -14,15 +14,18 @@ export const Err = (error: E): Result => { export async function wrap( p: Promise, - errorFactory: (err: Error) => E, -): Promise> { + errorFactory: (err: Error, source: string) => E, + source: string = "unspecified" + ): Promise> { try { - return Ok(await p); + return Ok(await p); } catch (e) { - return Err(errorFactory(e as Error)); + return Err(errorFactory(e as Error, source)); } -} + } -export function isErr(result: Result): result is { ok: false; error: E } { - return !result.ok; - } \ No newline at end of file +export function isErr( + result: Result, +): result is { ok: false; error: E } { + return !result.ok; +} -- cgit v1.2.3