The volunteer’s donations.py opens with its own exception classes. They let the report tell Joan which rows to fix, like line 3 (D-108), so nothing goes into the books until the whole month is clean. You’ll port the three classes and the except clauses that tell them apart.
Port the Exception Hierarchy
Python’s three classes, a base and two subclasses, map onto JavaScript piece by piece:
| Python | JavaScript |
|---|---|
class DonationError(Exception): |
class DonationError extends Error {} |
def __init__(self, line, donor, reason): |
constructor(line, donor, reason) { |
super().__init__(message) |
super(message); |
self.line = line |
this.line = line; |
except BadRowError as err: |
catch (err) { if (err instanceof BadRowError) ... } |
An instance of a class that extends Error is an error like any other. Python’s isinstance(err, DonationError) becomes err instanceof DonationError, which is also true for instances of every subclass. Here’s the port, which checks the message, an extra property, and instanceof for an error on line 3 of november.csv:
/** Anything that stops a donations import. */
class DonationError extends Error {}
class ExportMissingError extends DonationError {}
class BadRowError extends DonationError {
constructor(line, donor, reason) {
super(`line ${line} (${donor}): ${reason}`);
this.line = line;
this.donor = donor;
}
}
const err = new BadRowError(3, "D-108", "amount is not a number");
console.log(err.message, err.line);
console.log(err instanceof DonationError, err instanceof Error);
The error keeps its message and line, and instanceof is true for DonationError and Error:
line 3 (D-108): amount is not a number 3
true true
Braces mark a class body, so {} needs no pass, and the docstring becomes a JSDoc comment, /** ... */, above the class. A class with no constructor passes its arguments up, as in Python.
BadRowError calls super() before setting its properties, as __init__() does. In JavaScript, the order is required, since a constructor can’t use this before super().
Catch the Most Specific Class First
With plain except BadRowError and except DonationError clauses, the first match wins. The script’s main() uses except*, which a later lesson ports. JavaScript has no typed catch, so one catch (err) receives every error and the clauses become an if/else if chain of instanceof checks. The block repeats the classes so it runs on its own, and adds a readExport() that always fails:
class DonationError extends Error {}
class ExportMissingError extends DonationError {}
class BadRowError extends DonationError {
constructor(line, donor, reason) {
super(`line ${line} (${donor}): ${reason}`);
this.line = line;
this.donor = donor;
}
}
function readExport(path) {
throw new ExportMissingError(`no export at ${path}`);
}
try {
readExport("december.csv");
} catch (err) {
if (err instanceof BadRowError) {
console.log(`Nothing imported: check line ${err.line}`);
} else if (err instanceof DonationError) {
console.error(`Nothing imported: ${err.message}`);
} else {
throw err;
}
}
The ExportMissingError isn’t a BadRowError, so the second branch runs. As in the Python script, that failure report goes to standard error:
Nothing imported: no export at december.csv
Python lets an unmatched exception propagate, but JavaScript’s catch catches everything, so the else rethrows the rest. Without it, the catch would silently swallow your own bugs.
As with except clauses, put the subclass first: if you check DonationError first, every BadRowError matches it and the err.line branch never runs.
Watch out when you port f"{err}". Python gives only the message, but JavaScript converts an error with a message to text in ${err} or String(err) using its name, a colon, and its message: Nothing imported: Error: no export at december.csv. Use err.message instead. The next lesson explains why it says Error.
The report still has to name the class of the error that stopped the import, as type(err).__name__ does in Python. In the next lesson, Naming Your Error Classes, you’ll port that line with err.name and find out where JavaScript gets an error’s name from.