aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/git/error
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-08-14 22:28:17 +0200
committerHampusM <hampus@hampusmat.com>2021-08-14 22:28:17 +0200
commitddcf71225226bc0929d5c6d609b13ff0489e5b94 (patch)
tree7f788d29af4441e27d709aaec1359e205871a11b /packages/server/src/git/error
parent81b39ee7848b0fbdcd8b61a04077a58c23580dd1 (diff)
Revamped backend error handling & improved imports in tests
Diffstat (limited to 'packages/server/src/git/error')
-rw-r--r--packages/server/src/git/error/index.ts60
-rw-r--r--packages/server/src/git/error/types.ts39
2 files changed, 99 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
diff --git a/packages/server/src/git/error/types.ts b/packages/server/src/git/error/types.ts
new file mode 100644
index 0000000..b8f2de7
--- /dev/null
+++ b/packages/server/src/git/error/types.ts
@@ -0,0 +1,39 @@
+export abstract class ErrorType {
+ code: number;
+ message: string;
+
+ constructor(code: number, message: string) {
+ this.code = code;
+ this.message = message;
+ }
+}
+
+export class UnknownError extends ErrorType {
+ constructor() {
+ super(500, "Unknown error!");
+ }
+}
+
+export class NotFoundError extends ErrorType {
+ constructor(target: string) {
+ super(404, `${target} not found!`);
+ }
+}
+
+export class FailedError extends ErrorType {
+ constructor(attempt: string) {
+ super(500, `Failed to ${attempt}!`);
+ }
+}
+
+export class IsNotError extends ErrorType {
+ constructor(target: string, not: string) {
+ super(500, `${target} is not a ${not}!`);
+ }
+}
+
+export class CommitNotSignedError extends ErrorType {
+ constructor() {
+ super(500, "Commit isn't signed!");
+ }
+} \ No newline at end of file