Describe Errors by Their Class

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Export two error classes that extend Error: ValidationError(field, message), which keeps the name of the field that failed, and NetworkError(status, message), which keeps the HTTP status as a number. Then write describeError(error), which takes anything a catch block can catch and returns Invalid email: message for a ValidationError, Network error (503) for a NetworkError, the message of any other Error, and Unknown error for everything else. Type the parameter as unknown, and tell the cases apart with instanceof.

Your code must pass

  • Describes a ValidationError with its field
  • Describes a NetworkError with its status
  • Falls back for other errors and non-errors
  • Has no type errors
  • ValidationError keeps a string field
  • NetworkError's status must be a number
  • describeError takes unknown