-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtypes.ts
40 lines (34 loc) · 848 Bytes
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { Context } from "./context.ts";
export type Handler = (
ctx: Context,
) => Promise<unknown> | unknown;
export type ErrorInit = {
status?: number;
message?: string;
};
export class ServerError extends Error {
status: number;
expose: boolean;
constructor({
status = 500,
message = "An unknown error occurred.",
}: ErrorInit) {
super(message);
this.status = status;
this.expose = status < 500;
}
// deno-lint-ignore no-explicit-any
static from(error: any) {
const { status, message } = error;
return new ServerError({ status, message });
}
get #message() {
if (this.expose) return this.message;
return "An unknown error occurred.";
}
serialize = () => {
const { status } = this;
const message = this.#message;
return { error: { status, message } };
};
}