From ddcf71225226bc0929d5c6d609b13ff0489e5b94 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 14 Aug 2021 22:28:17 +0200 Subject: Revamped backend error handling & improved imports in tests --- packages/server/src/git/error/index.ts | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 packages/server/src/git/error/index.ts (limited to 'packages/server/src/git/error/index.ts') diff --git a/packages/server/src/git/error/index.ts b/packages/server/src/git/error/index.ts new file mode 100644 index 0000000..b8994d3 --- /dev/null +++ b/packages/server/src/git/error/index.ts @@ -0,0 +1,60 @@ +import { ErrorType } from "./types"; + +export * from "./types"; + +export class ServerError extends Error { + code: number; + + constructor(where: string, error_type: { code: number, message: string }) { + super(`A ${where} error has occured: ${error_type.message}`); + + this.name = "Server Error"; + this.code = error_type.code; + } +} + +export enum ErrorWhere { + Repository = "repository", + Tree = "tree", + Tag = "tag", + Branch = "branch", + Commit = "commit", + Diff = "diff", + Misc = "misc", + Blob = "blob" +} + +/** + * A error factory + * + * @param where - Where the error has occured + * @param err_type - What type of error it is + * @param args - Parameters for the error type + * + * @returns A server error + * + * @example Example usage: + * import { ErrorType, NotFoundError, createError } from "./error"; + * + * throw(createError(ErrorType.Repository, NotFoundError, "chili sauce")); + */ +export function createError ErrorType>(where: ErrorWhere, ErrType: T, ...args: ConstructorParameters): ServerError; + +/** + * A error factory + * + * @param where - Where the error has occured + * @param err_type - What type of error it is + * + * @returns A server error + * + * @example Example usage: + * import { ErrorType, UnknownError, createError } from "./error"; + * + * throw(createError(ErrorType.Repository, UnknownError)); + */ +export function createError ErrorType>(where: ErrorWhere, ErrType: T): ServerError; + +export function createError ErrorType>(where: ErrorWhere, ErrType: T, ...args: ConstructorParameters): ServerError { + return new ServerError(where, new ErrType(...args)); +} \ No newline at end of file -- cgit v1.2.3-18-g5258