Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 67 additions & 67 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
name: CI
on: [push, pull_request]
jobs:
build:
name: test deno ${{ matrix.deno }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 5
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
deno: [v1.x, canary]
fail-fast: true
steps:
- name: Clone repository
uses: actions/checkout@v4
- name: Setup deno
uses: denoland/setup-deno@main
with:
deno-version: ${{ matrix.deno }}
- name: Check formatting
if: matrix.os == 'ubuntu-latest'
run: deno fmt --check
- name: Check linting
if: matrix.os == 'ubuntu-latest'
run: deno lint
- name: Run tests
run: deno test --coverage=cov
- name: Run tests unstable
run: deno test --unstable
- name: Generate lcov
if: |
matrix.os == 'ubuntu-latest' &&
matrix.deno == 'v1.x'
run: deno coverage --lcov cov > cov.lcov
- name: Upload coverage
if: |
matrix.os == 'ubuntu-latest' &&
matrix.deno == 'v1.x'
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
files: cov.lcov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Release info
if: |
github.repository == 'udibo/http-error' &&
matrix.os == 'ubuntu-latest' &&
matrix.deno == 'v1.x' &&
startsWith(github.ref, 'refs/tags/')
shell: bash
run: |
echo "RELEASE_VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
- name: Bundle
if: env.RELEASE_VERSION != ''
run: |
mkdir -p target/release
deno bundle mod.ts target/release/http-error.${RELEASE_VERSION}.js
- name: Release
uses: softprops/action-gh-release@v1
if: env.RELEASE_VERSION != ''
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
draft: true
files: |
target/release/http-error.${{ env.RELEASE_VERSION }}.js
name: CI
on: [push, pull_request]
jobs:
build:
name: test deno ${{ matrix.deno }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 5
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
deno: [v2.x, canary]
fail-fast: true
steps:
- name: Clone repository
uses: actions/checkout@v4
- name: Setup deno
uses: denoland/setup-deno@main
with:
deno-version: ${{ matrix.deno }}
- name: Check formatting
if: matrix.os == 'ubuntu-latest'
run: deno fmt --check
- name: Check linting
if: matrix.os == 'ubuntu-latest'
run: deno lint
- name: Run tests
run: deno test --coverage=cov
- name: Run tests unstable
run: deno test --unstable
- name: Generate lcov
if: |
matrix.os == 'ubuntu-latest' &&
matrix.deno == 'v2.x'
run: deno coverage --lcov cov > cov.lcov
- name: Upload coverage
if: |
matrix.os == 'ubuntu-latest' &&
matrix.deno == 'v2.x'
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
files: cov.lcov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Release info
if: |
github.repository == 'udibo/http-error' &&
matrix.os == 'ubuntu-latest' &&
matrix.deno == 'v2.x' &&
startsWith(github.ref, 'refs/tags/')
shell: bash
run: |
echo "RELEASE_VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
- name: Bundle
if: env.RELEASE_VERSION != ''
run: |
mkdir -p target/release
deno bundle mod.ts target/release/http-error.${RELEASE_VERSION}.js
- name: Release
uses: softprops/action-gh-release@v1
if: env.RELEASE_VERSION != ''
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
draft: true
files: |
target/release/http-error.${{ env.RELEASE_VERSION }}.js
6 changes: 1 addition & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": false,
"deno.config": "./deno.jsonc",
"files.associations": {
"*.css": "tailwindcss"
},
"deno.config": "./deno.json",
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.quickSuggestions": {
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,21 @@ 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");
if (!response.ok) throw new ErrorResponse.toError(movies);
return await response.json();
}
```

The next example is similar, but uses the response JSON instead of the response.
The advantage of the first approach in the previous example is that it will
produce an HttpError based on the status code in the case that the response
doesn't have valid JSON.

```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();
Expand Down
30 changes: 30 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@udibo/http-error",
"version": "0.9.0",
"exports": {
".": "./mod.ts"
},
"publish": {
"include": [
"LICENSE",
"README.md",
"**/*.ts"
],
"exclude": ["**/*.test.ts"]
},
"imports": {
"@std/assert": "jsr:@std/assert@1",
"@std/http": "jsr:@std/http@1",
"@std/testing": "jsr:@std/testing@1"
},
"tasks": {
"check": {
"description": "Checks the formatting and runs the linter.",
"command": "deno lint && deno fmt --check"
},
"git-rebase": {
"description": "Gets your branch up to date with master after a squash merge.",
"command": "git fetch origin main && git rebase --onto origin/main HEAD"
}
}
}
26 changes: 0 additions & 26 deletions deno.jsonc

This file was deleted.

61 changes: 28 additions & 33 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,45 @@ it(ErrorResponseTests, "from external HttpError", () => {

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

it(toErrorTests, "with text response", async () => {
const response = new Response("oops", { status: 400 });
const error = await ErrorResponse.toError(response);
assertEquals(error.toString(), "BadRequestError");
assertEquals(error.name, "BadRequestError");
assertEquals(error.message, "");
assertEquals(error.status, 400);
assertEquals(error.expose, true);
assertEquals(error.cause, undefined);
});

it(toErrorTests, "with error response", async () => {
const response = new Response(
JSON.stringify({ error: { status: 400, message: "oops", custom: "data" } }),
{ status: 400 },
);
const error = await ErrorResponse.toError(response);
assertEquals(error.toString(), "BadRequestError: oops");
assertEquals(error.name, "BadRequestError");
assertEquals(error.message, "oops");
assertEquals(error.status, 400);
assertEquals(error.expose, true);
assertEquals(error.cause, undefined);
assertEquals(error.data, { custom: "data" });
});

it(toErrorTests, "with json response", async () => {
const response = new Response(JSON.stringify({ message: "oops" }), {
status: 400,
});
const error = await ErrorResponse.toError(response);
assertEquals(error.toString(), "BadRequestError: oops");
assertEquals(error.name, "BadRequestError");
assertEquals(error.message, "oops");
assertEquals(error.status, 400);
assertEquals(error.expose, true);
assertEquals(error.cause, undefined);
});

it(toErrorTests, "with internal ErrorResponse", () => {
const errorResponse = new ErrorResponse(new HttpError("oops"));
const error = ErrorResponse.toError(errorResponse);
Expand Down
Loading