1
1
# Http Error
2
2
3
- [ ![ version ] ( https://img.shields. io/badge/release-0.7.0-success )] ( https://deno.land/x/[email protected] )
4
- [ ![ deno doc ] ( https://doc.deno.land/badge.svg )] ( https://doc.deno.land/https/deno.land/x/[email protected] /mod.ts )
5
- [ ![ CI] ( https://github.com/udibo/http_error /workflows/CI/badge.svg )] ( https://github.com/udibo/http_error /actions?query=workflow%3ACI )
6
- [ ![ codecov] ( https://codecov.io/gh/udibo/http_error /branch/main/graph/badge.svg?token=8Q7TSUFWUY )] ( https://codecov.io/gh/udibo/http_error )
7
- [ ![ license] ( https://img.shields.io/github/license/udibo/http_error )] ( https://github.com/udibo/http_error /blob/master/LICENSE )
3
+ [ ![ JSR ] ( https://jsr. io/badges/@udibo/http-error )] ( https://jsr.io/@udibo/http-error )
4
+ [ ![ JSR Score ] ( https://jsr.io/badges/@udibo/http-error/score )] ( https://jsr.io/@udibo/http-error )
5
+ [ ![ CI] ( https://github.com/udibo/http-error /workflows/CI/badge.svg )] ( https://github.com/udibo/http-error /actions?query=workflow%3ACI )
6
+ [ ![ codecov] ( https://codecov.io/gh/udibo/http-error /branch/main/graph/badge.svg?token=8Q7TSUFWUY )] ( https://codecov.io/gh/udibo/http-error )
7
+ [ ![ license] ( https://img.shields.io/github/license/udibo/http-error )] ( https://github.com/udibo/http-error /blob/master/LICENSE )
8
8
9
- An error class for HTTP requests .
9
+ Utilities for creating and working with Http Errors .
10
10
11
- This module was inspired by
11
+ This package was inspired by
12
12
[ http-errors] ( https://www.npmjs.com/package/http-errors ) for Node.js.
13
13
14
14
## Features
15
15
16
16
- Framework agnostic
17
17
18
- ## Installation
19
-
20
- This is an ES Module written in TypeScript and can be used in Deno projects. ES
21
- Modules are the official standard format to package JavaScript code for reuse. A
22
- JavaScript bundle is provided with each release so that it can be used in
23
- Node.js packages or web browsers.
24
-
25
- ### Deno
26
-
27
- To include it in a Deno project, you can import directly from the TS files. This
28
- module is available in Deno's third part module registry but can also be
29
- imported directly from GitHub using raw content URLs.
30
-
31
- ``` ts
32
- // Import from Deno's third party module registry
33
- import {
HttpError ,
isHttpError }
from " https://deno.land/x/[email protected] /mod.ts" ;
34
- // Import from GitHub
35
- import { HttpError , isHttpError } " https://raw.githubusercontent.com/udibo/http_error/0.7.0/mod.ts" ;
36
- ` ` `
37
-
38
- ### Node.js
39
-
40
- Node.js fully supports ES Modules.
41
-
42
- If a Node.js package has the type "module" specified in its package.json file,
43
- the JavaScript bundle can be imported as a ` .js ` file.
44
-
45
- ` ` ` js
46
- import { HttpError , isHttpError } from " ./http_error_0.7.0.js" ;
47
- ```
48
-
49
- The default type for Node.js packages is "commonjs". To import the bundle into a
50
- commonjs package, the file extension of the JavaScript bundle must be changed
51
- from ` .js ` to ` .mjs ` .
52
-
53
- ``` js
54
- import { HttpError , isHttpError } from " ./http_error_0.7.0.mjs" ;
55
- ```
56
-
57
- See [ Node.js Documentation] ( https://nodejs.org/api/esm.html ) for more
58
- information.
59
-
60
- ### Browser
61
-
62
- Most modern browsers support ES Modules.
63
-
64
- The JavaScript bundle can be imported into ES modules. Script tags for ES
65
- modules must have the type attribute set to "module".
66
-
67
- ``` html
68
- <script type =" module" src =" main.js" ></script >
69
- ```
70
-
71
- ``` js
72
- // main.js
73
- import { HttpError , isHttpError } from " ./http_error_0.7.0.js" ;
74
- ```
75
-
76
- You can also embed a module script directly into an HTML file by placing the
77
- JavaScript code within the body of the script tag.
78
-
79
- ``` html
80
- <script type =" module" >
81
- import { HttpError , isHttpError } from " ./http_error_0.7.0.js" ;
82
- </script >
83
- ```
84
-
85
- See
86
- [ MDN Documentation] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules )
87
- for more information.
88
-
89
18
## Usage
90
19
91
20
Below are some examples of how to use this module.
@@ -97,6 +26,8 @@ different call signatures you can use. The 4 examples below would throw the same
97
26
error.
98
27
99
28
``` ts
29
+ import { HttpError } from " @udibo/http-error" ;
30
+
100
31
throw new HttpError (404 , " file not found" );
101
32
throw new HttpError (404 , { message: " file not found" });
102
33
throw new HttpError (" file not found" , { status: 404 });
@@ -107,6 +38,8 @@ You can also include a cause in the optional options argument for it like you
107
38
can with regular errors.
108
39
109
40
``` ts
41
+ import { HttpError } from " @udibo/http-error" ;
42
+
110
43
throw new HttpError (404 , " file not found" , { cause: error });
111
44
```
112
45
@@ -121,6 +54,8 @@ the name is not known for an HTTP error status code, it will default to
121
54
UnknownClientError or UnknownServerError.
122
55
123
56
``` ts
57
+ import { HttpError } from " @udibo/http-error" ;
58
+
124
59
const error = new HttpError (404 , " file not found" );
125
60
console .log (error .toString ()); // NotFoundError: file not found
126
61
```
@@ -129,6 +64,8 @@ If you would like to extend the HttpError class, you can pass your own error
129
64
name in the options.
130
65
131
66
``` ts
67
+ import { HttpError , type HttpErrorOptions } from " @udibo/http-error" ;
68
+
132
69
class CustomError extends HttpError {
133
70
constructor (
134
71
message ? : string ,
@@ -144,6 +81,12 @@ signature, you can make use of the optionsFromArgs function. It will prioritize
144
81
the status / message arguments over status / message options.
145
82
146
83
``` ts
84
+ import {
85
+ HttpError ,
86
+ type HttpErrorOptions ,
87
+ optionsFromArgs ,
88
+ } from " @udibo/http-error" ;
89
+
147
90
class CustomError extends HttpError {
148
91
constructor (
149
92
status ? : number ,
@@ -175,6 +118,8 @@ will also return true for Error objects that have status and expose properties
175
118
with matching types.
176
119
177
120
``` ts
121
+ import { HttpError , isHttpError } from " @udibo/http-error" ;
122
+
178
123
let error = new Error (" file not found" );
179
124
console .log (isHttpError (error )); // false
180
125
error = new HttpError (404 , " file not found" );
@@ -183,22 +128,35 @@ console.log(isHttpError(error)); // true
183
128
184
129
### ErrorResponse
185
130
186
- This object can be used to transform an HttpError into a JSON format that can be
131
+ This class can be used to transform an HttpError into a JSON format that can be
187
132
converted back into an HttpError. This makes it easy for the server to share
188
133
HttpError's with the client. This will work with any value that is thrown.
189
134
190
- Here is an example of how an oak server could have middleware that convert an
135
+ Here is an example of how an oak server could have middleware that converts an
191
136
error into into a JSON format.
192
137
193
138
``` ts
194
- app .use (async (ctx , next ) => {
139
+ import { Application } from " @oak/oak/application" ;
140
+ import { ErrorResponse , HttpError } from " @udibo/http-error" ;
141
+
142
+ const app = new Application ();
143
+
144
+ app .use (async (context , next ) => {
195
145
try {
196
146
await next ();
197
147
} catch (error ) {
148
+ const { response } = context ;
198
149
response .status = isHttpError (error ) ? error .status : 500 ;
199
150
response .body = new ErrorResponse (error );
200
151
}
201
152
});
153
+
154
+ app .use (() => {
155
+ // Will throw a 500 on every request.
156
+ throw new HttpError (500 );
157
+ });
158
+
159
+ await app .listen ({ port: 80 });
202
160
```
203
161
204
162
When ` JSON.stringify ` is used on the ErrorResponse object, the ErrorResponse
@@ -209,6 +167,8 @@ that example, the response to the request would have it's status match the error
209
167
and the body be a JSON representation of the error.
210
168
211
169
``` ts
170
+ import { HttpError } from " @udibo/http-error" ;
171
+
212
172
throw new HttpError (400 , " Invalid input" );
213
173
```
214
174
@@ -224,11 +184,6 @@ Then the response would have a 400 status and it's body would look like this:
224
184
}
225
185
```
226
186
227
- If the format of your error responses is different than this, you can look at
228
- the source code in [ mod.ts] ( /mod.ts ) to see how you could create your own
229
- ErrorResponse object that can be used to convert your error responses into
230
- HttpErrors.
231
-
232
187
#### ErrorResponse.toError
233
188
234
189
This function gives a client the ability to convert the error response JSON into
@@ -238,6 +193,8 @@ In the following example, if getMovies is called and API endpoint returned an
238
193
ErrorResponse, it would be converted into an HttpError object and be thrown.
239
194
240
195
``` ts
196
+ import { ErrorResponse , HttpError , isErrorResponse } from " @udibo/http-error" ;
197
+
241
198
async function getMovies() {
242
199
const response = await fetch (" https://example.com/movies.json" );
243
200
const movies = await response .json ();
@@ -266,6 +223,8 @@ The error that `getMovies` would throw would be equivalent to throwing the
266
223
following HttpError.
267
224
268
225
``` ts
226
+ import { HttpError } from " @udibo/http-error" ;
227
+
269
228
new HttpError (400 , " Invalid input" );
270
229
```
271
230
@@ -281,6 +240,8 @@ thrown. But if it isn't in that format and doesn't have an error status, the
281
240
response body will be returned as the assumed movies.
282
241
283
242
``` ts
243
+ import { HttpError , isErrorResponse } from " @udibo/http-error" ;
244
+
284
245
async function getMovies() {
285
246
const response = await fetch (" https://example.com/movies.json" );
286
247
const movies = await response .json ();
0 commit comments