Lesson 4 of 8
Errors that explain what happened
- 12 min
- 5 checks
- 3 hints
- Go to task
Goal
The word "failed" alone is not enough
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.
Your task
Make loadUser(id) throw an error that mentions the id and keeps the original cause.
Checks
5 checks- The function works with a valid id
- Throws an error when it fails
- The message mentions the id
- The original error is kept in cause
- The page shows the message and the cause