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

Commit 5fc3bf8

Browse files
committed
v1.0.0 public release
1 parent e015891 commit 5fc3bf8

File tree

6 files changed

+110
-13
lines changed

6 files changed

+110
-13
lines changed

README

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1-
<!-- replace all &pack -->
2-
31
<h3 align="center">
4-
<img src="https://raw.githubusercontent.com/wavim/&pack/master/assets/icon.png" width="130" alt="&pack icon" />
5-
<br />
6-
&pack name
2+
HTTP Error
73
</h3>
8-
<p align="center">&pack description</p>
4+
<p align="center">Convenient HTTP Error Utilities</p>
95

106
---
117

128
### Usage
139

14-
Add this package in your project:
10+
Add this package to your project:
1511

1612
```bash
17-
deno add jsr:@wvm/&pack
13+
deno add jsr:@wvm/http-error
1814
```
1915

20-
---
16+
And enjoy hassle-free HTTP error status handling:
17+
18+
```ts
19+
import { ErrorCode, HttpError, raise } from "@wvm/http-error";
20+
21+
// ErrorCode Mapper
22+
ErrorCode.NOT_FOUND; // 404
23+
ErrorCode[404]; // NOT_FOUND
2124

22-
_&emsp;_ &pack quotes
25+
// HttpError Raiser
26+
throw raise("NOT_FOUND")`Missing Page`;
27+
throw raise(404)`Missing Page`;
28+
29+
// ->
30+
error.status; // 404
31+
error.phrase; // NOT_FOUND
32+
error.message; // 404 NOT_FOUND: Missing Page
33+
34+
// HttpError Result
35+
if (error instanceof HttpError) {
36+
res.status(error.status).send(error.message);
37+
}
38+
```

assets/icon.png

Whitespace-only changes.

deno.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@wvm/&pack",
3-
"version": "0.0.0-r",
2+
"name": "@wvm/http-error",
3+
"version": "1.0.0",
44
"publish": {
55
"exclude": ["assets"]
66
},

src/codes.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* HTTP 4xx/5xx Error Codes
3+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status)
4+
*/
5+
export enum ErrorCode {
6+
BAD_REQUEST = 400,
7+
UNAUTHORIZED = 401,
8+
PAYMENT_REQUIRED = 402,
9+
FORBIDDEN = 403,
10+
NOT_FOUND = 404,
11+
METHOD_NOT_ALLOWED = 405,
12+
NOT_ACCEPTABLE = 406,
13+
PROXY_AUTHENTICATION_REQUIRED = 407,
14+
REQUEST_TIMEOUT = 408,
15+
CONFLICT = 409,
16+
GONE = 410,
17+
LENGTH_REQUIRED = 411,
18+
PRECONDITION_FAILED = 412,
19+
REQUEST_TOO_LONG = 413,
20+
REQUEST_URI_TOO_LONG = 414,
21+
UNSUPPORTED_MEDIA_TYPE = 415,
22+
REQUESTED_RANGE_NOT_SATISFIABLE = 416,
23+
EXPECTATION_FAILED = 417,
24+
IM_A_TEAPOT = 418,
25+
INSUFFICIENT_SPACE_ON_RESOURCE = 419,
26+
METHOD_FAILURE = 420,
27+
MISDIRECTED_REQUEST = 421,
28+
UNPROCESSABLE_ENTITY = 422,
29+
LOCKED = 423,
30+
FAILED_DEPENDENCY = 424,
31+
UPGRADE_REQUIRED = 426,
32+
PRECONDITION_REQUIRED = 428,
33+
TOO_MANY_REQUESTS = 429,
34+
REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
35+
UNAVAILABLE_FOR_LEGAL_REASONS = 451,
36+
INTERNAL_SERVER_ERROR = 500,
37+
NOT_IMPLEMENTED = 501,
38+
BAD_GATEWAY = 502,
39+
SERVICE_UNAVAILABLE = 503,
40+
GATEWAY_TIMEOUT = 504,
41+
HTTP_VERSION_NOT_SUPPORTED = 505,
42+
INSUFFICIENT_STORAGE = 507,
43+
NETWORK_AUTHENTICATION_REQUIRED = 511,
44+
}

src/error.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ErrorCode } from "./codes.ts";
2+
3+
/**
4+
* HTTP Error Class With Status Pair
5+
*/
6+
export class HttpError extends Error {
7+
readonly status: number;
8+
readonly phrase: string;
9+
10+
/**
11+
* @param error HTTP Error Code/Phrase
12+
* @param message HTTP Error Cause/Details
13+
*/
14+
constructor(error: ErrorCode | keyof typeof ErrorCode, message: string) {
15+
const status = typeof error === "number" ? error : ErrorCode[error];
16+
const phrase = typeof error === "number" ? ErrorCode[error] : error;
17+
18+
super(`${status} ${phrase}: ${message}`);
19+
20+
this.status = status;
21+
this.phrase = phrase;
22+
}
23+
}

src/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
console.log("Have a nice day!");
1+
import type { ErrorCode } from "./codes.ts";
2+
export { ErrorCode } from "./codes.ts";
3+
import { HttpError } from "./error.ts";
4+
export { HttpError } from "./error.ts";
5+
6+
/**
7+
* Get HTTP Error With Tagged Templates
8+
* @param error HTTP Error Code/Phrase
9+
* @returns Tag Function For HTTP Error
10+
*/
11+
export function raise(
12+
error: ErrorCode | keyof typeof ErrorCode,
13+
): (...template: Parameters<typeof String.raw>) => HttpError {
14+
return (...template) => new HttpError(error, String.raw(...template));
15+
}

0 commit comments

Comments
 (0)