refactor(core): standardize HttpError (#2858)
- Now extends `Error`, so that it satisfies `instanceof Error` operations. - Narrows the `status` property to be an actual HTTP status code. - Outsources default evaluation of the `message` property to `@std/http/status`, which already implements this functionality. - Brings the `stack` property to the class, regardless of its status, which seems like a needless constraint.
This commit is contained in:
@@ -40,6 +40,7 @@
|
||||
"exclude": ["**/*_test.*", "src/__OLD/**", "*.todo"]
|
||||
},
|
||||
"imports": {
|
||||
"@std/http": "jsr:@std/http@^1.0.15",
|
||||
"fresh": "jsr:@fresh/core@^2.0.0-alpha.26",
|
||||
"preact": "npm:preact@^10.25.1",
|
||||
"preact-render-to-string": "npm:preact-render-to-string@^6.5.11",
|
||||
|
||||
+9
-147
@@ -1,153 +1,15 @@
|
||||
export function getMessage(status: number): string {
|
||||
switch (status) {
|
||||
case 100:
|
||||
return "Continue";
|
||||
case 101:
|
||||
return "Switching Protocols";
|
||||
case 102:
|
||||
return "Processing (WebDAV)";
|
||||
case 103:
|
||||
return "Early Hints";
|
||||
case 200:
|
||||
return "OK";
|
||||
case 201:
|
||||
return "Created";
|
||||
case 202:
|
||||
return "Accepted";
|
||||
case 203:
|
||||
return "Non-Authoritative Information";
|
||||
case 204:
|
||||
return "No Content";
|
||||
case 205:
|
||||
return "Reset Content";
|
||||
case 206:
|
||||
return "Partial Content";
|
||||
case 207:
|
||||
return "Multi-Status (WebDAV)";
|
||||
case 208:
|
||||
return "Already Reported (WebDAV)";
|
||||
case 226:
|
||||
return "IM Used (HTTP Delta encoding)";
|
||||
case 300:
|
||||
return "Multiple Choices";
|
||||
case 301:
|
||||
return "Moved Permanently";
|
||||
case 302:
|
||||
return "Found";
|
||||
case 303:
|
||||
return "See Other";
|
||||
case 304:
|
||||
return "Not Modified";
|
||||
case 305:
|
||||
return "Use Proxy Deprecated";
|
||||
case 306:
|
||||
return "unused";
|
||||
case 307:
|
||||
return "Temporary Redirect";
|
||||
case 308:
|
||||
return "Permanent Redirect";
|
||||
case 400:
|
||||
return "Bad Request";
|
||||
case 401:
|
||||
return "Unauthorized";
|
||||
case 402:
|
||||
return "Payment Required Experimental";
|
||||
case 403:
|
||||
return "Forbidden";
|
||||
case 404:
|
||||
return "Not Found";
|
||||
case 405:
|
||||
return "Method Not Allowed";
|
||||
case 406:
|
||||
return "Not Acceptable";
|
||||
case 407:
|
||||
return "Proxy Authentication Required";
|
||||
case 408:
|
||||
return "Request Timeout";
|
||||
case 409:
|
||||
return "Conflict";
|
||||
case 410:
|
||||
return "Gone";
|
||||
case 411:
|
||||
return "Length Required";
|
||||
case 412:
|
||||
return "Precondition Failed";
|
||||
case 413:
|
||||
return "Payload Too Large";
|
||||
case 414:
|
||||
return "URI Too Long";
|
||||
case 415:
|
||||
return "Unsupported Media Type";
|
||||
case 416:
|
||||
return "Range Not Satisfiable";
|
||||
case 417:
|
||||
return "Expectation Failed";
|
||||
case 418:
|
||||
return "I'm a teapot";
|
||||
case 421:
|
||||
return "Misdirected Request";
|
||||
case 422:
|
||||
return "Unprocessable Content (WebDAV)";
|
||||
case 423:
|
||||
return "Locked (WebDAV)";
|
||||
case 424:
|
||||
return "Failed Dependency (WebDAV)";
|
||||
case 425:
|
||||
return "Too Early Experimental";
|
||||
case 426:
|
||||
return "Upgrade Required";
|
||||
case 428:
|
||||
return "Precondition Required";
|
||||
case 429:
|
||||
return "Too Many Requests";
|
||||
case 431:
|
||||
return "Request Header Fields Too Large";
|
||||
case 451:
|
||||
return "Unavailable For Legal Reasons";
|
||||
case 500:
|
||||
return "Internal Server Error";
|
||||
case 501:
|
||||
return "Not Implemented";
|
||||
case 502:
|
||||
return "Bad Gateway";
|
||||
case 503:
|
||||
return "Service Unavailable";
|
||||
case 504:
|
||||
return "Gateway Timeout";
|
||||
case 505:
|
||||
return "HTTP Version Not Supported";
|
||||
case 506:
|
||||
return "Variant Also Negotiates";
|
||||
case 507:
|
||||
return "Insufficient Storage (WebDAV)";
|
||||
case 508:
|
||||
return "Loop Detected (WebDAV)";
|
||||
case 510:
|
||||
return "Not Extended";
|
||||
case 511:
|
||||
return "Network Authentication Required";
|
||||
default:
|
||||
return "Internal Server Error";
|
||||
}
|
||||
}
|
||||
import { STATUS_TEXT } from "@std/http/status";
|
||||
|
||||
export class HttpError {
|
||||
#error: Error | null = null;
|
||||
name = "HttpError";
|
||||
message: string;
|
||||
export class HttpError extends Error {
|
||||
status: number;
|
||||
|
||||
constructor(
|
||||
public status: number,
|
||||
message: string = getMessage(status),
|
||||
public options?: ErrorOptions,
|
||||
status: keyof typeof STATUS_TEXT,
|
||||
message: string = STATUS_TEXT[status],
|
||||
options?: ErrorOptions,
|
||||
) {
|
||||
this.message = message;
|
||||
if (status >= 500) {
|
||||
this.#error = new Error();
|
||||
}
|
||||
}
|
||||
|
||||
get stack(): string | undefined {
|
||||
return this.#error?.stack;
|
||||
super(message, options);
|
||||
this.name = this.constructor.name;
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -1,12 +1,14 @@
|
||||
import { expect } from "@std/expect";
|
||||
import { HttpError } from "./error.ts";
|
||||
|
||||
Deno.test("HttpError - contains stack if error >=500", () => {
|
||||
Deno.test("HttpError", () => {
|
||||
const err = new HttpError(404);
|
||||
expect(err.status).toEqual(404);
|
||||
expect(typeof err.stack).toEqual("undefined");
|
||||
expect(err.message).toEqual("Not Found");
|
||||
expect(typeof err.stack).toEqual("string");
|
||||
|
||||
const err2 = new HttpError(500);
|
||||
expect(err2.status).toEqual(500);
|
||||
expect(err2.message).toEqual("Internal Server Error");
|
||||
expect(typeof err2.stack).toEqual("string");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user