-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from udibo/dev
Prepare for publishing to JSR
- Loading branch information
Showing
10 changed files
with
507 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: publish | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
id-token: write | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- run: npx jsr publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,20 @@ | ||
# Http Error | ||
|
||
[![version](https://img.shields.io/badge/release-0.7.0-success)](https://deno.land/x/[email protected]) | ||
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/[email protected]/mod.ts) | ||
[![CI](https://github.com/udibo/http_error/workflows/CI/badge.svg)](https://github.com/udibo/http_error/actions?query=workflow%3ACI) | ||
[![codecov](https://codecov.io/gh/udibo/http_error/branch/main/graph/badge.svg?token=8Q7TSUFWUY)](https://codecov.io/gh/udibo/http_error) | ||
[![license](https://img.shields.io/github/license/udibo/http_error)](https://github.com/udibo/http_error/blob/master/LICENSE) | ||
[![JSR](https://jsr.io/badges/@udibo/http-error)](https://jsr.io/@udibo/http-error) | ||
[![JSR Score](https://jsr.io/badges/@udibo/http-error/score)](https://jsr.io/@udibo/http-error) | ||
[![CI](https://github.com/udibo/http-error/workflows/CI/badge.svg)](https://github.com/udibo/http-error/actions?query=workflow%3ACI) | ||
[![codecov](https://codecov.io/gh/udibo/http-error/branch/main/graph/badge.svg?token=8Q7TSUFWUY)](https://codecov.io/gh/udibo/http-error) | ||
[![license](https://img.shields.io/github/license/udibo/http-error)](https://github.com/udibo/http-error/blob/master/LICENSE) | ||
|
||
An error class for HTTP requests. | ||
Utilities for creating and working with Http Errors. | ||
|
||
This module was inspired by | ||
This package was inspired by | ||
[http-errors](https://www.npmjs.com/package/http-errors) for Node.js. | ||
|
||
## Features | ||
|
||
- Framework agnostic | ||
|
||
## Installation | ||
|
||
This is an ES Module written in TypeScript and can be used in Deno projects. ES | ||
Modules are the official standard format to package JavaScript code for reuse. A | ||
JavaScript bundle is provided with each release so that it can be used in | ||
Node.js packages or web browsers. | ||
|
||
### Deno | ||
|
||
To include it in a Deno project, you can import directly from the TS files. This | ||
module is available in Deno's third part module registry but can also be | ||
imported directly from GitHub using raw content URLs. | ||
|
||
```ts | ||
// Import from Deno's third party module registry | ||
import { HttpError, isHttpError } from "https://deno.land/x/[email protected]/mod.ts"; | ||
// Import from GitHub | ||
import { HttpError, isHttpError } "https://raw.githubusercontent.com/udibo/http_error/0.7.0/mod.ts"; | ||
``` | ||
### Node.js | ||
Node.js fully supports ES Modules. | ||
If a Node.js package has the type "module" specified in its package.json file, | ||
the JavaScript bundle can be imported as a `.js` file. | ||
```js | ||
import { HttpError, isHttpError } from "./http_error_0.7.0.js"; | ||
``` | ||
|
||
The default type for Node.js packages is "commonjs". To import the bundle into a | ||
commonjs package, the file extension of the JavaScript bundle must be changed | ||
from `.js` to `.mjs`. | ||
|
||
```js | ||
import { HttpError, isHttpError } from "./http_error_0.7.0.mjs"; | ||
``` | ||
|
||
See [Node.js Documentation](https://nodejs.org/api/esm.html) for more | ||
information. | ||
|
||
### Browser | ||
|
||
Most modern browsers support ES Modules. | ||
|
||
The JavaScript bundle can be imported into ES modules. Script tags for ES | ||
modules must have the type attribute set to "module". | ||
|
||
```html | ||
<script type="module" src="main.js"></script> | ||
``` | ||
|
||
```js | ||
// main.js | ||
import { HttpError, isHttpError } from "./http_error_0.7.0.js"; | ||
``` | ||
|
||
You can also embed a module script directly into an HTML file by placing the | ||
JavaScript code within the body of the script tag. | ||
|
||
```html | ||
<script type="module"> | ||
import { HttpError, isHttpError } from "./http_error_0.7.0.js"; | ||
</script> | ||
``` | ||
|
||
See | ||
[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) | ||
for more information. | ||
|
||
## Usage | ||
|
||
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 | |
error. | ||
|
||
```ts | ||
import { HttpError } from "@udibo/http-error"; | ||
|
||
throw new HttpError(404, "file not found"); | ||
throw new HttpError(404, { message: "file not found" }); | ||
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 | |
can with regular errors. | ||
|
||
```ts | ||
import { HttpError } from "@udibo/http-error"; | ||
|
||
throw new HttpError(404, "file not found", { cause: error }); | ||
``` | ||
|
||
|
@@ -121,6 +54,8 @@ the name is not known for an HTTP error status code, it will default to | |
UnknownClientError or UnknownServerError. | ||
|
||
```ts | ||
import { HttpError } from "@udibo/http-error"; | ||
|
||
const error = new HttpError(404, "file not found"); | ||
console.log(error.toString()); // NotFoundError: file not found | ||
``` | ||
|
@@ -129,6 +64,8 @@ If you would like to extend the HttpError class, you can pass your own error | |
name in the options. | ||
|
||
```ts | ||
import { HttpError, type HttpErrorOptions } from "@udibo/http-error"; | ||
|
||
class CustomError extends HttpError { | ||
constructor( | ||
message?: string, | ||
|
@@ -144,6 +81,12 @@ signature, you can make use of the optionsFromArgs function. It will prioritize | |
the status / message arguments over status / message options. | ||
|
||
```ts | ||
import { | ||
HttpError, | ||
type HttpErrorOptions, | ||
optionsFromArgs, | ||
} from "@udibo/http-error"; | ||
|
||
class CustomError extends HttpError { | ||
constructor( | ||
status?: number, | ||
|
@@ -175,6 +118,8 @@ will also return true for Error objects that have status and expose properties | |
with matching types. | ||
|
||
```ts | ||
import { HttpError, isHttpError } from "@udibo/http-error"; | ||
|
||
let error = new Error("file not found"); | ||
console.log(isHttpError(error)); // false | ||
error = new HttpError(404, "file not found"); | ||
|
@@ -183,22 +128,35 @@ console.log(isHttpError(error)); // true | |
|
||
### ErrorResponse | ||
|
||
This object can be used to transform an HttpError into a JSON format that can be | ||
This class can be used to transform an HttpError into a JSON format that can be | ||
converted back into an HttpError. This makes it easy for the server to share | ||
HttpError's with the client. This will work with any value that is thrown. | ||
|
||
Here is an example of how an oak server could have middleware that convert an | ||
Here is an example of how an oak server could have middleware that converts an | ||
error into into a JSON format. | ||
|
||
```ts | ||
app.use(async (ctx, next) => { | ||
import { Application } from "@oak/oak/application"; | ||
import { ErrorResponse, HttpError } from "@udibo/http-error"; | ||
|
||
const app = new Application(); | ||
|
||
app.use(async (context, next) => { | ||
try { | ||
await next(); | ||
} catch (error) { | ||
const { response } = context; | ||
response.status = isHttpError(error) ? error.status : 500; | ||
response.body = new ErrorResponse(error); | ||
} | ||
}); | ||
|
||
app.use(() => { | ||
// Will throw a 500 on every request. | ||
throw new HttpError(500); | ||
}); | ||
|
||
await app.listen({ port: 80 }); | ||
``` | ||
|
||
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 | |
and the body be a JSON representation of the error. | ||
|
||
```ts | ||
import { HttpError } from "@udibo/http-error"; | ||
|
||
throw new HttpError(400, "Invalid input"); | ||
``` | ||
|
||
|
@@ -224,11 +184,6 @@ Then the response would have a 400 status and it's body would look like this: | |
} | ||
``` | ||
|
||
If the format of your error responses is different than this, you can look at | ||
the source code in [mod.ts](/mod.ts) to see how you could create your own | ||
ErrorResponse object that can be used to convert your error responses into | ||
HttpErrors. | ||
|
||
#### ErrorResponse.toError | ||
|
||
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 | |
ErrorResponse, it would be converted into an HttpError object and be thrown. | ||
|
||
```ts | ||
import { ErrorResponse, HttpError, isErrorResponse } from "@udibo/http-error"; | ||
|
||
async function getMovies() { | ||
const response = await fetch("https://example.com/movies.json"); | ||
const movies = await response.json(); | ||
|
@@ -266,6 +223,8 @@ The error that `getMovies` would throw would be equivalent to throwing the | |
following HttpError. | ||
|
||
```ts | ||
import { HttpError } from "@udibo/http-error"; | ||
|
||
new HttpError(400, "Invalid input"); | ||
``` | ||
|
||
|
@@ -281,6 +240,8 @@ thrown. But if it isn't in that format and doesn't have an error status, the | |
response body will be returned as the assumed movies. | ||
|
||
```ts | ||
import { HttpError, isErrorResponse } from "@udibo/http-error"; | ||
|
||
async function getMovies() { | ||
const response = await fetch("https://example.com/movies.json"); | ||
const movies = await response.json(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@udibo/http-error", | ||
"version": "0.8.1", | ||
"exports": { | ||
".": "./mod.ts" | ||
}, | ||
"publish": { | ||
"include": [ | ||
"LICENSE", | ||
"README.md", | ||
"**/*.ts" | ||
], | ||
"exclude": ["**/*.test.ts"] | ||
}, | ||
"imports": { | ||
"@std/assert": "jsr:@std/assert@1", | ||
"@std/http": "jsr:@std/http@0", | ||
"@std/testing": "jsr:@std/testing@0" | ||
}, | ||
"tasks": { | ||
// Checks the formatting and runs the linter. | ||
"check": "deno lint && deno fmt --check", | ||
// Gets your branch up to date with master after a squash merge. | ||
"git-rebase": "git fetch origin main && git rebase --onto origin/main HEAD" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.