Skip to content

Commit

Permalink
updated error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NickPhura committed Jul 15, 2024
1 parent 89f9b2e commit 87bd66d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
20 changes: 4 additions & 16 deletions api/src/errors/api-error.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { BaseError } from './base-error';

export enum ApiErrorType {
BUILD_SQL = 'Error constructing SQL query',
EXECUTE_SQL = 'Error executing SQL query',
GENERAL = 'Error',
UNKNOWN = 'Unknown Error'
}

export class ApiError extends Error {
errors?: (string | object)[];

export class ApiError extends BaseError {
constructor(name: ApiErrorType, message: string, errors?: (string | object)[], stack?: string) {
super(message);

this.name = name;
this.errors = errors || [];
this.stack = stack;

if (stack) {
this.stack = stack;
}

if (!this.stack) {
Error.captureStackTrace(this);
}
super(name, message, errors, stack);
}
}

Expand Down
25 changes: 25 additions & 0 deletions api/src/errors/base-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export class BaseError extends Error {
errors: (string | object)[] = [];

constructor(name: string, message: string, errors?: (string | object)[], stack?: string) {
super(message);

this.name = name;

for (const error of errors ?? []) {
if (error instanceof Error) {
// By default, properties of Error objects are not enumerable, so we need to manually extract them.
this.errors.push({ name: error.name, message: error.message });
} else {
this.errors.push(error);
}
}

// If a stack is provided, use it. Otherwise, generate a new stack trace.
if (stack) {
this.stack = stack;
} else {
Error.captureStackTrace(this);
}
}
}
22 changes: 4 additions & 18 deletions api/src/errors/http-error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DatabaseError } from 'pg';
import { ApiError } from './api-error';
import { BaseError } from './base-error';

export enum HTTPErrorType {
BAD_REQUEST = 'Bad Request',
Expand All @@ -9,24 +10,13 @@ export enum HTTPErrorType {
INTERNAL_SERVER_ERROR = 'Internal Server Error'
}

export class HTTPError extends Error {
export class HTTPError extends BaseError {
status: number;
errors?: (string | object)[];

constructor(name: HTTPErrorType, status: number, message: string, errors?: (string | object)[], stack?: string) {
super(message);
super(name, message, errors, stack);

this.name = name;
this.status = status;
this.errors = errors || [];

if (stack) {
this.stack = stack;
}

if (!this.stack) {
Error.captureStackTrace(this);
}
}
}

Expand Down Expand Up @@ -125,9 +115,5 @@ export const ensureHTTPError = (error: HTTPError | ApiError | Error | any): HTTP
);
}

if (error instanceof Error) {
return new HTTP500('Unexpected Error', [error.name, error.message]);
}

return new HTTP500('Unexpected Error');
return new HTTP500('Unexpected Error', [error]);
};

0 comments on commit 87bd66d

Please sign in to comment.