You want to create a custom error class DatabaseError that includes a query property showing the failed SQL query. Which is the best way to implement it?
hard📝 component behavior Q8 of 15
Node.js - Error Handling Patterns
You want to create a custom error class DatabaseError that includes a query property showing the failed SQL query. Which is the best way to implement it?
class DatabaseError extends Error { constructor(message) { super(message); this.query = ''; this.name = 'DatabaseError'; } } does not accept query parameter; C does not extend Error; D is a function, not a class.
Final Answer:
class DatabaseError extends Error { constructor(message, query) { super(message); this.query = query; this.name = 'DatabaseError'; } } -> Option B
Quick Check:
Extend Error + add custom properties in constructor [OK]
Quick Trick:Add custom properties after calling super(message) [OK]
Common Mistakes:
Not extending Error class
Not passing message to super
Ignoring custom property initialization
Master "Error Handling Patterns" in Node.js
9 interactive learning modes - each teaches the same concept differently