Performance: Custom error classes
MEDIUM IMPACT
This affects server response time and error handling efficiency, impacting how quickly errors are processed and sent to clients.
class NotFoundError extends Error { constructor(message) { super(message); this.name = 'NotFoundError'; this.status = 404; } } app.use((req, res, next) => { try { // some code } catch (err) { next(err); } });
app.use((req, res, next) => {
try {
// some code
} catch (err) {
err.status = 500;
next(err);
}
});| Pattern | Error Object Creation | Type Checking | Stack Trace Clarity | Verdict |
|---|---|---|---|---|
| Modifying generic Error objects dynamically | Low cost | High cost due to repeated checks | Unclear or inconsistent | [X] Bad |
| Using custom error classes with fixed properties | Low cost | Low cost with instanceof checks | Clear and consistent | [OK] Good |