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

At the start of the course, Python listed November’s bad rows, and the JavaScript port crashed with an AggregateError: 3 bad rows in november.csv. The trustees’ rule is all or nothing, every bad row in one report.

In Python, import_month() collects each BadRowError in problems and raises an ExceptionGroup, a message and then the errors:

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

JavaScript’s group is AggregateError, a built-in Error with an array named errors, where Python’s has exceptions. The raise ports as a throw of a new AggregateError:

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, Python listed November’s bad rows, and the JavaScript port crashed: an AggregateError, three bad rows, its stack cut here.

00:11 The trustees’ rule is all or nothing, every bad row in one report. You’ll find out why the port crashed. In Python, import_month() collects each BadRowError in problems and raises an ExceptionGroup: a message, then the errors.

00:29 JavaScript’s group is AggregateError, a built-in error with an array named errors, where Python’s has exceptions.

00:38 The raise ports as a throw of a new AggregateError. Watch out for the order: errors first, then the message, the other way round from ExceptionGroup.

00:48 Python’s order runs without an error but turns the message into the errors, one character each.

00:56 In main(), the JavaScript port has one catch, with an instanceof check per branch, so Python’s except* BadRowError becomes an instanceof check.

01:07 The report prints err.message, and the loop over err.errors prints each member’s message, since a whole error would add its class name.

01:17 Run the port. The group holds three BadRowErrors: is err a BadRowError, true or false? Your turn: pause the video and predict.

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

01:41 Nothing catches the group again, and JavaScript stops. Its trace runs through main() and importMonth() to the line that threw. Under [errors], in brackets, come the three BadRowErrors, with lines and donors.

01:56 This card keeps the first and cuts the rest.

02:00 Python’s except* looks inside the group, matching members by class. The instanceof check looks only at what was thrown: the AggregateError, not its members.

02:12 It works like except without the star, and JavaScript has no star: you test for the group yourself.

02:21 So test for the group by its own class. The loop stays. An AggregateError isn’t a DonationError, so it needs a branch of its own.

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

02:37 This group only holds BadRowErrors. When a group can hold other errors, test each one with instanceof. Throw the rest on in a new AggregateError, as except* does for you with the members no clause matches.

02:52 Promise.any() rejects with one as well: async JavaScript, a course of its own later.

03:00 So throw a new AggregateError, errors first, catch it by its class, and handle err.errors one by one. Next up, Check All the Pledges at Once.

03:11 You’ll check a whole batch of pledges before saving and report it as the Python does.

Become a Member to join the conversation.