Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.

Commit 1ef3ffb

Browse files
committed
v1.0.4 simplify error message
1 parent 874d8c7 commit 1ef3ffb

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,24 @@ Enjoy hassle-free HTTP error status handling:
1111
import { ErrorCode, HttpError, raise } from "@wvm/http-error";
1212

1313
// ErrorCode Mapper
14-
ErrorCode.NOT_FOUND; // 404
1514
ErrorCode[404]; // NOT_FOUND
15+
ErrorCode.NOT_FOUND; // 404
1616

1717
// HttpError Raiser
18-
throw raise("NOT_FOUND")`Missing Page`;
1918
throw raise(404)`Missing Page`;
19+
throw raise("NOT_FOUND")`Missing Page`;
2020

2121
// ->
2222
error.status; // 404
2323
error.phrase; // NOT_FOUND
24-
error.message; // 404 NOT_FOUND: Missing Page
24+
error.message; // Missing Page
2525

26-
// HttpError Result
26+
// Handle HttpError
2727
if (error instanceof HttpError) {
28+
switch (error.phrase) {
29+
// ...
30+
}
31+
2832
res.status(error.status).send(error.message);
2933
}
3034
```

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wvm/http-error",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"publish": {
55
"exclude": [".github"]
66
},

src/codes.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* HTTP 4xx/5xx Error Codes
2+
* HTTP Error Codes
33
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status)
44
*/
55
export enum ErrorCode {
@@ -33,12 +33,16 @@ export enum ErrorCode {
3333
TOO_MANY_REQUESTS = 429,
3434
REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
3535
UNAVAILABLE_FOR_LEGAL_REASONS = 451,
36+
3637
INTERNAL_SERVER_ERROR = 500,
3738
NOT_IMPLEMENTED = 501,
3839
BAD_GATEWAY = 502,
3940
SERVICE_UNAVAILABLE = 503,
4041
GATEWAY_TIMEOUT = 504,
4142
HTTP_VERSION_NOT_SUPPORTED = 505,
43+
VARIANT_ALSO_NEGOTIATES = 506,
4244
INSUFFICIENT_STORAGE = 507,
45+
LOOP_DETECTED = 508,
46+
NOT_EXTENDED = 510,
4347
NETWORK_AUTHENTICATION_REQUIRED = 511,
4448
}

src/error.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ export class HttpError extends Error {
1515

1616
/**
1717
* @param error HTTP Error Code/Phrase
18-
* @param message HTTP Error Cause/Details
18+
* @param message HTTP Error Details
1919
*/
2020
constructor(error: ErrorCode | keyof typeof ErrorCode, message: string) {
21-
const status = typeof error === "number" ? error : ErrorCode[error];
22-
const phrase = typeof error === "number" ? ErrorCode[error] : error;
21+
super(message);
2322

24-
super(`${status} ${phrase}: ${message}`);
25-
26-
this.status = status;
27-
this.phrase = phrase;
23+
this.status = typeof error === "number" ? error : ErrorCode[error];
24+
this.phrase = typeof error === "number" ? ErrorCode[error] : error;
2825
}
2926
}

src/raise.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { ErrorCode } from "./codes.ts";
22
import { HttpError } from "./error.ts";
33

44
/**
5-
* Get HTTP Error With Tagged Templates
5+
* HTTP Error With Tagged Templates
66
* @param error HTTP Error Code/Phrase
7-
* @returns Tag Function For HTTP Error
7+
* @returns HTTP Error Tag Function
88
*/
99
export function raise(
1010
error: ErrorCode | keyof typeof ErrorCode,

0 commit comments

Comments
 (0)