Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ErrorResponse #69

Merged
merged 1 commit into from
Jul 2, 2023
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Udibo React App

[![release](https://img.shields.io/badge/release-0.14.0-success)](https://github.com/udibo/react_app/releases/tag/0.14.0)
[![deno doc](https://doc.deno.land/badge.svg)](https://deno.land/x/udibo_react_app@0.14.0)
[![release](https://img.shields.io/badge/release-0.15.0-success)](https://github.com/udibo/react_app/releases/tag/0.15.0)
[![deno doc](https://doc.deno.land/badge.svg)](https://deno.land/x/udibo_react_app@0.15.0)
[![CI/CD](https://github.com/udibo/react_app/actions/workflows/main.yml/badge.svg)](https://github.com/udibo/react_app/actions/workflows/main.yml)
[![codecov](https://codecov.io/gh/udibo/react_app/branch/main/graph/badge.svg?token=G5XCR01X8E)](https://codecov.io/gh/udibo/react_app)
[![license](https://img.shields.io/github/license/udibo/react_app)](https://github.com/udibo/react_app/blob/main/LICENSE)
Expand Down Expand Up @@ -31,13 +31,13 @@ Apps are created using [React Router](https://reactrouter.com),

This module has 2 entry points.

- [mod.tsx](https://deno.land/x/udibo_react_app@0.14.0/mod.tsx): For use in code
- [mod.tsx](https://deno.land/x/udibo_react_app@0.15.0/mod.tsx): For use in code
that will be used both on the server and in the browser.
- [server.tsx](https://deno.land/x/udibo_react_app@0.14.0/server.tsx): For use
- [server.tsx](https://deno.land/x/udibo_react_app@0.15.0/server.tsx): For use
in code that will only be used on the server.

You can look at the [examples](#examples) and
[deno docs](https://deno.land/x/udibo_react_app@0.14.0) to learn more about
[deno docs](https://deno.land/x/udibo_react_app@0.15.0) to learn more about
usage.

### Examples
Expand Down
3 changes: 3 additions & 0 deletions deno.lock
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@
"https://deno.land/[email protected]/fs/_util.ts": "fbf57dcdc9f7bc8128d60301eece608246971a7836a3bb1e78da75314f08b978",
"https://deno.land/[email protected]/fs/ensure_dir.ts": "dc64c4c75c64721d4e3fb681f1382f803ff3d2868f08563ff923fdd20d071c40",
"https://deno.land/[email protected]/fs/walk.ts": "920be35a7376db6c0b5b1caf1486fb962925e38c9825f90367f8f26b5e5d0897",
"https://deno.land/[email protected]/http/http_status.ts": "8a7bcfe3ac025199ad804075385e57f63d055b2aed539d943ccc277616d6f932",
"https://deno.land/[email protected]/path/_constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0",
"https://deno.land/[email protected]/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b",
"https://deno.land/[email protected]/path/_util.ts": "d7abb1e0dea065f427b89156e28cdeb32b045870acdf865833ba808a73b576d0",
Expand All @@ -359,6 +360,8 @@
"https://deno.land/x/[email protected]/src/shared.ts": "b64749cd8c0f6252a11498bd8758ef1220003e46b2c9b68e16da63fd7e92b13a",
"https://deno.land/x/[email protected]/deps.ts": "f57a407345cabe6b759b9c678845ed4cbc0808d64df1f92b68dc5c5b308564fb",
"https://deno.land/x/[email protected]/mod.ts": "ca622eced625a6b68cd778fa3a0db727550fb9e7f14fedadae03a1f88f0b6030",
"https://deno.land/x/[email protected]/deps.ts": "4c5201aab868b3d91e289c095e387d680689bb412e75aaf0ae37cff597838299",
"https://deno.land/x/[email protected]/mod.ts": "cbf722e350b52ff03ae0867c19c80f34c485e50367fa42c11f30a759db9f5ba4",
"https://deno.land/x/[email protected]/_util.ts": "ada9a9618b537e6c0316c048a898352396c882b9f2de38aba18fd3f2950ede89",
"https://deno.land/x/[email protected]/mod.ts": "ae3d1cd7eabd18c01a4960d57db471126b020f23b37ef14e1359bbb949227ade",
"https://deno.land/x/[email protected]/application.ts": "4819f2820617f04117ccafe5bb4daae004c3cc0100eb321afd9a35ba558794d9",
Expand Down
10 changes: 8 additions & 2 deletions error.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { HttpError, HttpErrorOptions, isHttpError } from "x/http_error/mod.ts";
import {
ErrorResponse,
HttpError,
HttpErrorOptions,
isErrorResponse,
isHttpError,
} from "x/http_error/mod.ts";
import {
ComponentType,
createContext,
Expand All @@ -19,7 +25,7 @@ import type {

import { isDevelopment } from "./env.ts";

export { HttpError, isHttpError };
export { ErrorResponse, HttpError, isErrorResponse, isHttpError };
export type { ErrorBoundaryProps, FallbackProps, HttpErrorOptions };

/**
Expand Down
3 changes: 3 additions & 0 deletions example/deno.lock
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
"https://deno.land/[email protected]/types.d.ts": "dbaeb2c4d7c526db9828fc8df89d8aecf53b9ced72e0c4568f97ddd8cda616a4",
"https://deno.land/[email protected]/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462",
"https://deno.land/[email protected]/_util/os.ts": "d932f56d41e4f6a6093d56044e29ce637f8dcc43c5a90af43504a889cf1775e3",
"https://deno.land/[email protected]/http/http_status.ts": "8a7bcfe3ac025199ad804075385e57f63d055b2aed539d943ccc277616d6f932",
"https://deno.land/[email protected]/path/_constants.ts": "e49961f6f4f48039c0dfed3c3f93e963ca3d92791c9d478ac5b43183413136e0",
"https://deno.land/[email protected]/path/_interface.ts": "6471159dfbbc357e03882c2266d21ef9afdb1e4aa771b0545e90db58a0ba314b",
"https://deno.land/[email protected]/path/_util.ts": "d7abb1e0dea065f427b89156e28cdeb32b045870acdf865833ba808a73b576d0",
Expand All @@ -226,6 +227,8 @@
"https://deno.land/[email protected]/path/win32.ts": "d186344e5583bcbf8b18af416d13d82b35a317116e6460a5a3953508c3de5bba",
"https://deno.land/x/[email protected]/deps.ts": "f57a407345cabe6b759b9c678845ed4cbc0808d64df1f92b68dc5c5b308564fb",
"https://deno.land/x/[email protected]/mod.ts": "ca622eced625a6b68cd778fa3a0db727550fb9e7f14fedadae03a1f88f0b6030",
"https://deno.land/x/[email protected]/deps.ts": "4c5201aab868b3d91e289c095e387d680689bb412e75aaf0ae37cff597838299",
"https://deno.land/x/[email protected]/mod.ts": "cbf722e350b52ff03ae0867c19c80f34c485e50367fa42c11f30a759db9f5ba4",
"https://deno.land/x/[email protected]/application.ts": "3028d3f6fa5ee743de013881550d054372c11d83c45099c2d794033786d27008",
"https://deno.land/x/[email protected]/body.ts": "1899761b97fc9d776f3710b2637fb047ba29b968609afc6c0e5219b1108e703c",
"https://deno.land/x/[email protected]/buf_reader.ts": "26640736541598dbd9f2b84a9d0595756afff03c9ca55b66eef1911f7798b56d",
Expand Down
2 changes: 1 addition & 1 deletion example/import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"x/oak/": "https://deno.land/x/[email protected]/",
"x/esbuild/": "https://deno.land/x/[email protected]/",
"x/esbuild_deno_loader/": "https://deno.land/x/[email protected]/",
"x/http_error/": "https://deno.land/x/http_error@0.5.0/",
"x/http_error/": "https://deno.land/x/http_error@0.6.0/",
"npm/react": "https://esm.sh/[email protected]?target=deno&pin=v126",
"npm/react/jsx-runtime": "https://esm.sh/[email protected]/jsx-runtime?target=deno&pin=v126",
"npm/react/jsx-dev-runtime": "https://esm.sh/[email protected]/jsx-dev-runtime?target=deno&pin=v126",
Expand Down
16 changes: 7 additions & 9 deletions example/services/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {

import { AppContext } from "../context.ts";
import { Post } from "../models/posts.ts";
import { ErrorResponse, isErrorResponse } from "../../error.tsx";

const parseResponse = async (response: Response) => {
let data;
Expand All @@ -15,7 +16,10 @@ const parseResponse = async (response: Response) => {
} catch (e) {
throw new HttpError(response.status, "Invalid response");
}
if (response.status !== 200) throw data;
if (isErrorResponse(data)) throw ErrorResponse.toError(data);
if (response.status >= 400) {
throw new HttpError(response.status, "Invalid response");
}
return data;
};

Expand All @@ -39,10 +43,7 @@ export function getPosts() {
})
.catch((error: unknown) => {
setPosts(null);
const options = error && typeof error === "object"
? error as HttpErrorOptions
: {};
setError(new HttpError(options));
setError(HttpError.from(error));
});
}
}, []);
Expand All @@ -68,10 +69,7 @@ export function getPost(id: number) {
})
.catch((error: unknown) => {
setPost(null);
const options = error && typeof error === "object"
? error as HttpErrorOptions
: {};
setError(new HttpError(options));
setError(HttpError.from(error));
});
}
}, []);
Expand Down
2 changes: 1 addition & 1 deletion import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"x/oak/": "https://deno.land/x/[email protected]/",
"x/esbuild/": "https://deno.land/x/[email protected]/",
"x/esbuild_deno_loader/": "https://deno.land/x/[email protected]/",
"x/http_error/": "https://deno.land/x/http_error@0.5.0/",
"x/http_error/": "https://deno.land/x/http_error@0.6.0/",
"npm/react": "https://esm.sh/[email protected]?target=deno&pin=v126",
"npm/react/jsx-runtime": "https://esm.sh/[email protected]/jsx-runtime?target=deno&pin=v126",
"npm/react/jsx-dev-runtime": "https://esm.sh/[email protected]/jsx-dev-runtime?target=deno&pin=v126",
Expand Down
2 changes: 2 additions & 0 deletions mod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ import {
export {
AppErrorBoundary,
DefaultErrorFallback,
ErrorResponse,
HttpError,
isErrorResponse,
isHttpError,
NotFound,
useAutoReset,
Expand Down
9 changes: 7 additions & 2 deletions server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import {
} from "npm/react-router-dom";
import serialize from "npm/serialize-javascript";

import { AppErrorContext, HttpError, isHttpError } from "./error.tsx";
import {
AppErrorContext,
ErrorResponse,
HttpError,
isHttpError,
} from "./error.tsx";
import {
AppEnvironment,
createAppContext,
Expand Down Expand Up @@ -576,7 +581,7 @@ export function generateRouter(
response.status = error.status;
const extname = path.extname(request.url.pathname);
if (error.status !== 404 || extname === "") {
response.body = HttpError.json(error);
response.body = new ErrorResponse(error);
}
}
});
Expand Down