Build a ValidationError class and report its name.
Acceptance criteria
- 1.#out should show ValidationError.
JavaScript Tutorial · JavaScript Errors
These 10 tasks go with the JavaScript Custom Errors chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “A custom error class describes a failure specific to your own program.” .
1.Create a custom errorexercise · easy · 10 min
Build a ValidationError class and report its name.
Passes when: #out should show ValidationError.
2.Show it is still an Errorexercise · easy · 10 min · practice plan
Report whether a custom error passes the Error check.
Passes when: #out should show still an error.
3.Carry an extra propertyexercise · easy · 10 min · practice plan
Attach the field name and report it with the message.
Passes when: #out should show email is required.
4.Handle your own separatelyexercise · easy · 10 min · practice plan
Tell a validation problem apart from a real bug.
Passes when: #out should show show the user.
5.Build a small hierarchyexercise · medium · 10 min · practice plan
Show a NotFoundError is also an AppError.
Passes when: #out should show part of the family.
6.Fix the missing super callbug fix · easy · 8 min
The parent constructor must run before this is used, or the error cannot be built.
Passes when: #out should show name is required.
7.Fix the class that does not extend Errorbug fix · easy · 8 min · practice plan
Without extending Error it is just a plain object.
Passes when: #out should show still an error.
8.Fix the missing namebug fix · easy · 8 min · practice plan
Without setting name the error reports as a plain Error.
Passes when: #out should show ValidationError.
9.Fix the extra property never storedbug fix · easy · 8 min · practice plan
The field is accepted but never put on the error.
Passes when: #out should show email is required.
10.Fix the name compared instead of the typebug fix · easy · 8 min · practice plan
Comparing names is fragile; instanceof is the proper check.
Passes when: #out should show show the user.
Between them these tasks cover Custom errors, extends Error and instanceof.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.