Naming Your Error Classes
Joan’s December export doesn’t exist yet, so the Python import stops before it reads a row. Its report names the problem before the message, which tells Joan to export December, not to fix rows. Python takes that name from the error’s class:
except* DonationError as group:
for err in group.exceptions:
name = type(err).__name__
print(f"Nothing imported ({name}): {err}",
file=sys.stderr)
It prints Nothing imported (ExportMissingError): no export at december.csv. Its second line,
the cause, is the next section’s.
In the JavaScript port, the classes extend each other as in Python, and main()’s catch finds the
DonationError branch with instanceof. The catch gets the one error, so there’s no loop. Every
JavaScript error has a name property, and a built-in RangeError’s name is "RangeError", so
type(err).__name__ becomes err.name. The message is err.message, since Python’s f"{err}"
is only the message:
} else if (err instanceof DonationError) {
const name = err.name;
console.error(
`Nothing imported (${name}): ${err.message}`,
);
Run it, and nothing crashes, the message is right and the exit code is 1, as in Python. But the name is wrong:
$ node donations.js december.csv
Nothing imported (Error): no export at december.csv
JavaScript does know the class: the instanceof check matched it as a DonationError, and
err.constructor.name is "ExportMissingError". But name isn’t the class’s name. It’s a
property, and when the error doesn’t hold one, JavaScript falls back to the object its class
shares with every instance, its prototype, then the class above’s, then Error.prototype, the
object every error shares, where name is "Error".
A built-in RangeError’s prototype sets its own name, so its lookup stops there.
It’s like a Python instance falling back to class attributes, except JavaScript keeps them on the
prototype, a separate object, not on the class. As in Python, the class’s name isn’t on that path:
you ask the class for it, with type(err).__name__ there and err.constructor.name here.
Prototypes get a course of their own later.
Node’s own crash output reads the class: an uncaught BadRowError still prints BadRowError: line
3 (D-108): amount is not a number, with line: 3 and donor: 'D-108' after the stack, and
console.error(err) does the same. But ${err} and your report both say Error, so a test crash
won’t show you the slip.
Your report could print err.constructor.name instead, but the fix belongs where the error is
made, in the base class, once. Its constructor takes a message and options, passes both up to super()
unchanged (the next section is what the options are for), and sets this.name from
new.target.name:
class DonationError extends Error {
constructor(message, options) {
super(message, options);
this.name = new.target.name;
}
}
class ExportMissingError extends DonationError {}
Inside a constructor, new.target is the class that new was called on, like type(self) in a
Python __init__, so for a new ExportMissingError it’s ExportMissingError, even though the
constructor belongs to DonationError. A class with no constructor of its own gets one that
passes its arguments up. BadRowError’s own constructor calls super(), and its name is
"BadRowError". The report now matches the Python’s line:
$ node donations.js december.csv
Nothing imported (ExportMissingError): no export at december.csv
The error as text, ${err}, started with Error in the last lesson’s note; now it starts with
ExportMissingError. Python’s f"{err}" is still only the message, so err.message stays in the
report.
A JavaScript developer names every class once, in the program’s base class, so any error class you
add later names itself. You’ll also see name = "BadRowError"; set by hand in each class, as a
field: it works, but it has to be kept in step with the class name.
Next up is Report Each Rejected Pledge: on the food bank website’s pledge form, you’ll report each rejected pledge with its problem’s name and message, as the Python version does.