aboutsummaryrefslogtreecommitdiff
path: root/apps/cf-ai-backend/src/errors
diff options
context:
space:
mode:
authorKush Thaker <[email protected]>2024-07-31 10:56:40 +0530
committerKush Thaker <[email protected]>2024-07-31 10:56:40 +0530
commit6e1d53e28a056e429c54e1e6af45eaa7939daa41 (patch)
tree21dfd3c64742d9e9405e68b1695acbb95f5fdde2 /apps/cf-ai-backend/src/errors
parentfix ids not present in storecontent (diff)
downloadsupermemory-6e1d53e28a056e429c54e1e6af45eaa7939daa41.tar.xz
supermemory-6e1d53e28a056e429c54e1e6af45eaa7939daa41.zip
queues so far
Co-authored-by: Dhravya Shah <[email protected]>
Diffstat (limited to 'apps/cf-ai-backend/src/errors')
-rw-r--r--apps/cf-ai-backend/src/errors/baseError.ts46
-rw-r--r--apps/cf-ai-backend/src/errors/results.ts28
2 files changed, 74 insertions, 0 deletions
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<PropertyKey, string> {
+ 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<T, E extends Error> =
+ | { ok: true; value: T }
+ | { ok: false; error: E };
+
+export const Ok = <T>(data: T): Result<T, never> => {
+ return { ok: true, value: data };
+};
+
+export const Err = <E extends BaseError>(error: E): Result<never, E> => {
+ return { ok: false, error };
+};
+
+export async function wrap<T, E extends BaseError>(
+ p: Promise<T>,
+ errorFactory: (err: Error) => E,
+): Promise<Result<T, E>> {
+ try {
+ return Ok(await p);
+ } catch (e) {
+ return Err(errorFactory(e as Error));
+ }
+}
+
+export function isErr<T, E extends Error>(result: Result<T, E>): result is { ok: false; error: E } {
+ return !result.ok;
+ } \ No newline at end of file