Naming Your Error Classes
For December’s missing export, the Python report names the problem, ExportMissingError, from the
error’s class, with type(err).__name__: Nothing imported (ExportMissingError): no export at
december.csv. The cause line is for the next section.
The port’s DonationError branch is an instanceof check. A built-in RangeError’s name is its
class name, so type(err).__name__ becomes err.name in the report line:
} else if (err instanceof DonationError) {
const name = err.name;
console.error(
`Nothing imported (${name}): ${err.message}`,
);
Nothing crashes, but the report no longer says which problem stopped the import:
$ node donations.js december.csv
Nothing imported (Error): no export at december.csv
Yet JavaScript knows the class: the instanceof check matched, and err.constructor.name is
"ExportMissingError". But name isn’t the class’s name: it’s a property of the error, and neither
class’s empty body sets one.
The error holds no name, so JavaScript falls back to its class’s prototype, then the class above’s,
then Error.prototype, where name is "Error". A RangeError’s own prototype sets
"RangeError". It’s like Python’s fallback to class attributes, except JavaScript keeps them on the
prototype, a separate object. As in Python, your class’s name isn’t on that path. Prototypes get a
course of their own later.
Node’s crash output reads the class itself: 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. But ${err}
and your report both say Error, so a test crash won’t show you the slip. Printing
err.constructor.name would fix only the report, so fix the base class, once.
DonationError gets a constructor that passes a message and options up to super() unchanged
(the options are for the next section), and this.name gets 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, like type(self) in a Python __init__(), new.target is the class that new
was called on. For a new ExportMissingError, that’s ExportMissingError, even though the
constructor belongs to DonationError. Run the port again, and the line names ExportMissingError,
as Python’s does:
$ node donations.js december.csv
Nothing imported (ExportMissingError): no export at december.csv
BadRowError’s own constructor calls super(), and new.target is still the class you created, so
its name is "BadRowError". The error as text now starts with ExportMissingError, not plain
Error as in the last lesson.
A JavaScript developer names every class once, in the base class, so new classes name themselves. A
hand-typed field such as name = "BadRowError"; in each class works too, if it matches the class name.
Next up: Report Each Rejected Pledge, an exercise. On the food bank website’s pledge form, you’ll report each rejected pledge’s problem by name and message, as the Python version does.