Skip to main content

JavaScript Custom Errors — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Custom Errors lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Create a custom error

    easy10 min

    Build a ValidationError class and report its name.

  2. 2

    Show it is still an Error

    easy10 min

    Report whether a custom error passes the Error check.

  3. 3

    Carry an extra property

    easy10 min

    Attach the field name and report it with the message.

  4. 4

    Handle your own separately

    easy10 min

    Tell a validation problem apart from a real bug.

  5. 5

    Build a small hierarchy

    medium10 min

    Show a NotFoundError is also an AppError.

Find the bug(5)

  1. 1

    Fix the missing super call

    easy8 min

    The parent constructor must run before this is used, or the error cannot be built.

  2. 2

    Fix the class that does not extend Error

    easy8 min

    Without extending Error it is just a plain object.

  3. 3

    Fix the missing name

    easy8 min

    Without setting name the error reports as a plain Error.

  4. 4

    Fix the extra property never stored

    easy8 min

    The field is accepted but never put on the error.

  5. 5

    Fix the name compared instead of the type

    easy8 min

    Comparing names is fragile; instanceof is the proper check.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace