fix(capture): handle non-JSON responses from capture service gracefully

This commit is contained in:
Zac Gaetano 2026-05-20 13:55:06 -04:00
parent 96ef569720
commit 5161644205

View file

@ -7,67 +7,58 @@ router.use(requireAuth);
const CAPTURE_URL = process.env.CAPTURE_URL || 'http://capture:3001'; const CAPTURE_URL = process.env.CAPTURE_URL || 'http://capture:3001';
// Helper to proxy requests async function proxyRequest(method, path, body = null) {
const proxyRequest = async (method, path, body = null) => {
const options = { const options = {
method, method,
headers: { headers: { 'Content-Type': 'application/json' },
'Content-Type': 'application/json', signal: AbortSignal.timeout(8000),
},
}; };
if (body) options.body = JSON.stringify(body);
if (body) { const response = await fetch(`${CAPTURE_URL}${path}`, options);
options.body = JSON.stringify(body); const text = await response.text();
}
let data;
try { try {
const response = await fetch(`${CAPTURE_URL}${path}`, options); data = JSON.parse(text);
const data = await response.json(); } catch {
return { status: response.status, data }; // Capture service returned non-JSON (HTML error page, plain text, etc.)
} catch (err) { data = { message: text.slice(0, 300) || '(empty response)' };
console.error('Capture service error:', err);
throw err;
} }
};
// POST /start - Forward start request return { status: response.status, data };
}
// POST /start
router.post('/start', async (req, res, next) => { router.post('/start', async (req, res, next) => {
try { try {
const { status, data } = await proxyRequest('POST', '/start', req.body); const { status, data } = await proxyRequest('POST', '/start', req.body);
res.status(status).json(data); res.status(status).json(data);
} catch (err) { } catch (err) { next(err); }
next(err);
}
}); });
// POST /stop - Forward stop request // POST /stop
router.post('/stop', async (req, res, next) => { router.post('/stop', async (req, res, next) => {
try { try {
const { status, data } = await proxyRequest('POST', '/stop', req.body); const { status, data } = await proxyRequest('POST', '/stop', req.body);
res.status(status).json(data); res.status(status).json(data);
} catch (err) { } catch (err) { next(err); }
next(err);
}
}); });
// GET /status - Forward status request // GET /status
router.get('/status', async (req, res, next) => { router.get('/status', async (req, res, next) => {
try { try {
const { status, data } = await proxyRequest('GET', '/status'); const { status, data } = await proxyRequest('GET', '/status');
res.status(status).json(data); res.status(status).json(data);
} catch (err) { } catch (err) { next(err); }
next(err);
}
}); });
// GET /devices - Forward devices request // GET /devices
router.get('/devices', async (req, res, next) => { router.get('/devices', async (req, res, next) => {
try { try {
const { status, data } = await proxyRequest('GET', '/devices'); const { status, data } = await proxyRequest('GET', '/devices');
res.status(status).json(data); res.status(status).json(data);
} catch (err) { } catch (err) { next(err); }
next(err);
}
}); });
export default router; export default router;