0
0
Expressframework~5 mins

Custom error classes in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom error class in Express?
A custom error class is a user-defined class that extends the built-in Error class to create specific error types with custom properties or messages, helping to handle errors more clearly in Express apps.
Click to reveal answer
beginner
How do you create a basic custom error class in Express?
You create a class that extends Error, call super(message) in the constructor, and optionally add properties like statusCode to represent HTTP error codes.
Click to reveal answer
intermediate
Why add a statusCode property to a custom error class in Express?
Adding statusCode helps middleware know which HTTP status to send in the response, making error responses clear and consistent.
Click to reveal answer
intermediate
What is the benefit of using custom error classes over plain Error objects in Express?
Custom error classes let you distinguish error types easily, add extra info, and handle different errors in specific ways in middleware.
Click to reveal answer
beginner
Show a simple example of a custom NotFoundError class in Express.
class NotFoundError extends Error {
  constructor(message = 'Not Found') {
    super(message);
    this.name = 'NotFoundError';
    this.statusCode = 404;
  }
}
Click to reveal answer
What must a custom error class extend in Express?
AError
BObject
CResponse
DRequest
Why include a statusCode property in a custom error?
ATo store HTTP status for responses
BTo log errors to console
CTo change request headers
DTo modify URL paths
Which method is called inside the constructor of a custom error class?
Anew Error()
Bthis.error()
Cextend()
Dsuper(message)
What is a key advantage of custom error classes in Express?
AThey speed up server response
BThey allow specific error handling
CThey reduce file size
DThey replace middleware
Which property usually holds the error message in a custom error?
Aname
BstatusCode
Cmessage
Dstack
Explain how to create and use a custom error class in Express.
Think about how errors flow from route to middleware.
You got /5 concepts.
    Describe why custom error classes improve error handling in Express applications.
    Consider how different errors need different responses.
    You got /5 concepts.