What if your app could tell exactly what went wrong without messy code everywhere?
Why Custom error classes in Express? - Purpose & Use Cases
Imagine building an Express app where you check errors everywhere with generic messages like "Something went wrong" and manually set HTTP status codes in each route.
This manual error handling is confusing, repetitive, and easy to mess up. You might forget to set the right status code or mix error types, making debugging and user feedback frustrating.
Custom error classes let you define clear, reusable error types with built-in status codes and messages. This makes your error handling consistent, clean, and easy to maintain across your Express app.
if (!user) { res.status(404).send('User not found'); }
throw new NotFoundError('User not found');It enables centralized, clear, and scalable error handling that improves code quality and user experience.
When a user tries to access a missing page, a custom NotFoundError automatically sends a 404 status with a helpful message, without repeating code in every route.
Manual error handling is repetitive and error-prone.
Custom error classes organize errors with clear types and status codes.
This leads to cleaner, more maintainable Express apps.
Practice
Solution
Step 1: Understand the role of custom error classes
Custom error classes extend the built-in Error class to represent specific error types with meaningful messages and HTTP status codes.Step 2: Compare with other options
Custom errors do not replace Express error handling but improve it; they do not fix code automatically or remove the need for try-catch.Final Answer:
To define specific error types with custom messages and status codes -> Option AQuick Check:
Custom error classes = specific error types [OK]
- Thinking custom errors fix bugs automatically
- Believing they replace Express error middleware
- Assuming they remove try-catch necessity
NotFoundError that sets a 404 status code?Solution
Step 1: Check class inheritance and constructor
The custom error class must extend Error and call super() with the message to set the error message properly.Step 2: Verify setting status property
Setting this.status = 404 inside the constructor is correct for HTTP status code.Final Answer:
class NotFoundError extends Error { constructor() { super('Not Found'); this.status = 404; } } -> Option DQuick Check:
Extend Error + super() + status property = correct syntax [OK]
- Not calling super() in constructor
- Defining error as plain class without extending Error
- Using function instead of class for custom error
throw new ValidationError('Invalid input'); is caught and logged?class ValidationError extends Error {
constructor(message) {
super(message);
this.status = 400;
}
}
try {
throw new ValidationError('Invalid input');
} catch (err) {
console.log(err.message, err.status);
}Solution
Step 1: Understand the error message and status
The ValidationError constructor calls super(message), so err.message is 'Invalid input'. The status property is set to 400.Step 2: Check console.log output
Logging err.message and err.status prints 'Invalid input' and 400 separated by space.Final Answer:
Invalid input 400 -> Option AQuick Check:
err.message = 'Invalid input', err.status = 400 [OK]
- Expecting class name as message
- Assuming status is undefined
- Confusing Error default message with custom message
class AuthError extends Error {
constructor(message) {
this.message = message;
this.status = 401;
}
}Solution
Step 1: Check constructor of subclass of Error
When extending Error, the constructor must call super(message) to properly initialize the error message.Step 2: Identify missing super call
The code sets this.message directly but does not call super(), which causes errors in Error inheritance.Final Answer:
Missing call to super(message) in constructor -> Option CQuick Check:
Always call super(message) in Error subclass constructor [OK]
- Setting this.message without super()
- Forgetting to extend Error
- Using wrong status codes for errors
DatabaseError that accepts a message and an optional error code, defaulting to 500. Which implementation correctly supports this and sets both status and code properties?Solution
Step 1: Check constructor parameters and defaults
class DatabaseError extends Error { constructor(message, code) { super(message); this.status = code || 500; this.code = code || 500; } } uses code parameter without default but uses code || 500 to set status and code, supporting optional code.Step 2: Verify super call and property assignments
super(message) is called correctly; this.status and this.code are set to code or 500 if code is falsy.Step 3: Compare with other options
class DatabaseError extends Error { constructor(message, code = 500) { super(message); this.status = 500; this.code = code; } } sets status always to 500 ignoring code parameter; class DatabaseError extends Error { constructor(message, code = 500) { super(message); this.status = code; this.code = 500; } } swaps status and code values; class DatabaseError extends Error { constructor(message, code) { this.message = message; this.status = 500; this.code = code || 500; } } misses super() call.Final Answer:
class DatabaseError extends Error { constructor(message, code) { super(message); this.status = code || 500; this.code = code || 500; } } -> Option BQuick Check:
Optional code with fallback and super() call = correct [OK]
- Ignoring super() call
- Swapping status and code values
- Not supporting optional parameter correctly
Ready to prove it?
Pass the test and this pattern moves unknown
