Lesson 4 of 8

Errors that explain what happened

Goal

The word "failed" alone is not enough

01

Explanation

A message like "Something went wrong" helps nobody: the user doesn't know what to do, and you don't know where to look.

A useful error tells you three things: what the code was trying to do, with which input, and what the original cause was. Modern JavaScript has an option called cause made for exactly this:

throw new Error('Could not load user 42', { cause: original })

You keep the original error intact and add a message on top that explains what was going on.

02

Your task

Make loadUser(id) throw an error that mentions the id and keeps the original cause.

Checks

5 checks
  1. The function works with a valid id
  2. Throws an error when it fails
  3. The message mentions the id
  4. The original error is kept in cause
  5. The page shows the message and the cause
03

Hints

0/3
Track lessons0 of 8 done

Learn how to read an error message, check inputs before you use them, and catch the errors worth handling.

Intermediate level, 8 lessons, 1.5 hours in total.

index.html
Preview
index.html100%
–/5

Press “Run and check” to grade your code.

5 checks listed under “Your task”.

Track lessons0 of 8 done

Learn how to read an error message, check inputs before you use them, and catch the errors worth handling.

Intermediate level, 8 lessons, 1.5 hours in total.