Skip to content

Commit 0eec073

Browse files
committed
doc: update internal errors documentation
PR-URL: #19203 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 49963f4 commit 0eec073

File tree

2 files changed

+29
-68
lines changed

2 files changed

+29
-68
lines changed

CPP_STYLE_GUIDE.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,13 @@ env->SetMethod(target, "foo", Foo);
256256
exports.foo = function(str) {
257257
// Prefer doing the type-checks in JavaScript
258258
if (typeof str !== 'string') {
259-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'str', 'string');
259+
throw new errors.codes.ERR_INVALID_ARG_TYPE('str', 'string');
260260
}
261261
262262
const ctx = {};
263263
const result = binding.foo(str, ctx);
264264
if (ctx.code !== undefined) {
265-
throw new errors.Error('ERR_ERROR_NAME', ctx.code);
265+
throw new errors.codes.ERR_ERROR_NAME(ctx.code);
266266
}
267267
return result;
268268
};

doc/guides/using-internal-errors.md

+27-66
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ considered a `semver-major` change.
1919

2020
## Using internal/errors.js
2121

22-
The `internal/errors` module exposes three custom `Error` classes that
23-
are intended to replace existing `Error` objects within the Node.js source.
22+
The `internal/errors` module exposes all custom errors as subclasses of the
23+
builtin errors. After being added, an error can be found in the `codes` object.
2424

2525
For instance, an existing `Error` such as:
2626

@@ -32,15 +32,15 @@ Can be replaced by first adding a new error key into the `internal/errors.js`
3232
file:
3333

3434
```js
35-
E('FOO', 'Expected string received %s');
35+
E('FOO', 'Expected string received %s', TypeError);
3636
```
3737

3838
Then replacing the existing `new TypeError` in the code:
3939

4040
```js
41-
const errors = require('internal/errors');
41+
const { FOO } = require('internal/errors').codes;
4242
// ...
43-
const err = new errors.TypeError('FOO', type);
43+
const err = new FOO(type);
4444
```
4545

4646
## Adding new errors
@@ -49,16 +49,33 @@ New static error codes are added by modifying the `internal/errors.js` file
4949
and appending the new error codes to the end using the utility `E()` method.
5050

5151
```js
52-
E('EXAMPLE_KEY1', 'This is the error value');
53-
E('EXAMPLE_KEY2', (a, b) => `${a} ${b}`);
52+
E('EXAMPLE_KEY1', 'This is the error value', TypeError);
53+
E('EXAMPLE_KEY2', (a, b) => `${a} ${b}`, RangeError);
5454
```
5555

5656
The first argument passed to `E()` is the static identifier. The second
5757
argument is either a String with optional `util.format()` style replacement
5858
tags (e.g. `%s`, `%d`), or a function returning a String. The optional
5959
additional arguments passed to the `errors.message()` function (which is
6060
used by the `errors.Error`, `errors.TypeError` and `errors.RangeError` classes),
61-
will be used to format the error message.
61+
will be used to format the error message. The third argument is the base class
62+
that the new error will extend.
63+
64+
It is possible to create multiple derived
65+
classes by providing additional arguments. The other ones will be exposed as
66+
properties of the main class:
67+
68+
<!-- eslint-disable no-unreachable -->
69+
```js
70+
E('EXAMPLE_KEY', 'Error message', TypeError, RangeError);
71+
72+
// In another module
73+
const { EXAMPLE_KEY } = require('internal/errors').codes;
74+
// TypeError
75+
throw new EXAMPLE_KEY();
76+
// RangeError
77+
throw new EXAMPLE_KEY.RangeError();
78+
```
6279

6380
## Documenting new errors
6481

@@ -115,65 +132,9 @@ likely be required.
115132

116133
## API
117134

118-
### Class: errors.Error(key[, args...])
119-
120-
* `key` {string} The static error identifier
121-
* `args...` {any} Zero or more optional arguments
122-
123-
```js
124-
const errors = require('internal/errors');
125-
126-
const arg1 = 'foo';
127-
const arg2 = 'bar';
128-
const myError = new errors.Error('KEY', arg1, arg2);
129-
throw myError;
130-
```
131-
132-
The specific error message for the `myError` instance will depend on the
133-
associated value of `KEY` (see "Adding new errors").
134-
135-
The `myError` object will have a `code` property equal to the `key` and a
136-
`name` property equal to `` `Error [${key}]` ``.
137-
138-
### Class: errors.TypeError(key[, args...])
139-
140-
* `key` {string} The static error identifier
141-
* `args...` {any} Zero or more optional arguments
142-
143-
```js
144-
const errors = require('internal/errors');
145-
146-
const arg1 = 'foo';
147-
const arg2 = 'bar';
148-
const myError = new errors.TypeError('KEY', arg1, arg2);
149-
throw myError;
150-
```
151-
152-
The specific error message for the `myError` instance will depend on the
153-
associated value of `KEY` (see "Adding new errors").
154-
155-
The `myError` object will have a `code` property equal to the `key` and a
156-
`name` property equal to `` `TypeError [${key}]` ``.
157-
158-
### Class: errors.RangeError(key[, args...])
159-
160-
* `key` {string} The static error identifier
161-
* `args...` {any} Zero or more optional arguments
162-
163-
```js
164-
const errors = require('internal/errors');
165-
166-
const arg1 = 'foo';
167-
const arg2 = 'bar';
168-
const myError = new errors.RangeError('KEY', arg1, arg2);
169-
throw myError;
170-
```
171-
172-
The specific error message for the `myError` instance will depend on the
173-
associated value of `KEY` (see "Adding new errors").
135+
### Object: errors.codes
174136

175-
The `myError` object will have a `code` property equal to the `key` and a
176-
`name` property equal to `` `RangeError [${key}]` ``.
137+
Exposes all internal error classes to be used by Node.js APIs.
177138

178139
### Method: errors.message(key, args)
179140

0 commit comments

Comments
 (0)