0
0
Expressframework~20 mins

Custom error classes in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
});
AThe client receives status 500 with message 'Internal Server Error'
BThe client receives status 404 with message 'Resource not found'
CThe client receives status 404 with message 'Internal Server Error'
DThe client receives status 500 with message 'Resource not found'
Attempts:
2 left
💡 Hint
Look at the error's status property and how the error handler uses it.
📝 Syntax
intermediate
2: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.
A
function ValidationError(message) {
  this.status = 400;
  Error.call(this, message);
}
ValidationError.prototype = Object.create(Error.prototype);
B
class ValidationError extends Error {
  constructor(message) {
    this.status = 400;
    super(message);
  }
}
C
class ValidationError extends Error {
  constructor(message) {
    super(message);
    this.status = 400;
  }
}
D
class ValidationError {
  constructor(message) {
    super(message);
    this.status = 400;
  }
}
Attempts:
2 left
💡 Hint
Remember to call super(message) before using 'this' in a class constructor.
🔧 Debug
advanced
2: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);
});
AIf err.status is undefined, res.status(undefined) defaults to 500, but here err.status is 401, so this is not the cause.
BThe error handler does not check if err.status exists, so if err.status is undefined it sends status 0, causing fallback to 500.
CThe error handler should use res.status(err.status || 500) to handle cases where err.status is undefined.
DThe error handler is missing the 'next' parameter, so Express does not recognize it as an error handler.
Attempts:
2 left
💡 Hint
Express requires error handlers to have four parameters to work properly.
state_output
advanced
2: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);
A"DatabaseError"
B"Error"
C"ServiceUnavailableError"
D"CustomError"
Attempts:
2 left
💡 Hint
The name property of an Error instance defaults to the class name.
🧠 Conceptual
expert
2:00remaining
Why use custom error classes in Express applications?
Which of the following is the best reason to use custom error classes in Express?
ATo group related errors and easily set HTTP status codes and messages for consistent error handling.
BTo avoid using try-catch blocks anywhere in the code by throwing only custom errors.
CTo automatically log errors to a database without additional code.
DTo replace Express's built-in error handler with a custom one that never sends 500 status.
Attempts:
2 left
💡 Hint
Think about how custom errors help organize error handling and responses.