Challenge - 5 Problems
Express Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when throwing a custom error in Express?
Consider this Express middleware that throws a custom error. What will be the HTTP status code sent to the client?
Express
class NotFoundError extends Error { constructor(message) { super(message); this.status = 404; } } app.use((req, res, next) => { throw new NotFoundError('Resource not found'); }); app.use((err, req, res, next) => { res.status(err.status || 500).send(err.message); });
Attempts:
2 left
💡 Hint
Look at the error's status property and how the error handler uses it.
✗ Incorrect
The custom error sets status to 404. The error handler uses err.status or defaults to 500. So the response status is 404 with the error message.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a custom error class in Express?
Choose the option that correctly defines a custom error class named ValidationError that sets a status property to 400.
Attempts:
2 left
💡 Hint
Remember to call super(message) before using 'this' in a class constructor.
✗ Incorrect
Option C correctly calls super(message) first, then sets this.status. Option C calls this.status before super, which is invalid. Option C uses function syntax but misses proper Error initialization. Option C lacks 'extends Error'.
🔧 Debug
advanced2:00remaining
Why does this custom error not send the correct status code?
This Express error handler always sends status 500 even when a custom error with a status property is thrown. Why?
Express
class AuthError extends Error { constructor(message) { super(message); this.status = 401; } } app.use((req, res, next) => { next(new AuthError('Unauthorized')); }); app.use((err, req, res) => { res.status(err.status).send(err.message); });
Attempts:
2 left
💡 Hint
Express requires error handlers to have four parameters to work properly.
✗ Incorrect
Express identifies error handlers by the presence of four parameters (err, req, res, next). If the handler misses 'next', it is treated as a normal middleware and won't catch errors properly, causing fallback to default 500.
❓ state_output
advanced2:00remaining
What is the value of err.name for this custom error instance?
Given this custom error class and instance, what is the value of err.name?
Express
class DatabaseError extends Error { constructor(message) { super(message); this.status = 503; } } const err = new DatabaseError('DB connection failed'); console.log(err.name);
Attempts:
2 left
💡 Hint
The name property of an Error instance defaults to the class name.
✗ Incorrect
When extending Error, the name property is automatically set to the class name, here 'DatabaseError'.
🧠 Conceptual
expert2:00remaining
Why use custom error classes in Express applications?
Which of the following is the best reason to use custom error classes in Express?
Attempts:
2 left
💡 Hint
Think about how custom errors help organize error handling and responses.
✗ Incorrect
Custom error classes help categorize errors, attach status codes, and provide clear messages, making error handling consistent and maintainable.