diff options
Diffstat (limited to 'packages/server/src/git/error/index.ts')
-rw-r--r-- | packages/server/src/git/error/index.ts | 60 |
1 files changed, 60 insertions, 0 deletions
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<T extends new (...args: string[]) => ErrorType>(where: ErrorWhere, ErrType: T, ...args: ConstructorParameters<T>): 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<T extends new () => ErrorType>(where: ErrorWhere, ErrType: T): ServerError; + +export function createError<T extends new(...args: string[]) => ErrorType>(where: ErrorWhere, ErrType: T, ...args: ConstructorParameters<T>): ServerError { + return new ServerError(where, new ErrType(...args)); +}
\ No newline at end of file |