LearnFrenzy
Sign in Take a free test

Error Handling · Find the Error · Question 12

Bug: validation errors get the generic message Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
class ValidationError extends Error {
  constructor(m) { super(m); this.name = "ValidationError"; }
}
function message(e) {
  if (e instanceof Error) return "Something went wrong";
  if (e instanceof ValidationError) return `Please fix: ${e.message}`;
  return "Unknown problem";
}
console.log(message(new ValidationError("phone must be 10 digits")));
  • A instanceof does not work with classes
  • B ValidationError needs a status code
  • C Every ValidationError is also an Error, so the general check matches first – test the specific class before Error Correct
  • D message must be async
Correct Answer: Option C

Order checks from most specific to most general.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links