fix: guard the async /check-now handler against crashing (finding 8)
Express 4 geeft een afgewezen promise uit een async handler niet door aan de error-handling; met Node 22 crashte daardoor het hele proces. De handler vangt de fout nu zelf af, logt hem en antwoordt met een 500. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013gwc3aCiyQxmCWQke8RvEz
This commit is contained in:
+10
-3
@@ -31,10 +31,17 @@ function createApp(deps) {
|
|||||||
res.redirect('/');
|
res.redirect('/');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Express 4 vangt een afgewezen promise uit een async handler niet af, en
|
||||||
|
// Node 22 laat het proces daarop crashen — dus zelf afvangen.
|
||||||
app.post('/check-now', async (req, res) => {
|
app.post('/check-now', async (req, res) => {
|
||||||
const products = listProducts();
|
try {
|
||||||
await checkAllProducts(products);
|
const products = listProducts();
|
||||||
res.redirect('/?checked=1');
|
await checkAllProducts(products);
|
||||||
|
res.redirect('/?checked=1');
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`[check-now] mislukt: ${err && err.message}`);
|
||||||
|
res.status(500).send('Er ging iets mis bij het controleren van de prijzen.');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return app;
|
return app;
|
||||||
|
|||||||
@@ -69,3 +69,19 @@ test('check-now triggers checkAllProducts and redirects with confirmation', asyn
|
|||||||
assert.equal(res.headers.location, '/?checked=1');
|
assert.equal(res.headers.location, '/?checked=1');
|
||||||
assert.equal(called, true);
|
assert.equal(called, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('check-now answers 500 when the check fails, without crashing the process', async (t) => {
|
||||||
|
t.mock.method(console, 'error', () => {});
|
||||||
|
const deps = makeDeps({
|
||||||
|
checkAllProducts: async () => { throw new Error('db weg'); },
|
||||||
|
});
|
||||||
|
deps.addProduct({ name: 'Nog steeds hier', url: 'https://shop.example/p3' });
|
||||||
|
const app = createApp(deps);
|
||||||
|
const res = await request(app).post('/check-now').auth('admin', 'secret');
|
||||||
|
assert.equal(res.status, 500);
|
||||||
|
assert.match(res.text, /Er ging iets mis/);
|
||||||
|
// Same process, same app instance: proves nothing crashed.
|
||||||
|
const list = await request(app).get('/').auth('admin', 'secret');
|
||||||
|
assert.equal(list.status, 200);
|
||||||
|
assert.match(list.text, /Nog steeds hier/);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user