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

Reporting Every Bad Row With AggregateError

Reporting Every Bad Row With AggregateError

The trustees’ rule is all or nothing: if any row of an export is bad, nothing is imported, and the report lists every bad row at once. So Python’s import_month() collects each BadRowError in a list, problems, and raises them together as an ExceptionGroup, a message and then the list:

Language: Python
    if problems:
        message = f"{len(problems)} bad rows in {path}"
        raise ExceptionGroup(message, problems)

main() catches the group with except* and prints the group’s message, then each bad row:

Language: Python
    except* BadRowError as group:
        print(f"Nothing imported: {group.message}")
        for err in group.exceptions:
            print(f"  {err}")

For november.csv, the script prints four lines and exits with 1:

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 At the start of the course, the JavaScript port crashed on November’s export. Its first lines name an AggregateError, three bad rows in November’s file, and the rest is cut here.

00:13 The trustees’ rule is all or nothing: a bad row means nothing is imported, and the report lists every bad row at once. You’ll port that report and find out why the port crashed.

00:26 In Python, import_month() collects the bad rows first. Each BadRowError goes into a list, problems, and the loop moves on.

00:37 If the list isn’t empty, it raises an ExceptionGroup: a message, then the errors. main() catches it with except* and prints the group’s message, then each bad row, and exits with one.

00:52 JavaScript’s group is AggregateError: a built-in error that carries an array of errors, named errors, where Python’s group has exceptions.

01:03 The raise ports as a throw of a new AggregateError. Watch out for the order: errors first, then the message, as on the line the crash pointed at.

01:12 That’s the other way round from ExceptionGroup, and Python’s order runs without an error but turns the message into the errors, one character each.

01:22 Now the report, in main(). The JavaScript port already has one catch, and an instanceof check picks the branch, as the DonationError branch below does.

01:34 So except* BadRowError ports as an instanceof check. The group’s message becomes err.message, and the loop runs over err.errors, naming each one problem, since catch already uses err.

01:50 Each prints its message: the whole error would add its class name. Then the branch sets the exit code to one.

02:00 Run the port on November’s export. The group holds three BadRowErrors, and the first branch asks whether err is a BadRowError.

02:09 True or false? Your turn: pause the video and predict.

02:19 False. The DonationError check is false too, so the last branch rethrows the group.

02:28 Nothing catches the group again, and JavaScript stops. The trace points at the line that threw it, in importMonth(), and runs through main().

02:39 Under [errors], in square brackets, come the three BadRowErrors, each with its stack, line and donor. This card shows the first, and cuts the other two from a trace of thirty-seven lines.

02:52 Python, meanwhile, prints its report.

02:57 Python’s except* looks inside the group and matches its members by class. The instanceof check looks only at what was thrown: the AggregateError.

03:08 The BadRowErrors are inside it, in err.errors. An instanceof check works like Python’s except without the star, which misses the group too.

03:20 But JavaScript has no star, so you test for the group yourself, as you’d catch ExceptionGroup itself in Python.

03:29 So test for the group by its own class. The loop over err.errors stays as it is. An AggregateError isn’t a DonationError, so the base class’s branch never catches it: the group needs a branch of its own.

03:45 Run it again, and the report matches the Python’s four lines, with exit code one.

03:52 This group only holds BadRowErrors: importMonth() pushes nothing else. When a group can hold other errors, test each one with instanceof.

04:03 And throw the rest on in a new AggregateError.

04:08 That’s what except* does with members no clause matches: it raises them again as a group. You’ll also meet AggregateError from Promise.any(), part of async JavaScript, which gets a course of its own later.

04:24 When one failure stands for many, throw a new AggregateError, errors first. Catch it by its own class, then handle err.errors one by one, with the same instanceof checks as for a single error.

04:38 Next up, Check All the Pledges at Once. The pledge form now checks a whole batch before saving, and you’ll report it as the Python does.

Become a Member to join the conversation.