Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Custom Error Classes in Node.js
📖 Scenario: You are building a simple Node.js application that processes user input. To handle errors clearly, you want to create a custom error class that extends the built-in Error class.
🎯 Goal: Create a custom error class called ValidationError that extends Error. Then use it to throw an error with a specific message.
📋 What You'll Learn
Create a class named ValidationError that extends Error
Add a constructor that accepts a message parameter
Call the parent Error constructor with the message
Set the name property of the error to ValidationError
Throw an instance of ValidationError with the message 'Invalid input'
💡 Why This Matters
🌍 Real World
Custom error classes help make error handling clearer and more specific in Node.js applications, improving debugging and user feedback.
💼 Career
Understanding custom errors is important for backend developers to write robust, maintainable code and handle different error cases properly.
Progress0 / 4 steps
1
Create the custom error class skeleton
Create a class called ValidationError that extends Error. Add a constructor that takes a message parameter.
Node.js
Hint
Remember to use extends to inherit from Error and call super(message) inside the constructor.
2
Set the error name property
Inside the ValidationError constructor, set this.name to the string 'ValidationError'.
Node.js
Hint
Setting this.name helps identify the error type when caught.
3
Throw the custom error
Write a line that throws a new ValidationError with the message 'Invalid input'.
Node.js
Hint
Use the throw keyword followed by new ValidationError('Invalid input').
4
Wrap throw in try-catch and log error name and message
Wrap the throw statement inside a try block. Add a catch block that catches the error as err and logs err.name and err.message.
Node.js
Hint
Use try { ... } catch (err) { ... } and inside catch log the error's name and message.
Practice
(1/5)
1. What is the main reason to create a custom error class in Node.js?
easy
A. To define specific error types for clearer error handling
B. To make the program run faster
C. To avoid using try-catch blocks
D. To replace the built-in Error class completely
Solution
Step 1: Understand the purpose of custom errors
Custom error classes help identify and handle specific error cases clearly in your code.
Step 2: Compare with other options
Custom errors do not speed up code, avoid try-catch, or replace Error class but extend it.
Final Answer:
To define specific error types for clearer error handling -> Option A
Quick Check:
Custom error purpose = Specific error types [OK]
Hint: Custom errors clarify error types, not speed or structure [OK]
Common Mistakes:
Thinking custom errors improve performance
Believing custom errors remove need for try-catch
Assuming custom errors replace built-in Error
2. Which of the following is the correct way to define a custom error class named MyError in Node.js?
easy
A. class MyError extends Object { constructor(message) { super(message); this.name = 'MyError'; } }
B. function MyError() { this.message = 'Error'; this.name = 'MyError'; }
C. class MyError { constructor(message) { this.message = message; } }
D. class MyError extends Error { constructor(message) { super(message); this.name = 'MyError'; } }
Solution
Step 1: Check class inheritance
Custom errors must extend the built-in Error class to behave like errors.
Step 2: Verify constructor and name setting
The constructor calls super(message) and sets this.name to the class name for clarity.
Final Answer:
class MyError extends Error { constructor(message) { super(message); this.name = 'MyError'; } } -> Option D
Quick Check:
Extend Error and set name = MyError [OK]
Hint: Extend Error and set this.name in constructor [OK]
When extending Error, constructor must call super() before using this.
Step 2: Identify missing super call
The code sets this.message and this.name without calling super(msg), causing a runtime error.
Final Answer:
Missing call to super() in constructor -> Option A
Quick Check:
Always call super() first in subclass constructor [OK]
Hint: Always call super() before using this in constructor [OK]
Common Mistakes:
Forgetting super() call causes ReferenceError
Setting this.message before super()
Extending wrong base class
5. You want to create a custom error class AuthError that includes a statusCode property for HTTP status codes. Which implementation correctly adds this property and preserves the error behavior?
hard
A. class AuthError extends Error {
constructor(statusCode) {
super();
this.name = 'AuthError';
this.statusCode = statusCode;
}
}
B. class AuthError extends Error {
constructor(message, statusCode) {
this.message = message;
this.statusCode = statusCode;
this.name = 'AuthError';
}
}
C. class AuthError extends Error {
constructor(message, statusCode) {
super(message);
this.name = 'AuthError';
this.statusCode = statusCode;
}
}
D. class AuthError {
constructor(message, statusCode) {
this.message = message;
this.statusCode = statusCode;
this.name = 'AuthError';
}
}
Solution
Step 1: Check proper Error extension and constructor
The class must extend Error and call super(message) to set the error message correctly.
Step 2: Verify additional property and name
Setting this.statusCode after super() adds the extra info; setting this.name clarifies error type.
Final Answer:
class AuthError extends Error {
constructor(message, statusCode) {
super(message);
this.name = 'AuthError';
this.statusCode = statusCode;
}
} -> Option C
Quick Check:
Extend Error, call super(message), add extra properties [OK]
Hint: Call super(message) first, then add extra properties [OK]