mirror of
https://github.com/screentinker/screentinker.git
synced 2026-08-16 23:33:10 -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 = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve(JSON.parse(xhr.responseText));
|
||||
try {
|
||||
resolve(JSON.parse(xhr.responseText));
|
||||
} catch {
|
||||
resolve({ ok: true }); // handle empty/non-JSON 2xx response
|
||||
}
|
||||
} 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'));
|
||||
|
|
|
|||
|
|
@ -965,6 +965,15 @@ app.get('/download/apk', (req, res) => {
|
|||
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
|
||||
// clients fail fast instead of hanging until Cloudflare's 15s upstream timeout.
|
||||
app.get('*', (req, res) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue