mirror of
https://github.com/screentinker/screentinker.git
synced 2026-08-17 07:34:15 -06:00
fix: handle empty API responses in frontend and return JSON on server crash
This commit is contained in:
parent
16fa1701d8
commit
f5ed7ae491
|
|
@ -99,9 +99,15 @@ export const api = {
|
||||||
}
|
}
|
||||||
xhr.onload = () => {
|
xhr.onload = () => {
|
||||||
if (xhr.status >= 200 && xhr.status < 300) {
|
if (xhr.status >= 200 && xhr.status < 300) {
|
||||||
|
try {
|
||||||
resolve(JSON.parse(xhr.responseText));
|
resolve(JSON.parse(xhr.responseText));
|
||||||
|
} catch {
|
||||||
|
resolve({ ok: true }); // handle empty/non-JSON 2xx response
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
reject(new Error('Upload failed'));
|
let msg = 'Upload failed';
|
||||||
|
try { msg = JSON.parse(xhr.responseText).error || msg; } catch {}
|
||||||
|
reject(new Error(msg));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
xhr.onerror = () => reject(new Error('Upload failed'));
|
xhr.onerror = () => reject(new Error('Upload failed'));
|
||||||
|
|
|
||||||
|
|
@ -965,6 +965,15 @@ app.get('/download/apk', (req, res) => {
|
||||||
res.sendFile(apk.path, (err) => { if (err) release(); });
|
res.sendFile(apk.path, (err) => { if (err) release(); });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Global API error handler — prevents Express from sending HTML errors on API routes
|
||||||
|
app.use((err, req, res, next) => {
|
||||||
|
console.error('[Unhandled Express Error]', err);
|
||||||
|
if (req.path.startsWith('/api/')) {
|
||||||
|
return res.status(500).json({ error: 'Internal server error' });
|
||||||
|
}
|
||||||
|
next(err);
|
||||||
|
});
|
||||||
|
|
||||||
// SPA fallback for app routes. Unmatched /api/ paths return 404 so misrouted
|
// SPA fallback for app routes. Unmatched /api/ paths return 404 so misrouted
|
||||||
// clients fail fast instead of hanging until Cloudflare's 15s upstream timeout.
|
// clients fail fast instead of hanging until Cloudflare's 15s upstream timeout.
|
||||||
app.get('*', (req, res) => {
|
app.get('*', (req, res) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue