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:
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, andPOUNDSformats the total, the job of:,.2f. DonationErrorpasses the options, where a cause travels, tosuper(), andthis.name = new.target.namenames every subclass.BadRowErrorsets its fields aftersuper(), sincethisdoesn’t exist before it, and passes the options on: unlikefrom, a cause rides in through the constructor.catchhas no class filter, soreadExport()testserr.codefor"ENOENT", and{ cause: err }does the job offrom err.Number()never throws on a string:"n/a"givesNaNand""gives0, soparseRow()tests the result.importMonth()counts from zero with.entries(), keeps eachBadRowErrorand rethrows the rest. JavaScript’stryhas noelse, so Python’selse:becomes the rest of thetry. An empty array counts as true in anif, so the test reads its length.AggregateErrortakes the errors first, the reverse ofExceptionGroup, and it’s the classmain()’s catch will test for. That catch only holds the place for now: it rethrows every error until the exercise adds the reports.processneeds no import, andprocess.argv[2]is Python’ssys.argv[1].