Skip to content

Commit ca3bb1b

Browse files
authored
Use ErrorResponse (#69)
1 parent c9b8b1d commit ca3bb1b

File tree

9 files changed

+37
-20
lines changed

9 files changed

+37
-20
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Udibo React App
22

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

3232
This module has 2 entry points.
3333

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

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

4343
### Examples

deno.lock

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

error.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { HttpError, HttpErrorOptions, isHttpError } from "x/http_error/mod.ts";
1+
import {
2+
ErrorResponse,
3+
HttpError,
4+
HttpErrorOptions,
5+
isErrorResponse,
6+
isHttpError,
7+
} from "x/http_error/mod.ts";
28
import {
39
ComponentType,
410
createContext,
@@ -19,7 +25,7 @@ import type {
1925

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

22-
export { HttpError, isHttpError };
28+
export { ErrorResponse, HttpError, isErrorResponse, isHttpError };
2329
export type { ErrorBoundaryProps, FallbackProps, HttpErrorOptions };
2430

2531
/**

example/deno.lock

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

example/import_map.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"x/oak/": "https://deno.land/x/oak@v12.5.0/",
66
"x/esbuild/": "https://deno.land/x/esbuild@v0.15.10/",
77
"x/esbuild_deno_loader/": "https://deno.land/x/esbuild_deno_loader@0.6.0/",
8-
"x/http_error/": "https://deno.land/x/http_error@0.5.0/",
8+
"x/http_error/": "https://deno.land/x/http_error@0.6.0/",
99
"npm/react": "https://esm.sh/react@18.2.0?target=deno&pin=v126",
1010
"npm/react/jsx-runtime": "https://esm.sh/react@18.2.0/jsx-runtime?target=deno&pin=v126",
1111
"npm/react/jsx-dev-runtime": "https://esm.sh/react@18.2.0/jsx-dev-runtime?target=deno&pin=v126",

example/services/posts.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77

88
import { AppContext } from "../context.ts";
99
import { Post } from "../models/posts.ts";
10+
import { ErrorResponse, isErrorResponse } from "../../error.tsx";
1011

1112
const parseResponse = async (response: Response) => {
1213
let data;
@@ -15,7 +16,10 @@ const parseResponse = async (response: Response) => {
1516
} catch (e) {
1617
throw new HttpError(response.status, "Invalid response");
1718
}
18-
if (response.status !== 200) throw data;
19+
if (isErrorResponse(data)) throw ErrorResponse.toError(data);
20+
if (response.status >= 400) {
21+
throw new HttpError(response.status, "Invalid response");
22+
}
1923
return data;
2024
};
2125

@@ -39,10 +43,7 @@ export function getPosts() {
3943
})
4044
.catch((error: unknown) => {
4145
setPosts(null);
42-
const options = error && typeof error === "object"
43-
? error as HttpErrorOptions
44-
: {};
45-
setError(new HttpError(options));
46+
setError(HttpError.from(error));
4647
});
4748
}
4849
}, []);
@@ -68,10 +69,7 @@ export function getPost(id: number) {
6869
})
6970
.catch((error: unknown) => {
7071
setPost(null);
71-
const options = error && typeof error === "object"
72-
? error as HttpErrorOptions
73-
: {};
74-
setError(new HttpError(options));
72+
setError(HttpError.from(error));
7573
});
7674
}
7775
}, []);

import_map.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"x/oak/": "https://deno.land/x/oak@v12.5.0/",
66
"x/esbuild/": "https://deno.land/x/esbuild@v0.15.10/",
77
"x/esbuild_deno_loader/": "https://deno.land/x/esbuild_deno_loader@0.6.0/",
8-
"x/http_error/": "https://deno.land/x/http_error@0.5.0/",
8+
"x/http_error/": "https://deno.land/x/http_error@0.6.0/",
99
"npm/react": "https://esm.sh/react@18.2.0?target=deno&pin=v126",
1010
"npm/react/jsx-runtime": "https://esm.sh/react@18.2.0/jsx-runtime?target=deno&pin=v126",
1111
"npm/react/jsx-dev-runtime": "https://esm.sh/react@18.2.0/jsx-dev-runtime?target=deno&pin=v126",

mod.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ import {
3636
export {
3737
AppErrorBoundary,
3838
DefaultErrorFallback,
39+
ErrorResponse,
3940
HttpError,
41+
isErrorResponse,
4042
isHttpError,
4143
NotFound,
4244
useAutoReset,

server.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ import {
2424
} from "npm/react-router-dom";
2525
import serialize from "npm/serialize-javascript";
2626

27-
import { AppErrorContext, HttpError, isHttpError } from "./error.tsx";
27+
import {
28+
AppErrorContext,
29+
ErrorResponse,
30+
HttpError,
31+
isHttpError,
32+
} from "./error.tsx";
2833
import {
2934
AppEnvironment,
3035
createAppContext,
@@ -576,7 +581,7 @@ export function generateRouter(
576581
response.status = error.status;
577582
const extname = path.extname(request.url.pathname);
578583
if (error.status !== 404 || extname === "") {
579-
response.body = HttpError.json(error);
584+
response.body = new ErrorResponse(error);
580585
}
581586
}
582587
});

0 commit comments

Comments
 (0)