diff --git a/webclient/src/Comments.js b/webclient/src/Comments.js index 0da6c6b..f3060ad 100644 --- a/webclient/src/Comments.js +++ b/webclient/src/Comments.js @@ -16,7 +16,7 @@ function Comments({ cache }) { if (id in cache) console.log('cache hit'); const [story, setStory] = useState(cache[id] || false); - const [error, setError] = useState(false); + const [error, setError] = useState(''); const [collapsed, setCollapsed] = useState([]); const [expanded, setExpanded] = useState([]); @@ -31,7 +31,12 @@ function Comments({ cache }) { ); fetch('/api/' + id) - .then(res => res.json()) + .then(res => { + if (!res.ok) { + throw new Error(`Server responded with ${res.status} ${res.statusText}`); + } + return res.json(); + }) .then( (result) => { setStory(result.story); @@ -47,7 +52,8 @@ function Comments({ cache }) { } }, (error) => { - setError(true); + const errorMessage = `Failed to fetch comments (ID: ${id}). Your connection may be down or the server might be experiencing issues. ${error.toString()}.`; + setError(errorMessage); } ); }, [id]); @@ -97,7 +103,13 @@ function Comments({ cache }) { return (
- {error &&

Connection error?

} + {error && +
+ Connection error? Click to expand. +

{error}

+ {story &&

Loaded comments from cache.

} +
+ } {story ?