Skip to content

Commit a2e14e7

Browse files
committed
Add ErrorWithExtras and StatusError
1 parent d436930 commit a2e14e7

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/errors.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,43 @@ export abstract class CustomError extends Error {
4646
}
4747
}
4848

49+
export class ErrorWithExtras extends CustomError {
50+
constructor(
51+
message: string,
52+
extras: {
53+
code?: string,
54+
statusCode?: number,
55+
cause?: Error
56+
}
57+
) {
58+
super(message);
59+
60+
this.code = extras.code;
61+
this.statusCode = extras.statusCode;
62+
this.cause = extras.cause;
63+
}
64+
65+
public readonly code?: string;
66+
public readonly statusCode?: number;
67+
public readonly cause?: Error;
68+
}
69+
70+
export class StatusError extends ErrorWithExtras {
71+
constructor(
72+
/**
73+
* Should be a valid HTTP status code
74+
*/
75+
statusCode: number,
76+
message: string,
77+
extras: {
78+
code?: string,
79+
cause?: Error
80+
} = {}
81+
) {
82+
super(message, { ...extras, statusCode: statusCode });
83+
}
84+
}
85+
4986
/**
5087
* An error to throw in expected-never cases - by using this, you ask TypeScript to
5188
* be sure that it agrees that the case is truly unreachable.

0 commit comments

Comments
 (0)