@@ -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
2525For 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`
3232file:
3333
3434``` js
35- E (' FOO' , ' Expected string received %s' );
35+ E (' FOO' , ' Expected string received %s' , TypeError );
3636```
3737
3838Then 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
4949and 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
5656The first argument passed to ` E() ` is the static identifier. The second
5757argument is either a String with optional ` util.format() ` style replacement
5858tags (e.g. ` %s ` , ` %d ` ), or a function returning a String. The optional
5959additional arguments passed to the ` errors.message() ` function (which is
6060used 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