JavaScript, inside out · Chapter 38 of 51
Error handling
By the end of this chapter you can throw clear errors of the right kind, make your own error types, catch only what you can fix, and clean up whether the work failed or not. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Going async
- 6 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
Last Friday one bad order, "two" bun maska typed as a word, crashed the whole order screen, and Ravi took orders on paper for an hour. The UPI gateway times out in the monsoon, ginger runs out at 4 pm, and the till drawer stayed open after a printer jam. Meera does not ask for an app that never fails: "Fail properly. Tell me what went wrong, and keep the rest of the stall running."
So far your functions either return an answer or crash. A function could return null or 'error' instead, but then every caller must remember to check, and one that forgets carries on with garbage. And an error that nobody catches stops everything after it.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
parseQuantity: bad quantities get the right kind of error
Write parseQuantity(input). It accepts a number or a string holding a number (spaces around it are fine) and returns the whole number of cups: parseQuantity('3') gives 3, parseQuantity(' 2 ') gives 2, parseQuantity(5) gives 5.
Throw a TypeError when the input is not a whole number at all: text such as 'two' or '3 cups', an empty or blank string, a decimal such as 2.5 or '2.5', NaN, null and undefined. Throw a RangeError when it is a whole number outside 1 to 20, such as 0, '0', -1, 21 or '21'. 1 and 20 are allowed.
- Task 2
OrderError: a name, a code and a cause
Finish the class OrderError, which extends Error. new OrderError(message, { code, cause }) keeps the message, has the name 'OrderError', and has the code it was given, or 'ORDER_FAILED' when there is none. The cause is passed on to Error, so err.cause is the original error, or undefined when none was given. The options object may be left out completely.
String(new OrderError('Rider not found')) gives 'OrderError: Rider not found', and it is still an instance of Error with a stack.
- Task 3
OutOfStockError: a kind of OrderError
Make OutOfStockError extend OrderError (not Error directly). new OutOfStockError('ginger') has the message 'ginger is out of stock', the name 'OutOfStockError', the code 'OUT_OF_STOCK' and the item 'ginger'.
It is an instance of OutOfStockError, OrderError and Error, while a plain OrderError is not an OutOfStockError.
- Task 4
chargeUPI: wrap a failed charge, keep the original error
Write chargeUPI(gateway, amount). Call gateway.charge(amount) and return what it returns.
If gateway.charge throws, throw an OrderError instead, with the message 'Payment of ₹120 failed' (with the real amount), the code 'PAYMENT_FAILED', and the gateway's original error as its cause.
- Task 5 · interview question
Interview: tryPlaceOrder, handle only the errors you expect
Write tryPlaceOrder(place). Call place() and, when it works, return { ok: true, order } with what it returned.
When it throws an OutOfStockError, return { ok: false, message } with that error's message, such as 'bun maska is out of stock'. Any other error, including a TypeError from a bug or another OrderError, is thrown again unchanged: the very same error object.
- Task 6
withTill: the till always closes
Write withTill(till, work). Call till.open(), then work(), then till.close(), and return what work returned: the order of calls is open, work, close.
If work throws, the till still closes, and then the very same error reaches the caller: withTill does not catch it or replace it.
What you will learn
- throw with the right type: TypeError and RangeError say what kind of problem it is.
- try, catch and finally: recover from an error, and clean up either way.
- Your own error classes: a name, extra fields, and a cause that keeps the original error.
- Rethrowing: handle the errors you expect and let the rest through.
More in Going async
Build "Error handling" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.