Skip to content

Commit 214a392

Browse files
jasnellitaloacasas
authored andcommitted
errors: add internal/errors.js
Add the internal/errors.js core mechanism. PR-URL: #11220 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent a710167 commit 214a392

File tree

5 files changed

+361
-0
lines changed

5 files changed

+361
-0
lines changed

doc/guides/using-internal-errors.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Using the internal/errors.js Module
2+
3+
## What is internal/errors.js
4+
5+
The `require('internal/errors')` module is an internal-only module that can be
6+
used to produce `Error`, `TypeError` and `RangeError` instances that use a
7+
static, permanent error code and an optionally parameterized message.
8+
9+
The intent of the module is to allow errors provided by Node.js to be assigned a
10+
permanent identifier. Without a permanent identifier, userland code may need to
11+
inspect error messages to distinguish one error from another. An unfortunate
12+
result of that practice is that changes to error messages result in broken code
13+
in the ecosystem. For that reason, Node.js has considered error message changes
14+
to be breaking changes. By providing a permanent identifier for a specific
15+
error, we reduce the need for userland code to inspect error messages.
16+
17+
*Note*: Switching an existing error to use the `internal/errors` module must be
18+
considered a `semver-major` change. However, once using `internal/errors`,
19+
changes to `internal/errors` error messages will be handled as `semver-minor`
20+
or `semver-patch`.
21+
22+
## Using internal/errors.js
23+
24+
The `internal/errors` module exposes three custom `Error` classes that
25+
are intended to replace existing `Error` objects within the Node.js source.
26+
27+
For instance, an existing `Error` such as:
28+
29+
```js
30+
var err = new TypeError('Expected string received ' + type);
31+
```
32+
33+
Can be replaced by first adding a new error key into the `internal/errors.js`
34+
file:
35+
36+
```js
37+
E('FOO', 'Expected string received %s');
38+
```
39+
40+
Then replacing the existing `new TypeError` in the code:
41+
42+
```js
43+
const errors = require('internal/errors');
44+
// ...
45+
var err = new errors.TypeError('FOO', type);
46+
```
47+
48+
## Adding new errors
49+
50+
New static error codes are added by modifying the `internal/errors.js` file
51+
and appending the new error codes to the end using the utility `E()` method.
52+
53+
```js
54+
E('EXAMPLE_KEY1', 'This is the error value');
55+
E('EXAMPLE_KEY2', (a, b) => return `${a} ${b}`);
56+
```
57+
58+
The first argument passed to `E()` is the static identifier. The second
59+
argument is either a String with optional `util.format()` style replacement
60+
tags (e.g. `%s`, `%d`), or a function returning a String. The optional
61+
additional arguments passed to the `errors.message()` function (which is
62+
used by the `errors.Error`, `errors.TypeError` and `errors.RangeError` classes),
63+
will be used to format the error message.
64+
65+
## Documenting new errors
66+
67+
Whenever a new static error code is added and used, corresponding documentation
68+
for the error code should be added to the `doc/api/errors.md` file. This will
69+
give users a place to go to easily look up the meaning of individual error
70+
codes.
71+
72+
73+
## API
74+
75+
### Class: errors.Error(key[, args...])
76+
77+
* `key` {String} The static error identifier
78+
* `args...` {Any} Zero or more optional arguments
79+
80+
```js
81+
const errors = require('internal/errors');
82+
83+
var arg1 = 'foo';
84+
var arg2 = 'bar';
85+
const myError = new errors.Error('KEY', arg1, arg2);
86+
throw myError;
87+
```
88+
89+
The specific error message for the `myError` instance will depend on the
90+
associated value of `KEY` (see "Adding new errors").
91+
92+
The `myError` object will have a `code` property equal to the `key` and a
93+
`name` property equal to `Error[${key}]`.
94+
95+
### Class: errors.TypeError(key[, args...])
96+
97+
* `key` {String} The static error identifier
98+
* `args...` {Any} Zero or more optional arguments
99+
100+
```js
101+
const errors = require('internal/errors');
102+
103+
var arg1 = 'foo';
104+
var arg2 = 'bar';
105+
const myError = new errors.TypeError('KEY', arg1, arg2);
106+
throw myError;
107+
```
108+
109+
The specific error message for the `myError` instance will depend on the
110+
associated value of `KEY` (see "Adding new errors").
111+
112+
The `myError` object will have a `code` property equal to the `key` and a
113+
`name` property equal to `TypeError[${key}]`.
114+
115+
### Class: errors.RangeError(key[, args...])
116+
117+
* `key` {String} The static error identifier
118+
* `args...` {Any} Zero or more optional arguments
119+
120+
```js
121+
const errors = require('internal/errors');
122+
123+
var arg1 = 'foo';
124+
var arg2 = 'bar';
125+
const myError = new errors.RangeError('KEY', arg1, arg2);
126+
throw myError;
127+
```
128+
129+
The specific error message for the `myError` instance will depend on the
130+
associated value of `KEY` (see "Adding new errors").
131+
132+
The `myError` object will have a `code` property equal to the `key` and a
133+
`name` property equal to `RangeError[${key}]`.
134+
135+
### Method: errors.message(key, args)
136+
137+
* `key` {String} The static error identifier
138+
* `args` {Array} Zero or more optional arguments passed as an Array
139+
* Returns: {String}
140+
141+
Returns the formatted error message string for the given `key`.

lib/internal/errors.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// The whole point behind this internal module is to allow Node.js to no
4+
// longer be forced to treat every error message change as a semver-major
5+
// change. The NodeError classes here all expose a `code` property whose
6+
// value statically and permanently identifies the error. While the error
7+
// message may change, the code should not.
8+
9+
const kCode = Symbol('code');
10+
const messages = new Map();
11+
12+
var assert, util;
13+
function lazyAssert() {
14+
if (!assert)
15+
assert = require('assert');
16+
return assert;
17+
}
18+
19+
function lazyUtil() {
20+
if (!util)
21+
util = require('util');
22+
return util;
23+
}
24+
25+
function makeNodeError(Base) {
26+
return class NodeError extends Base {
27+
constructor(key, ...args) {
28+
super(message(key, args));
29+
this[kCode] = key;
30+
Error.captureStackTrace(this, NodeError);
31+
}
32+
33+
get name() {
34+
return `${super.name}[${this[kCode]}]`;
35+
}
36+
37+
get code() {
38+
return this[kCode];
39+
}
40+
};
41+
}
42+
43+
function message(key, args) {
44+
const assert = lazyAssert();
45+
assert.strictEqual(typeof key, 'string');
46+
const util = lazyUtil();
47+
const msg = messages.get(key);
48+
assert(msg, `An invalid error message key was used: ${key}.`);
49+
let fmt = util.format;
50+
if (typeof msg === 'function') {
51+
fmt = msg;
52+
} else {
53+
if (args === undefined || args.length === 0)
54+
return msg;
55+
args.unshift(msg);
56+
}
57+
return String(fmt.apply(null, args));
58+
}
59+
60+
// Utility function for registering the error codes. Only used here. Exported
61+
// *only* to allow for testing.
62+
function E(sym, val) {
63+
messages.set(sym, typeof val === 'function' ? val : String(val));
64+
}
65+
66+
module.exports = exports = {
67+
message,
68+
Error: makeNodeError(Error),
69+
TypeError: makeNodeError(TypeError),
70+
RangeError: makeNodeError(RangeError),
71+
E // This is exported only to facilitate testing.
72+
};
73+
74+
// To declare an error message, use the E(sym, val) function above. The sym
75+
// must be an upper case string. The val can be either a function or a string.
76+
// The return value of the function must be a string.
77+
// Examples:
78+
// E('EXAMPLE_KEY1', 'This is the error value');
79+
// E('EXAMPLE_KEY2', (a, b) => return `${a} ${b}`);
80+
//
81+
// Once an error code has been assigned, the code itself MUST NOT change and
82+
// any given error code must never be reused to identify a different error.
83+
//
84+
// Any error code added here should also be added to the documentation
85+
//
86+
// Note: Please try to keep these in alphabetical order
87+
E('ERR_ASSERTION', (msg) => msg);
88+
// Add new errors from here...

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
'lib/internal/cluster/shared_handle.js',
8383
'lib/internal/cluster/utils.js',
8484
'lib/internal/cluster/worker.js',
85+
'lib/internal/errors.js',
8586
'lib/internal/freelist.js',
8687
'lib/internal/fs.js',
8788
'lib/internal/linkedlist.js',

test/common.js

+15
Original file line numberDiff line numberDiff line change
@@ -620,3 +620,18 @@ exports.WPT = {
620620
assert.fail(undefined, undefined, `Reached unreachable code: ${desc}`);
621621
}
622622
};
623+
624+
// Useful for testing expected internal/error objects
625+
exports.expectsError = function expectsError(code, type, message) {
626+
return function(error) {
627+
assert.strictEqual(error.code, code);
628+
if (type !== undefined)
629+
assert(error instanceof type, 'error is not the expected type');
630+
if (message !== undefined) {
631+
if (!util.isRegExp(message))
632+
message = new RegExp(String(message));
633+
assert(message.test(error.message), 'error.message does not match');
634+
}
635+
return true;
636+
};
637+
};

test/parallel/test-internal-errors.js

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
const common = require('../common');
5+
const errors = require('internal/errors');
6+
const assert = require('assert');
7+
8+
errors.E('TEST_ERROR_1', 'Error for testing purposes: %s');
9+
errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`);
10+
11+
const err1 = new errors.Error('TEST_ERROR_1', 'test');
12+
const err2 = new errors.TypeError('TEST_ERROR_1', 'test');
13+
const err3 = new errors.RangeError('TEST_ERROR_1', 'test');
14+
const err4 = new errors.Error('TEST_ERROR_2', 'abc', 'xyz');
15+
16+
assert(err1 instanceof Error);
17+
assert.strictEqual(err1.name, 'Error[TEST_ERROR_1]');
18+
assert.strictEqual(err1.message, 'Error for testing purposes: test');
19+
assert.strictEqual(err1.code, 'TEST_ERROR_1');
20+
21+
assert(err2 instanceof TypeError);
22+
assert.strictEqual(err2.name, 'TypeError[TEST_ERROR_1]');
23+
assert.strictEqual(err2.message, 'Error for testing purposes: test');
24+
assert.strictEqual(err2.code, 'TEST_ERROR_1');
25+
26+
assert(err3 instanceof RangeError);
27+
assert.strictEqual(err3.name, 'RangeError[TEST_ERROR_1]');
28+
assert.strictEqual(err3.message, 'Error for testing purposes: test');
29+
assert.strictEqual(err3.code, 'TEST_ERROR_1');
30+
31+
assert(err4 instanceof Error);
32+
assert.strictEqual(err4.name, 'Error[TEST_ERROR_2]');
33+
assert.strictEqual(err4.message, 'abc xyz');
34+
assert.strictEqual(err4.code, 'TEST_ERROR_2');
35+
36+
assert.throws(
37+
() => new errors.Error('TEST_FOO_KEY'),
38+
/^AssertionError: An invalid error message key was used: TEST_FOO_KEY.$/);
39+
// Calling it twice yields same result (using the key does not create it)
40+
assert.throws(
41+
() => new errors.Error('TEST_FOO_KEY'),
42+
/^AssertionError: An invalid error message key was used: TEST_FOO_KEY.$/);
43+
assert.throws(
44+
() => new errors.Error(1),
45+
/^AssertionError: 'number' === 'string'$/);
46+
assert.throws(
47+
() => new errors.Error({}),
48+
/^AssertionError: 'object' === 'string'$/);
49+
assert.throws(
50+
() => new errors.Error([]),
51+
/^AssertionError: 'object' === 'string'$/);
52+
assert.throws(
53+
() => new errors.Error(true),
54+
/^AssertionError: 'boolean' === 'string'$/);
55+
assert.throws(
56+
() => new errors.TypeError(1),
57+
/^AssertionError: 'number' === 'string'$/);
58+
assert.throws(
59+
() => new errors.TypeError({}),
60+
/^AssertionError: 'object' === 'string'$/);
61+
assert.throws(
62+
() => new errors.TypeError([]),
63+
/^AssertionError: 'object' === 'string'$/);
64+
assert.throws(
65+
() => new errors.TypeError(true),
66+
/^AssertionError: 'boolean' === 'string'$/);
67+
assert.throws(
68+
() => new errors.RangeError(1),
69+
/^AssertionError: 'number' === 'string'$/);
70+
assert.throws(
71+
() => new errors.RangeError({}),
72+
/^AssertionError: 'object' === 'string'$/);
73+
assert.throws(
74+
() => new errors.RangeError([]),
75+
/^AssertionError: 'object' === 'string'$/);
76+
assert.throws(
77+
() => new errors.RangeError(true),
78+
/^AssertionError: 'boolean' === 'string'$/);
79+
80+
81+
// Tests for common.expectsError
82+
assert.doesNotThrow(() => {
83+
assert.throws(() => {
84+
throw new errors.TypeError('TEST_ERROR_1', 'a');
85+
}, common.expectsError('TEST_ERROR_1'));
86+
});
87+
88+
assert.doesNotThrow(() => {
89+
assert.throws(() => {
90+
throw new errors.TypeError('TEST_ERROR_1', 'a');
91+
}, common.expectsError('TEST_ERROR_1', TypeError, /^Error for testing/));
92+
});
93+
94+
assert.doesNotThrow(() => {
95+
assert.throws(() => {
96+
throw new errors.TypeError('TEST_ERROR_1', 'a');
97+
}, common.expectsError('TEST_ERROR_1', TypeError));
98+
});
99+
100+
assert.doesNotThrow(() => {
101+
assert.throws(() => {
102+
throw new errors.TypeError('TEST_ERROR_1', 'a');
103+
}, common.expectsError('TEST_ERROR_1', Error));
104+
});
105+
106+
assert.throws(() => {
107+
assert.throws(() => {
108+
throw new errors.TypeError('TEST_ERROR_1', 'a');
109+
}, common.expectsError('TEST_ERROR_1', RangeError));
110+
}, /^AssertionError: error is not the expected type/);
111+
112+
assert.throws(() => {
113+
assert.throws(() => {
114+
throw new errors.TypeError('TEST_ERROR_1', 'a');
115+
}, common.expectsError('TEST_ERROR_1', TypeError, /^Error for testing 2/));
116+
}, /^AssertionError: error.message does not match/);

0 commit comments

Comments
 (0)