fix: handle empty API responses in frontend and return JSON on server crash

This commit is contained in:
xadyz1 2026-07-26 19:53:23 +01:00
parent 16fa1701d8
commit f5ed7ae491
2 changed files with 17 additions and 2 deletions

View file

@ -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'));

View file

@ -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) => {