-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.ts
93 lines (84 loc) · 1.95 KB
/
mod.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*!
* Copyright (c) 2020 Henry Zhuang
* MIT Licensed
*/
import { Status, STATUS_TEXT } from "./deps.ts";
export abstract class HttpError extends Error {
name: string;
message: string;
status: number;
expose: boolean = false;
[key: string]: any
constructor(code: number, message?: string) {
super(message);
if (!Status[code]) {
throw TypeError(`Unknown HTTP Status Code \`${code}\``);
}
if (code < 400 || code >= 600) {
throw TypeError(
`Only 4xx or 5xx status codes allowed, but got \`${code}\``,
);
}
if (code >= 400 && code < 500) {
this.expose = true;
}
let className = Status[code];
if (!className.endsWith("Error")) {
className += "Error";
}
const msg = message != null ? message : STATUS_TEXT.get(code)!;
this.message = msg;
this.status = code;
this.name = className;
(Error as any).captureStackTrace(this, this.constructor);
Object.setPrototypeOf(this, new.target.prototype);
}
toString() {
return `${this.name} [${this.status}]: ${this.message}`;
}
toJSON() {
return {
name: this.name,
status: this.status,
message: this.message,
};
}
}
class HttpErrorImpl extends HttpError {}
export interface Props {
[key: string]: any;
}
/**
* Create a new HttpError.
*
* @returns {HttpError}
* @public
*/
export function createError(
status: number,
message?: string,
props?: Props,
): HttpError;
export function createError(status: number, props: Props): HttpError;
export function createError(
status: number,
message?: any,
props?: Props,
): HttpError {
let err;
if (typeof message === "string") {
err = new HttpErrorImpl(status, message);
} else {
props = message;
err = new HttpErrorImpl(status);
}
if (props) {
for (let key in props) {
if (key !== "status") {
err[key] = props[key];
}
}
}
(Error as any).captureStackTrace(err, createError);
return err;
}