Performance: Centralized error handler
This affects server response time and user experience by managing errors efficiently without blocking the event loop.
app.get('/data', async (req, res, next) => { try { const result = await database.query('SELECT * FROM table'); res.send(result); } catch (err) { next(err); } }); app.use((err, req, res, next) => { res.status(500).send('Centralized error: ' + err.message); });
app.get('/data', (req, res) => { database.query('SELECT * FROM table', (err, result) => { if (err) { res.status(500).send('Error occurred'); return; } res.send(result); }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated inline error handling | N/A (server-side) | N/A | N/A | [X] Bad |
| Centralized error middleware | N/A (server-side) | N/A | N/A | [OK] Good |
app.get('/test', (req, res, next) => {
next(new Error('Oops!'));
});
app.use((err, req, res, next) => {
res.status(500).send('Error caught: ' + err.message);
});app.use((req, res, next) => {
throw new Error('Fail');
});
app.use((req, res, next) => {
res.status(500).send('Error occurred');
});Pass the test and this pattern moves unknown