0
0
Node.jsframework~10 mins

Custom error classes in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a custom error class named MyError.

Node.js
class MyError extends [1] {
  constructor(message) {
    super(message);
    this.name = 'MyError';
  }
}
Drag options to blanks, or click blank then click option'
AObject
BArray
CError
DFunction
Attempts:
3 left
💡 Hint
Common Mistakes
Extending Object or Array instead of Error.
Forgetting to call super(message) in constructor.
2fill in blank
medium

Complete the code to throw a new instance of the custom error MyError with a message.

Node.js
throw new [1]('Something went wrong');
Drag options to blanks, or click blank then click option'
AError
BMyError
CTypeError
DReferenceError
Attempts:
3 left
💡 Hint
Common Mistakes
Throwing the base Error instead of the custom error.
Forgetting the new keyword.
3fill in blank
hard

Fix the error in the custom error class by completing the missing call to the parent constructor.

Node.js
class ValidationError extends Error {
  constructor(message) {
    [1];
    this.name = 'ValidationError';
  }
}
Drag options to blanks, or click blank then click option'
Asuper(message)
Bthis.message = message
CError.call(this, message)
Dreturn message
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning message directly without calling super.
Calling Error.call without super in ES6 classes.
4fill in blank
hard

Fill both blanks to create a custom error class named DatabaseError that sets the name and captures the stack trace.

Node.js
class DatabaseError extends Error {
  constructor(message) {
    super(message);
    this.name = [1];
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, [2]);
    }
  }
}
Drag options to blanks, or click blank then click option'
A'DatabaseError'
BDatabaseError
C'Error'
DError
Attempts:
3 left
💡 Hint
Common Mistakes
Setting name to the class instead of a string.
Passing a string instead of the class to captureStackTrace.
5fill in blank
hard

Fill all three blanks to create a custom error class ApiError that accepts a message and a status code, sets the name, and stores the status code.

Node.js
class ApiError extends Error {
  constructor(message, statusCode) {
    super([1]);
    this.name = [2];
    this.statusCode = [3];
  }
}
Drag options to blanks, or click blank then click option'
Amessage
B'ApiError'
CstatusCode
D'Error'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing statusCode to super instead of message.
Setting name to 'Error' instead of 'ApiError'.
Not storing the statusCode property.