Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Keeping the Original Error With cause

Keeping the Original Error With cause

Joan’s December export still doesn’t exist. In Python, read_export() turns the missing file into the program’s own error, and main() prints the original, err.__cause__, on the report’s second line:

Language: Python
    except FileNotFoundError as err:
        raise ExportMissingError(f"no export at {path}") from err

Uncaught, the traceback shows the FileNotFoundError, then “The above exception was the direct cause of the following exception:”, then the ExportMissingError. Even without from, Python keeps the first error as __context__, joined by “During handling of the above exception, another exception occurred:”.

In the JavaScript port, readExport() awaits readFile() inside try, so it’s an async function, and its catch checks err.code for "ENOENT" (“error, no entry”: no such file). JavaScript has no from, so the natural port leaves it out, and an uncaught call from a two-line script shows no sign of the ENOENT error:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Joan’s December export still doesn’t exist. The Python report prints two lines: the script’s summary, and under it, what the system reported, for when the summary isn’t enough.

00:13 The JavaScript port prints only the first. In this lesson, you get the second line to print.

00:21 In Python, read_export() raises the program’s own error from the FileNotFoundError, and main() prints that cause on the second line.

00:31 Let read_export() crash, uncaught, from a two-line script. The traceback shows the FileNotFoundError first, then the direct cause line, and your ExportMissingError last.

00:44 Without from, Python keeps both, joined by a during handling line instead.

00:52 In the JavaScript port, readExport() awaits readFile() inside try, so it’s marked as an async function.

01:00 Its catch checks err.code for ENOENT, short for error, no entry: no file by that name. Anything else is thrown on again.

01:11 JavaScript has no from, so the raise line ports without it, as a throw of a new ExportMissingError with the same message.

01:21 Call readExport() the same way, uncaught, from a two-line script that imports it and awaits it at the top level. Python’s traceback showed both errors, with from or without it.

01:34 Does the ENOENT error show up anywhere in Node’s crash output? Your turn: pause the video and predict.

01:46 No. The crash shows the throw line, ExportMissingError with its message, and two stack frames. Nothing in it mentions ENOENT, or the original error.

02:00 Python attaches the error being handled to any error raised in the except, as its __context__, for you. With from, it sets __cause__ too.

02:12 JavaScript doesn’t: a new error knows nothing about the one being handled. new builds a fresh error from only what you pass. So the ENOENT stays behind in the catch, and the second line has nothing to print.

02:29 The fix is the options object every built-in error constructor takes. Pass the original as { cause: err }, and err.cause holds it on the new error.

02:41 DonationError hands its options to super(), as the last section set it up. Watch out: Python’s from works on any class, but here the cause goes through the constructor, so one of your own, like BadRowError, must take options and pass them to super(), or the cause is lost without a word.

03:02 Run the two-line script again. After the stack, the crash adds a block marked [cause]. It holds the ENOENT error with its message, code, and path.

03:13 It’s Node’s version of Python’s first traceback, nested under the new error, not above it. But this one has no stack frames: it says what failed, not where.

03:27 In the report, err.cause is Python’s __cause__, set only when you pass it. This check was there all along, folded: without a cause, it’s undefined, which counts as false in an if, so the line never printed.

03:43 The line prints the cause’s message: as text, a JavaScript error would start with its name. Run December’s import, and the second line prints. Its words are each runtime’s own: Python says Errno 2, JavaScript says ENOENT.

04:03 The error thrown inside the async readExport() arrives at the caller’s await, so main()’s try around await importMonth() catches it, as a try around a plain call would.

04:14 Async errors get a course of their own later.

04:19 When you catch one error to throw a clearer one, pass the original as cause, and make every constructor of your own hand super() its options.

04:29 BadRowError is ready too, but the amount check has nothing to chain: Number() doesn’t throw. Next up, Explain Why the Settings Didn’t Load.

04:40 You’ll report that hand-edited settings didn’t load and, on a second line, what JSON.parse() objected to, as Python does.

Become a Member to join the conversation.