Skip to content

Commit f43feb0

Browse files
authored
Merge pull request #49 from dockersamples/add-simple-client-error-handling
Add small error handling to provide reminder to start backend
2 parents 1993ada + 577628e commit f43feb0

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

dev/webapp/src/App.jsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ import "./App.css";
33
import { ProductRow } from "./ProductRow";
44

55
function App() {
6-
const [lastRequest, setLastRequest] = useState(null);
76
const [catalog, setCatalog] = useState(null);
7+
const [errorOccurred, setErrorOccurred] = useState(false);
88

99
const fetchCatalog = useCallback(() => {
10+
setErrorOccurred(false);
11+
1012
fetch("/api/products")
1113
.then((response) => response.json())
1214
.then((data) => {
1315
setCatalog(data);
14-
setLastRequest({
15-
method: "GET",
16-
url: "/api/products",
17-
status: 200,
18-
response: data,
19-
});
16+
})
17+
.catch((e) => {
18+
setErrorOccurred(e);
2019
});
21-
}, []);
20+
}, [setErrorOccurred, setCatalog]);
2221

2322
const createProduct = useCallback(() => {
2423
const body = {
@@ -77,7 +76,16 @@ function App() {
7776
)}
7877
</>
7978
) : (
80-
<p>Loading catalog...</p>
79+
<>
80+
{errorOccurred ? (
81+
<p>
82+
An error occurred while fetching the catalog. Is the backend
83+
running?
84+
</p>
85+
) : (
86+
<p>Loading catalog...</p>
87+
)}
88+
</>
8189
)}
8290
</>
8391
);

0 commit comments

Comments
 (0)