Performance: Custom error classes
This affects the error handling flow and stack trace generation during runtime, impacting debugging speed and error reporting performance.
Jump into concepts and practice - no test required
class FetchError extends Error { constructor(message) { super(message); this.name = 'FetchError'; } } function fetchData() { throw new FetchError('Failed to fetch data'); }
function fetchData() {
throw new Error('Failed to fetch data');
}| Pattern | Runtime Overhead | Stack Trace Clarity | Debugging Speed | Verdict |
|---|---|---|---|---|
| Generic Error | Minimal | Low (generic message) | Slower due to unclear error type | [!] OK |
| Custom Error Class | Minimal | High (specific error name) | Faster due to clear error type | [OK] Good |
MyError in Node.js?class NotFoundError extends Error {
constructor(message) {
super(message);
this.name = 'NotFoundError';
}
}
try {
throw new NotFoundError('Item not found');
} catch (e) {
console.log(e.name + ': ' + e.message);
}class ValidationError extends Error {
constructor(msg) {
this.message = msg;
this.name = 'ValidationError';
}
}AuthError that includes a statusCode property for HTTP status codes. Which implementation correctly adds this property and preserves the error behavior?