Loading video player…

Naming Your Error Classes

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:

Language: JavaScript
    } 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:

Language: Shell
$ 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:

Language: JavaScript
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:

Language: Shell
$ 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.

00:00 For December’s missing export, the Python report names ExportMissingError, with type(err).__name__. The cause line is for the next section.

00:10 Here, you port that line with err.name and find out where JavaScript gets an error’s name from.

00:18 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.

00:32 Run the port on December’s export. What name does its line print in the parentheses? Your turn: pause the video and predict.

00:45 Error. Not ExportMissingError. Nothing crashed, but the report no longer says which problem stopped the import. Yet JavaScript knows the class: the instanceof check matched, and err.constructor.name is ExportMissingError.

01:06 But name isn’t the class’s name: it’s a property of the error, and neither class’s empty body sets one.

01:14 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.

01:24 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.

01:35 As in Python, your class’s name isn’t on that path. Prototypes get a course of their own later.

01:43 Node’s crash output reads the class itself: an uncaught BadRowError still says BadRowError, fields and all. But the error as text and your report both say Error, so a test crash won’t show you the slip.

01:57 Printing the constructor’s name would fix only the report, so fix the base class, once.

02:05 DonationError gets a constructor that passes a message and options up to super() unchanged. The options are for the next section.

02:13 this.name gets new.target.name as its value. Inside a constructor, like type(self) in a Python .__init__(), new.target is the class that new was called on.

02:25 For a new ExportMissingError, that’s ExportMissingError, even though the constructor belongs to DonationError.

02:35 Run the port again, and the line names ExportMissingError, as Python’s does. BadRowError’s own constructor calls super(), and new.target is still the class you created, so its name is BadRowError.

02:49 The error as text now starts with ExportMissingError, not plain Error as in the last lesson.

02:57 A JavaScript developer names every class once, in the base class, so new classes name themselves. A hand-typed name field works too, if it matches the class name.

03:07 Next up, Report Each Rejected Pledge: on the food bank’s pledge form, you’ll report each rejected pledge’s problem by name and message, as the Python does.

Become a Member to join the conversation.