Complete the code to define a custom error class named MyError.
class MyError extends [1] { constructor(message) { super(message); this.name = 'MyError'; } }
The custom error class should extend the built-in Error class to behave like an error.
Complete the code to throw the custom error with a message.
throw new MyError([1]);Errors usually carry a descriptive message as a string.
Fix the error in the custom error class to properly set the prototype chain.
class CustomError extends Error { constructor(message) { super(message); this.name = 'CustomError'; [1]; } }
Setting the prototype chain with Object.setPrototypeOf(this, CustomError.prototype) ensures instanceof works correctly.
Fill both blanks to create a middleware function that catches CustomError and sends a 400 status.
function errorHandler(err, req, res, next) {
if (err instanceof [1]) {
res.status([2]).send({ error: err.message });
} else {
next(err);
}
}The middleware checks if the error is a CustomError and sends a 400 Bad Request status.
Fill all three blanks to define a custom error with a status code property and use it in middleware.
class [1] extends Error { constructor(message) { super(message); this.status = [2]; this.name = '[3]'; } } function handler(err, req, res, next) { res.status(err.status || 500).send({ error: err.message }); }
The class is named ValidationError, has a status 400, and sets the name accordingly for clear error handling.