@@ -19,8 +19,8 @@ considered a `semver-major` change.
19
19
20
20
## Using internal/errors.js
21
21
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 .
24
24
25
25
For instance, an existing ` Error ` such as:
26
26
@@ -32,15 +32,15 @@ Can be replaced by first adding a new error key into the `internal/errors.js`
32
32
file:
33
33
34
34
``` js
35
- E (' FOO' , ' Expected string received %s' );
35
+ E (' FOO' , ' Expected string received %s' , TypeError );
36
36
```
37
37
38
38
Then replacing the existing ` new TypeError ` in the code:
39
39
40
40
``` js
41
- const errors = require (' internal/errors' );
41
+ const { FOO } = require (' internal/errors' ). codes ;
42
42
// ...
43
- const err = new errors.TypeError ( ' FOO' , type);
43
+ const err = new FOO ( type);
44
44
```
45
45
46
46
## Adding new errors
@@ -49,16 +49,33 @@ New static error codes are added by modifying the `internal/errors.js` file
49
49
and appending the new error codes to the end using the utility ` E() ` method.
50
50
51
51
``` 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 );
54
54
```
55
55
56
56
The first argument passed to ` E() ` is the static identifier. The second
57
57
argument is either a String with optional ` util.format() ` style replacement
58
58
tags (e.g. ` %s ` , ` %d ` ), or a function returning a String. The optional
59
59
additional arguments passed to the ` errors.message() ` function (which is
60
60
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
+ ```
62
79
63
80
## Documenting new errors
64
81
@@ -115,65 +132,9 @@ likely be required.
115
132
116
133
## API
117
134
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
174
136
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.
177
138
178
139
### Method: errors.message(key, args)
179
140
0 commit comments