Skip to content

Commit ac7e4af

Browse files
committed
error handling
1 parent 3d8ba36 commit ac7e4af

File tree

4 files changed

+84
-12
lines changed

4 files changed

+84
-12
lines changed

src/components/error.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import React from "react"
22

3-
const showError = async (
3+
const showHTTPError = async (
44
setter: (value: React.SetStateAction<React.ReactNode>) => void,
55
response: Response
66
) => {
77
const data =
88
response.status < 500
99
? JSON.stringify(await response.json(), null, 2)
1010
: await response.text()
11+
return await showError(setter, response.status < 500, data)
12+
}
13+
14+
const showError = async (
15+
setter: (value: React.SetStateAction<React.ReactNode>) => void,
16+
warning: boolean,
17+
message: string
18+
) => {
19+
console.log(message)
1120
setter(() => (
1221
<div>
1322
<div
14-
className={
15-
response.status < 500 ? "alert alert-warning" : "alert alert-danger"
16-
}
23+
className={warning ? "alert alert-warning" : "alert alert-danger"}
1724
role="alert"
1825
>
19-
{response.status < 500 ? (
26+
{warning ? (
2027
<div>
21-
<pre className="error-message">{data}</pre>
28+
<pre className="warning-message">{message}</pre>
2229
</div>
2330
) : (
2431
<p>
@@ -38,4 +45,4 @@ const showError = async (
3845
))
3946
}
4047

41-
export default showError
48+
export { showHTTPError, showError }

src/pages/apps/bsky-follow-suggestions.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useSearchParams, BrowserRouter } from "react-router-dom"
33
import Layout from "../../components/layout"
44
import Closer from "../../components/closer"
55
import { ProfileViewDetailed } from "@atproto/api/dist/client/types/app/bsky/actor/defs"
6-
import showError from "../../components/error"
6+
import { showHTTPError } from "../../components/error"
77

88
const requestFrequency = 100
99

@@ -72,7 +72,7 @@ const Bsky = () => {
7272
)
7373
if (!response.ok) {
7474
clearApplicationState()
75-
showError(setError, response)
75+
showHTTPError(setError, response)
7676
return
7777
}
7878
const data: string[] = await response.json()
@@ -108,7 +108,7 @@ const Bsky = () => {
108108
)
109109
if (!response.ok) {
110110
clearApplicationState()
111-
showError(setError, response)
111+
showHTTPError(setError, response)
112112
return
113113
}
114114
const data: string[] = await response.json()
@@ -224,7 +224,7 @@ const Bsky = () => {
224224
)
225225
if (!response.ok) {
226226
clearApplicationState()
227-
showError(setError, response)
227+
showHTTPError(setError, response)
228228
return
229229
}
230230
const data: { [key: string]: ProfileViewDetailed } = await response.json()

src/pages/apps/testing.tsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React, { useEffect, useRef, useState } from "react"
2+
import { useSearchParams, BrowserRouter } from "react-router-dom"
3+
import Layout from "../../components/layout"
4+
import Closer from "../../components/closer"
5+
import { showError } from "../../components/error"
6+
7+
const Bsky = () => {
8+
// START: GENERIC STATE
9+
// This kind of state is likely to be used in most applications.
10+
const handleRef = useRef<HTMLInputElement | null>(null)
11+
const [searchParams, setSearchParams] =
12+
typeof window !== "undefined"
13+
? useSearchParams()
14+
: [new URLSearchParams(), () => {}]
15+
const [error, setError] = useState<React.ReactNode>()
16+
// END: GENERIC STATE
17+
18+
// Get the "handle" query parameter.
19+
// This should eventually be replaced with a user input field.
20+
// "I / Me" from this point are the perspective of the user, eg. the handle.
21+
// "You" is the server / website.
22+
const myHandle = searchParams.get("handle")
23+
24+
return (
25+
<Layout>
26+
<section className="post-body">
27+
<div className="post-header">
28+
<h2>
29+
<p>Testing</p>
30+
</h2>
31+
</div>
32+
<div className="post-content">
33+
<button
34+
className="btn btn-warning"
35+
type="button"
36+
onClick={() => {
37+
showError(setError, true, "This is a warning message!")
38+
}}
39+
>
40+
Warn!
41+
</button>
42+
<button
43+
className="btn btn-danger"
44+
type="button"
45+
onClick={() => {
46+
showError(setError, false, "This is an error message!")
47+
}}
48+
>
49+
Error!
50+
</button>
51+
</div>
52+
<div className="post-content">{error ? error : null}</div>
53+
<Closer />
54+
</section>
55+
</Layout>
56+
)
57+
}
58+
59+
export default () => {
60+
return typeof window !== "undefined" ? (
61+
<BrowserRouter>
62+
<Bsky />
63+
</BrowserRouter>
64+
) : null
65+
}

src/sass/post.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ p > code {
163163
}
164164

165165
// Styling of triple ```
166-
pre:not(.error-message) {
166+
pre:not(.warning-message) {
167167
background-color: vars.$dark-purple;
168168
background-image: url("../images/rows-triangles.svg");
169169
background-repeat: repeat-x;

0 commit comments

Comments
 (0)