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 both errors, joined by “The above exception was the direct cause of the following exception:”. Python keeps both, with from or without it.

In the JavaScript port, readExport() is async to await readFile(), and its catch checks err.code for "ENOENT" (“error, no entry”: no such file). JavaScript has no from, so the raise line ports without it. Called uncaught from a two-line script, it crashes with 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. Python’s report prints two lines: the summary, and what the system reported, for when that isn’t enough. The JavaScript port prints only the first.

00:12 You’ll get the second line to print. In Python, read_export() raises its own error from the FileNotFoundError. Let it crash, and the traceback shows both, joined by the direct cause line: Python keeps both, with from or without it.

00:31 The JavaScript port’s readExport() is async to await readFile(), and its catch checks err.code for ENOENT, no such file.

00:40 JavaScript has no from, so the raise line ports without it. Let readExport() crash the same way, from a two-line script.

00:50 Does the ENOENT error show up anywhere in the crash? Your turn: pause the video and predict.

01:02 No. The throw line, the new error and two stack frames, and no ENOENT anywhere.

01:11 Python attaches the error being handled for you, as __context__. JavaScript doesn’t: a new error knows nothing about the one being handled, and new builds it from only what you pass.

01:22 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.

01:35 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.

01:55 Run the two-line script again. After the stack, the crash adds a block marked [cause]: the ENOENT error, with its code and path.

02:03 This one has no stack frames: it says what failed, not where.

02:09 In the report, err.cause is Python’s __cause__, set only when you pass it. This check was there all along, folded, and undefined counts as false in it, so the line never printed.

02:22 Run December’s import, and the second line prints the cause’s message. Python says Errno 2, JavaScript says ENOENT.

02:33 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.

02:44 Async errors get a course of their own later.

02:48 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.

02:57 The amount check has nothing to chain: Number() doesn’t throw. Next up, Explain Why the Settings Didn’t Load: report that hand-edited settings didn’t load and, on a second line, what JSON.parse() objected to.

Become a Member to join the conversation.