class AppError {
constructor(message, code) {
this.message = message;
this.code = code;
this.name = 'AppError';
}
}
try {
throw new AppError('Something went wrong', 500);
} catch (e) {
console.log(e instanceof Error);
console.log(e.name);
console.log(e.message);
}class AppError extends Error {
constructor(message, code) {
super(message);
this.name = 'AppError';
this.code = code;
}
}
try {
throw new AppError('Something went wrong', 500);
} catch (e) {
console.log(e instanceof Error);
console.log(e.name);
console.log(e.message);
}Bug: AppError does not extend Error. It has no prototype relationship to Error so instanceof Error is false and there is no stack trace.
Explanation: extends Error + super(message) establishes the prototype chain. this.name overrides the default "Error" string.
Key Insight: Custom errors must extend Error to pass instanceof checks, get a stack trace, and work correctly with error monitoring tools.