Skip to content

Commit 763a730

Browse files
authored
Merge pull request #9 from udibo/dev
Make ErrorResponse.toJSON easier to use
2 parents 4f2a23b + 0b93cf9 commit 763a730

File tree

8 files changed

+228
-135
lines changed

8 files changed

+228
-135
lines changed

.github/workflows/ci.yml

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
1-
name: CI
2-
on: [push, pull_request]
3-
jobs:
4-
build:
5-
name: test deno ${{ matrix.deno }} ${{ matrix.os }}
6-
runs-on: ${{ matrix.os }}
7-
timeout-minutes: 5
8-
strategy:
9-
matrix:
10-
os: [ubuntu-latest, windows-latest, macOS-latest]
11-
deno: [v1.x, canary]
12-
fail-fast: true
13-
steps:
14-
- name: Clone repository
15-
uses: actions/checkout@v4
16-
- name: Setup deno
17-
uses: denoland/setup-deno@main
18-
with:
19-
deno-version: ${{ matrix.deno }}
20-
- name: Check formatting
21-
if: matrix.os == 'ubuntu-latest'
22-
run: deno fmt --check
23-
- name: Check linting
24-
if: matrix.os == 'ubuntu-latest'
25-
run: deno lint
26-
- name: Run tests
27-
run: deno test --coverage=cov
28-
- name: Run tests unstable
29-
run: deno test --unstable
30-
- name: Generate lcov
31-
if: |
32-
matrix.os == 'ubuntu-latest' &&
33-
matrix.deno == 'v1.x'
34-
run: deno coverage --lcov cov > cov.lcov
35-
- name: Upload coverage
36-
if: |
37-
matrix.os == 'ubuntu-latest' &&
38-
matrix.deno == 'v1.x'
39-
uses: codecov/codecov-action@v4
40-
with:
41-
fail_ci_if_error: true
42-
files: cov.lcov
43-
env:
44-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
45-
- name: Release info
46-
if: |
47-
github.repository == 'udibo/http-error' &&
48-
matrix.os == 'ubuntu-latest' &&
49-
matrix.deno == 'v1.x' &&
50-
startsWith(github.ref, 'refs/tags/')
51-
shell: bash
52-
run: |
53-
echo "RELEASE_VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
54-
- name: Bundle
55-
if: env.RELEASE_VERSION != ''
56-
run: |
57-
mkdir -p target/release
58-
deno bundle mod.ts target/release/http-error.${RELEASE_VERSION}.js
59-
- name: Release
60-
uses: softprops/action-gh-release@v1
61-
if: env.RELEASE_VERSION != ''
62-
env:
63-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64-
with:
65-
draft: true
66-
files: |
67-
target/release/http-error.${{ env.RELEASE_VERSION }}.js
1+
name: CI
2+
on: [push, pull_request]
3+
jobs:
4+
build:
5+
name: test deno ${{ matrix.deno }} ${{ matrix.os }}
6+
runs-on: ${{ matrix.os }}
7+
timeout-minutes: 5
8+
strategy:
9+
matrix:
10+
os: [ubuntu-latest, windows-latest, macOS-latest]
11+
deno: [v2.x, canary]
12+
fail-fast: true
13+
steps:
14+
- name: Clone repository
15+
uses: actions/checkout@v4
16+
- name: Setup deno
17+
uses: denoland/setup-deno@main
18+
with:
19+
deno-version: ${{ matrix.deno }}
20+
- name: Check formatting
21+
if: matrix.os == 'ubuntu-latest'
22+
run: deno fmt --check
23+
- name: Check linting
24+
if: matrix.os == 'ubuntu-latest'
25+
run: deno lint
26+
- name: Run tests
27+
run: deno test --coverage=cov
28+
- name: Run tests unstable
29+
run: deno test --unstable
30+
- name: Generate lcov
31+
if: |
32+
matrix.os == 'ubuntu-latest' &&
33+
matrix.deno == 'v2.x'
34+
run: deno coverage --lcov cov > cov.lcov
35+
- name: Upload coverage
36+
if: |
37+
matrix.os == 'ubuntu-latest' &&
38+
matrix.deno == 'v2.x'
39+
uses: codecov/codecov-action@v4
40+
with:
41+
fail_ci_if_error: true
42+
files: cov.lcov
43+
env:
44+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
45+
- name: Release info
46+
if: |
47+
github.repository == 'udibo/http-error' &&
48+
matrix.os == 'ubuntu-latest' &&
49+
matrix.deno == 'v2.x' &&
50+
startsWith(github.ref, 'refs/tags/')
51+
shell: bash
52+
run: |
53+
echo "RELEASE_VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
54+
- name: Bundle
55+
if: env.RELEASE_VERSION != ''
56+
run: |
57+
mkdir -p target/release
58+
deno bundle mod.ts target/release/http-error.${RELEASE_VERSION}.js
59+
- name: Release
60+
uses: softprops/action-gh-release@v1
61+
if: env.RELEASE_VERSION != ''
62+
env:
63+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64+
with:
65+
draft: true
66+
files: |
67+
target/release/http-error.${{ env.RELEASE_VERSION }}.js

.vscode/settings.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
{
22
"deno.enable": true,
33
"deno.lint": true,
4-
"deno.unstable": false,
5-
"deno.config": "./deno.jsonc",
6-
"files.associations": {
7-
"*.css": "tailwindcss"
8-
},
4+
"deno.config": "./deno.json",
95
"editor.formatOnSave": true,
106
"editor.defaultFormatter": "denoland.vscode-deno",
117
"editor.quickSuggestions": {

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ ErrorResponse, it would be converted into an HttpError object and be thrown.
195195
```ts
196196
import { ErrorResponse, HttpError, isErrorResponse } from "@udibo/http-error";
197197

198+
async function getMovies() {
199+
const response = await fetch("https://example.com/movies.json");
200+
if (!response.ok) throw new ErrorResponse.toError(movies);
201+
return await response.json();
202+
}
203+
```
204+
205+
The next example is similar, but uses the response JSON instead of the response.
206+
The advantage of the first approach in the previous example is that it will
207+
produce an HttpError based on the status code in the case that the response
208+
doesn't have valid JSON.
209+
210+
```ts
211+
import { ErrorResponse, HttpError, isErrorResponse } from "@udibo/http-error";
212+
198213
async function getMovies() {
199214
const response = await fetch("https://example.com/movies.json");
200215
const movies = await response.json();

deno.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@udibo/http-error",
3+
"version": "0.9.0",
4+
"exports": {
5+
".": "./mod.ts"
6+
},
7+
"publish": {
8+
"include": [
9+
"LICENSE",
10+
"README.md",
11+
"**/*.ts"
12+
],
13+
"exclude": ["**/*.test.ts"]
14+
},
15+
"imports": {
16+
"@std/assert": "jsr:@std/assert@1",
17+
"@std/http": "jsr:@std/http@1",
18+
"@std/testing": "jsr:@std/testing@1"
19+
},
20+
"tasks": {
21+
"check": {
22+
"description": "Checks the formatting and runs the linter.",
23+
"command": "deno lint && deno fmt --check"
24+
},
25+
"git-rebase": {
26+
"description": "Gets your branch up to date with master after a squash merge.",
27+
"command": "git fetch origin main && git rebase --onto origin/main HEAD"
28+
}
29+
}
30+
}

deno.jsonc

Lines changed: 0 additions & 26 deletions
This file was deleted.

deno.lock

Lines changed: 28 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mod.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,45 @@ it(ErrorResponseTests, "from external HttpError", () => {
584584

585585
const toErrorTests = describe(ErrorResponseTests, "toError");
586586

587+
it(toErrorTests, "with text response", async () => {
588+
const response = new Response("oops", { status: 400 });
589+
const error = await ErrorResponse.toError(response);
590+
assertEquals(error.toString(), "BadRequestError");
591+
assertEquals(error.name, "BadRequestError");
592+
assertEquals(error.message, "");
593+
assertEquals(error.status, 400);
594+
assertEquals(error.expose, true);
595+
assertEquals(error.cause, undefined);
596+
});
597+
598+
it(toErrorTests, "with error response", async () => {
599+
const response = new Response(
600+
JSON.stringify({ error: { status: 400, message: "oops", custom: "data" } }),
601+
{ status: 400 },
602+
);
603+
const error = await ErrorResponse.toError(response);
604+
assertEquals(error.toString(), "BadRequestError: oops");
605+
assertEquals(error.name, "BadRequestError");
606+
assertEquals(error.message, "oops");
607+
assertEquals(error.status, 400);
608+
assertEquals(error.expose, true);
609+
assertEquals(error.cause, undefined);
610+
assertEquals(error.data, { custom: "data" });
611+
});
612+
613+
it(toErrorTests, "with json response", async () => {
614+
const response = new Response(JSON.stringify({ message: "oops" }), {
615+
status: 400,
616+
});
617+
const error = await ErrorResponse.toError(response);
618+
assertEquals(error.toString(), "BadRequestError: oops");
619+
assertEquals(error.name, "BadRequestError");
620+
assertEquals(error.message, "oops");
621+
assertEquals(error.status, 400);
622+
assertEquals(error.expose, true);
623+
assertEquals(error.cause, undefined);
624+
});
625+
587626
it(toErrorTests, "with internal ErrorResponse", () => {
588627
const errorResponse = new ErrorResponse(new HttpError("oops"));
589628
const error = ErrorResponse.toError(errorResponse);

0 commit comments

Comments
 (0)