forked from tanner/qotnews
fix: Improve error handling for non-JSON server responses in Submit
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
@@ -17,7 +17,18 @@ function Submit() {
|
|||||||
data.append('url', url);
|
data.append('url', url);
|
||||||
|
|
||||||
fetch('/api/submit', { method: 'POST', body: data })
|
fetch('/api/submit', { method: 'POST', body: data })
|
||||||
.then(res => res.json().then(data => ({ ok: res.ok, data })))
|
.then(res => {
|
||||||
|
if (res.ok) {
|
||||||
|
return res.json().then(data => ({ ok: true, data: data }));
|
||||||
|
}
|
||||||
|
// Handle error responses
|
||||||
|
return res.json()
|
||||||
|
.then(data => ({ ok: false, data: data })) // Our API's JSON error
|
||||||
|
.catch(() => {
|
||||||
|
// Not a JSON error from our API, so it's a server issue
|
||||||
|
throw new Error(`Server responded with ${res.status} ${res.statusText}`);
|
||||||
|
});
|
||||||
|
})
|
||||||
.then(
|
.then(
|
||||||
({ ok, data }) => {
|
({ ok, data }) => {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
|
|||||||
Reference in New Issue
Block a user