useCustomErrorDefinition (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useCustomErrorDefinition - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Inspired from
unicorn/custom-error-definition
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useCustomErrorDefinition": "error" } } }}Description
Section titled “Description”Enforce a consistent way of defining custom error classes.
A custom error class should have a name ending with Error, set its name property to
that name, and pass the message to super(). This way, it behaves like the built-in errors
and is easy to recognize in stack traces and logs.
When the class extends a built-in error such as Error or TypeError, its constructor
should also accept an options parameter and pass it to super(). The options carry the
cause of the error, which keeps the original failure available for debugging.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”// The `name` property isn't set, so the error shows up as `Error`.class FooError extends Error { constructor(message, options) { super(message, options); }}code-block.js:2:7 lint/nursery/useCustomErrorDefinition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The name property of this error class isn't set to "FooError".
1 │ // The `name` property isn't set, so the error shows up as `Error`.
> 2 │ class FooError extends Error {
│ ^^^^^^^^
3 │ constructor(message, options) {
4 │ super(message, options);
ℹ The name property is shown in stack traces and logs. When it isn't the class name, the error is hard to tell apart from other errors.
ℹ Set the name property to the string "FooError", either in the class body or in the constructor. Use a string rather than this.constructor.name, because tools that shrink code can rename the class.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class FooError extends Error { constructor(message, options) { super(message, options); // Tools that shrink code can rename the class. Use a string instead. this.name = this.constructor.name; }}code-block.js:5:21 lint/nursery/useCustomErrorDefinition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The name property of this error class isn't set to "FooError".
3 │ super(message, options);
4 │ // Tools that shrink code can rename the class. Use a string instead.
> 5 │ this.name = this.constructor.name;
│ ^^^^^^^^^^^^^^^^^^^^^
6 │ }
7 │ }
ℹ The name property is shown in stack traces and logs. When it isn't the class name, the error is hard to tell apart from other errors.
ℹ Set the name property to the string "FooError", either in the class body or in the constructor. Use a string rather than this.constructor.name, because tools that shrink code can rename the class.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class FooError extends Error { constructor(message, options) { // Pass the message to `super()` instead of setting `this.message`. super(undefined, options); this.message = message; this.name = "FooError"; }}code-block.js:5:9 lint/nursery/useCustomErrorDefinition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The error message is set with this.message instead of being passed to super().
3 │ // Pass the message to `super()` instead of setting `this.message`.
4 │ super(undefined, options);
> 5 │ this.message = message;
│ ^^^^^^^^^^^^^^^^^^^^^^
6 │ this.name = "FooError";
7 │ }
ℹ The Error constructor already sets the message from its first argument.
ℹ Pass the message to super() as the first argument, and remove this assignment.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
// The class name should end with `Error`.class Foo extends Error { name = "Foo";}code-block.js:2:7 lint/nursery/useCustomErrorDefinition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The name of the error class Foo doesn't start with an uppercase letter and end with Error.
1 │ // The class name should end with `Error`.
> 2 │ class Foo extends Error {
│ ^^^
3 │ name = "Foo";
4 │ }
ℹ A consistent naming convention makes error classes easy to recognize.
ℹ Rename the class to FooError.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class FooError extends Error { // The constructor should accept an `options` parameter and pass it to `super()`. constructor(message) { super(message); this.name = "FooError"; }}code-block.js:3:17 lint/nursery/useCustomErrorDefinition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This error constructor doesn't accept an options parameter.
1 │ class FooError extends Error {
2 │ // The constructor should accept an `options` parameter and pass it to `super()`.
> 3 │ constructor(message) {
│ ^^^^^^^
4 │ super(message);
5 │ this.name = "FooError";
ℹ The options object lets the code that creates the error attach the original error as its cause. This keeps the chain of failures available for debugging.
ℹ Add an options parameter, and pass it to super() as the second argument.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class FooError extends Error { constructor(message, options) { // The message passed to `super()` hides the `message` getter below. super(message, options); this.name = "FooError"; }
get message() { return "Custom message"; }}code-block.js:4:9 lint/nursery/useCustomErrorDefinition ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The error message is passed to super(), but the class defines its own message accessor.
2 │ constructor(message, options) {
3 │ // The message passed to `super()` hides the `message` getter below.
> 4 │ super(message, options);
│ ^^^^^^^^^^^^^^^^^^^^^^^
5 │ this.name = "FooError";
6 │ }
ℹ The message passed to super() is stored directly on the error, which hides the message getter and setter of the class.
ℹ Pass undefined as the first argument of super(), and store the message somewhere else, such as in a private field.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class FooError extends Error { constructor(message, options) { super(message, options); this.name = "FooError"; }}
class ApiError extends Error { constructor(status, message, options) { super(message, options); this.name = "ApiError"; this.status = status; }}
class TimeoutError extends Error { constructor(cause) { super("The request timed out", { cause }); this.name = "TimeoutError"; }}
class BarError extends TypeError { name = "BarError";}
class MessageError extends Error { #message;
constructor(message, options) { super(undefined, options); this.#message = message; this.name = "MessageError"; }
get message() { return this.#message; }}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.