Bird
0
0

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📝 Application Q15 of 15
Node.js - Error Handling Patterns
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?
Aclass AuthError extends Error { constructor(statusCode) { super(); this.name = 'AuthError'; this.statusCode = statusCode; } }
Bclass AuthError extends Error { constructor(message, statusCode) { this.message = message; this.statusCode = statusCode; this.name = 'AuthError'; } }
Cclass AuthError extends Error { constructor(message, statusCode) { super(message); this.name = 'AuthError'; this.statusCode = statusCode; } }
Dclass AuthError { constructor(message, statusCode) { this.message = message; this.statusCode = statusCode; this.name = 'AuthError'; } }
Step-by-Step Solution
Solution:
  1. Step 1: Check proper Error extension and constructor

    The class must extend Error and call super(message) to set the error message correctly.
  2. Step 2: Verify additional property and name

    Setting this.statusCode after super() adds the extra info; setting this.name clarifies error type.
  3. Final Answer:

    class AuthError extends Error { constructor(message, statusCode) { super(message); this.name = 'AuthError'; this.statusCode = statusCode; } } -> Option C
  4. Quick Check:

    Extend Error, call super(message), add extra properties [OK]
Quick Trick: Call super(message) first, then add extra properties [OK]
Common Mistakes:
  • Not calling super(message) in constructor
  • Not extending Error class
  • Missing message parameter in super call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes