This repository was archived by the owner on Aug 27, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +20
-15
lines changed
Expand file tree Collapse file tree 5 files changed +20
-15
lines changed Original file line number Diff line number Diff line change @@ -11,20 +11,24 @@ Enjoy hassle-free HTTP error status handling:
1111import { ErrorCode , HttpError , raise } from " @wvm/http-error" ;
1212
1313// ErrorCode Mapper
14- ErrorCode .NOT_FOUND ; // 404
1514ErrorCode [404 ]; // NOT_FOUND
15+ ErrorCode .NOT_FOUND ; // 404
1616
1717// HttpError Raiser
18- throw raise (" NOT_FOUND" )` Missing Page ` ;
1918throw raise (404 )` Missing Page ` ;
19+ throw raise (" NOT_FOUND" )` Missing Page ` ;
2020
2121// ->
2222error .status ; // 404
2323error .phrase ; // NOT_FOUND
24- error .message ; // 404 NOT_FOUND: Missing Page
24+ error .message ; // Missing Page
2525
26- // HttpError Result
26+ // Handle HttpError
2727if (error instanceof HttpError ) {
28+ switch (error .phrase ) {
29+ // ...
30+ }
31+
2832 res .status (error .status ).send (error .message );
2933}
3034```
Original file line number Diff line number Diff line change 11{
22 "name" : " @wvm/http-error" ,
3- "version" : " 1.0.3 " ,
3+ "version" : " 1.0.4 " ,
44 "publish" : {
55 "exclude" : [" .github" ]
66 },
Original file line number Diff line number Diff line change 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 */
55export 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change @@ -2,9 +2,9 @@ import type { ErrorCode } from "./codes.ts";
22import { 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 */
99export function raise (
1010 error : ErrorCode | keyof typeof ErrorCode ,
You can’t perform that action at this time.
0 commit comments