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

Porting the All-or-Nothing Import

Porting the All-or-Nothing Import

The food bank’s trustees asked for one change to Joan’s monthly import: never half-import a month. So the Python script has its own error classes, keeps the original error when it wraps a missing file, and collects every bad row into one group. Run on November’s export, it imports nothing and lists all three bad rows:

Language: Program Output
Nothing imported: 3 bad rows in november.csv
  line 3 (D-108): amount is not a number
  line 4 (D-107): expected date;donor;amount
  line 6 (D-105): amount must be positive, got -40.00

In this lesson, you ported it to JavaScript, all but main()’s two error reports:

  • The docstring becomes a JSDoc block, readFile() is the only import, and POUNDS formats the total, the job of :,.2f.
  • DonationError passes the options, where a cause travels, to super(), and this.name = new.target.name names every subclass. BadRowError sets its fields after super(), since this doesn’t exist before it, and passes the options on: unlike from, a cause rides in through the constructor.
  • catch has no class filter, so readExport() tests err.code for "ENOENT", and { cause: err } does the job of from err.
  • Number() never throws on a string: "n/a" gives NaN and "" gives 0, so parseRow() tests the result.
  • importMonth() counts from zero with .entries(), keeps each BadRowError and rethrows the rest. JavaScript’s try has no else, so Python’s else: becomes the rest of the try. An empty array counts as true in an if, so the test reads its length.
  • AggregateError takes the errors first, the reverse of ExceptionGroup, and it’s the class main()’s catch will test for. That catch only holds the place for now: it rethrows every error until the exercise adds the reports.
  • process needs no import, and process.argv[2] is Python’s sys.argv[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 The trustees’ rule for Joan’s import: never half-import a month. The Python script has its own error classes, keeps the error it wraps, and collects every bad row.

00:13 On November’s export, it imports nothing and lists three bad rows. You’ll port it to JavaScript, all but main()’s two error reports.

00:26 The docstring becomes a JSDoc block, and the only import is for reading the file. POUNDS formats the total: the job of Python’s format spec.

00:39 Everything is exported. DonationError extends Error and hands super() the message and the options, where a cause travels.

00:49 new.target.name names every subclass. Without it, the report would say Error, as you saw. ExportMissingError needs no body.

01:02 BadRowError takes the line, donor and reason, plus options. super() builds the message before the fields are set: Python’s order, required here, since this doesn’t exist until super() has run.

01:16 Unlike Python’s from, a cause rides in through the constructor, so BadRowError passes the options on.

01:25 readExport() is async, since readFile() has to be awaited. catch has no class filter, so ENOENT stands in for file not found, and the rest is rethrown.

01:39 The cause option does from err’s job. Without it, the caused-by line disappears, as you saw. parseRow() splits the row.

01:50 One without three fields throws, and destructuring skips the date. Number() never throws on a string. For n/a, it returns NaN, so the check tests the result, where Python catches a ValueError.

02:06 Nothing’s caught, so there’s no cause to pass.

02:11 The check also tests for an empty amount. Python’s float() raises for an empty string. What does Number() return for one? Your turn: pause the video and predict.

02:28 Zero. It’s finite, so without the test, an empty amount would fail the next check. That check catches D-105’s refund, and parseRow() returns the donor and amount.

02:43 importMonth() awaits the export before .slice(1) drops the header, hence the parentheses. .entries() counts from zero, so the line is the index plus two.

02:55 The catch keeps each BadRowError and rethrows the rest. JavaScript’s try has no else, so Python’s else becomes the rest of the try.

03:07 An empty array counts as true in an if, unlike Python’s, so the test reads its length. AggregateError takes the errors first, then the message.

03:19 In Python’s order, the constructor doesn’t complain, but the message becomes the errors, as you saw. main()’s catch will test for this class, not BadRowError.

03:31 Otherwise, it returns the count and total.

03:35 main() awaits the import; again, Python’s else becomes the rest of the try. For now, the catch only holds the place and rethrows every error.

03:47 The reports and exit code are the exercise’s. A template literal puts the class name in front, where Python’s f-string doesn’t, as you saw. import.meta.main does the __name__ check’s job.

04:02 process needs no import, and the export’s name is process.argv[2], where Python reads sys.argv[1].

04:10 node --check prints nothing: the syntax is fine. On the corrected export, it imports five donations, one thousand four hundred and thirty pounds.

04:24 Run the Python, and its line matches exactly.

04:30 That’s the import, the JavaScript way: one base class that names every error, the cause option for from, and an AggregateError, errors first.

04:42 Next up, the exercise Finish the All-or-Nothing Import. You’ll write main()’s reports, so November’s export, a missing December one and the corrected one each match the Python’s lines and exit codes.

Become a Member to join the conversation.