Skip to content

Commit

Permalink
Add ErrorWithExtras and StatusError
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Jun 6, 2024
1 parent d436930 commit a2e14e7
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,43 @@ export abstract class CustomError extends Error {
}
}

export class ErrorWithExtras extends CustomError {
constructor(
message: string,
extras: {
code?: string,
statusCode?: number,
cause?: Error
}
) {
super(message);

this.code = extras.code;
this.statusCode = extras.statusCode;
this.cause = extras.cause;
}

public readonly code?: string;
public readonly statusCode?: number;
public readonly cause?: Error;
}

export class StatusError extends ErrorWithExtras {
constructor(
/**
* Should be a valid HTTP status code
*/
statusCode: number,
message: string,
extras: {
code?: string,
cause?: Error
} = {}
) {
super(message, { ...extras, statusCode: statusCode });
}
}

/**
* An error to throw in expected-never cases - by using this, you ask TypeScript to
* be sure that it agrees that the case is truly unreachable.
Expand Down

0 comments on commit a2e14e7

Please sign in to comment.