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