MediumModern JS🐛 Debug Challenge

Custom error class not extending Error

Buggy Code — Can you spot the issue?

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);
}

Fixed Code

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 Explained

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.

More Modern JS Debug Challenges

EasyManual swap loses original valueEasysort() mutates the original arrayMediumRegex with global flag alternates true/falseHardIterator is single-use — second loop produces nothing

Practice spotting bugs live →

38 debug challenges with AI hints

🐛 Try Debug Lab