Skip to content

Commit b89d817

Browse files
BridgeARMylesBorins
authored andcommittedMay 4, 2018
errors: minor (SystemError) refactoring
This removes the former default values and the spread arguments usage. That was unnecessary and now it does only what is necessary. The `message` function got renamed to `getMessage` to outline that it is actually a function and a helper function was inlined into the SystemError constructor as it was only used there. PR-URL: #20337 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 58be6ef commit b89d817

File tree

4 files changed

+46
-57
lines changed

4 files changed

+46
-57
lines changed
 

‎lib/internal/errors.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@ function inspectValue(val) {
7979
).split('\n');
8080
}
8181

82-
function sysErrorMessage(prefix, ctx) {
83-
let message = `${prefix}: ${ctx.syscall} returned ` +
84-
`${ctx.code} (${ctx.message})`;
85-
if (ctx.path !== undefined)
86-
message += ` ${ctx.path}`;
87-
if (ctx.dest !== undefined)
88-
message += ` => ${ctx.dest}`;
89-
return message;
90-
}
91-
9282
// A specialized Error that includes an additional info property with
9383
// additional information about the error condition.
9484
// It has the properties present in a UVException but with a custom error
@@ -98,9 +88,17 @@ function sysErrorMessage(prefix, ctx) {
9888
// The context passed into this error must have .code, .syscall and .message,
9989
// and may have .path and .dest.
10090
class SystemError extends Error {
101-
constructor(key, context = {}) {
102-
context = context || {};
103-
super(sysErrorMessage(message(key), context));
91+
constructor(key, context) {
92+
const prefix = getMessage(key, []);
93+
let message = `${prefix}: ${context.syscall} returned ` +
94+
`${context.code} (${context.message})`;
95+
96+
if (context.path !== undefined)
97+
message += ` ${context.path}`;
98+
if (context.dest !== undefined)
99+
message += ` => ${context.dest}`;
100+
101+
super(message);
104102
Object.defineProperty(this, kInfo, {
105103
configurable: false,
106104
enumerable: false,
@@ -183,16 +181,16 @@ class SystemError extends Error {
183181

184182
function makeSystemErrorWithCode(key) {
185183
return class NodeError extends SystemError {
186-
constructor(...args) {
187-
super(key, ...args);
184+
constructor(ctx) {
185+
super(key, ctx);
188186
}
189187
};
190188
}
191189

192190
function makeNodeErrorWithCode(Base, key) {
193191
return class NodeError extends Base {
194192
constructor(...args) {
195-
super(message(key, args));
193+
super(getMessage(key, args));
196194
}
197195

198196
get name() {
@@ -485,7 +483,7 @@ function internalAssert(condition, message) {
485483
}
486484
}
487485

488-
function message(key, args = []) {
486+
function getMessage(key, args) {
489487
const msg = messages.get(key);
490488
if (util === undefined) util = require('util');
491489

@@ -694,7 +692,7 @@ module.exports = {
694692
exceptionWithHostPort,
695693
uvException,
696694
isStackOverflowError,
697-
message,
695+
getMessage,
698696
AssertionError,
699697
SystemError,
700698
codes,

‎test/parallel/test-buffer-fill.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33
const common = require('../common');
44
const assert = require('assert');
5-
const errors = require('internal/errors');
5+
const { codes: { ERR_INDEX_OUT_OF_RANGE } } = require('internal/errors');
66
const SIZE = 28;
77

88
const buf1 = Buffer.allocUnsafe(SIZE);
@@ -214,13 +214,11 @@ function genBuffer(size, args) {
214214
return b.fill(0).fill.apply(b, args);
215215
}
216216

217-
218217
function bufReset() {
219218
buf1.fill(0);
220219
buf2.fill(0);
221220
}
222221

223-
224222
// This is mostly accurate. Except write() won't write partial bytes to the
225223
// string while fill() blindly copies bytes into memory. To account for that an
226224
// error will be thrown if not all the data can be written, and the SIZE has
@@ -237,8 +235,9 @@ function writeToFill(string, offset, end, encoding) {
237235
end = buf2.length;
238236
}
239237

238+
// Should never be reached.
240239
if (offset < 0 || end > buf2.length)
241-
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
240+
throw new ERR_INDEX_OUT_OF_RANGE();
242241

243242
if (end <= offset)
244243
return buf2;
@@ -266,7 +265,6 @@ function writeToFill(string, offset, end, encoding) {
266265
return buf2;
267266
}
268267

269-
270268
function testBufs(string, offset, length, encoding) {
271269
bufReset();
272270
buf1.fill.apply(buf1, arguments);

‎test/parallel/test-errors-systemerror.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33

44
require('../common');
55
const assert = require('assert');
6-
const errors = require('internal/errors');
7-
8-
const { E, SystemError } = errors;
6+
const { E, SystemError, codes } = require('internal/errors');
97

108
assert.throws(
11-
() => { throw new errors.SystemError(); },
9+
() => { throw new SystemError(); },
1210
{
1311
name: 'TypeError',
1412
message: 'Cannot read property \'match\' of undefined'
1513
}
1614
);
1715

1816
E('ERR_TEST', 'custom message', SystemError);
19-
const { ERR_TEST } = errors.codes;
17+
const { ERR_TEST } = codes;
2018

2119
{
2220
const ctx = {

‎test/parallel/test-internal-errors.js

+24-29
Original file line numberDiff line numberDiff line change
@@ -93,100 +93,95 @@ common.expectsError(() => {
9393
});
9494

9595
// Test ERR_INVALID_FD_TYPE
96-
assert.strictEqual(errors.message('ERR_INVALID_FD_TYPE', ['a']),
96+
assert.strictEqual(errors.getMessage('ERR_INVALID_FD_TYPE', ['a']),
9797
'Unsupported fd type: a');
9898

9999
// Test ERR_INVALID_URL_SCHEME
100-
assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', ['file']),
100+
assert.strictEqual(errors.getMessage('ERR_INVALID_URL_SCHEME', ['file']),
101101
'The URL must be of scheme file');
102-
assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', [['file']]),
102+
assert.strictEqual(errors.getMessage('ERR_INVALID_URL_SCHEME', [['file']]),
103103
'The URL must be of scheme file');
104-
assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', [['http', 'ftp']]),
104+
assert.strictEqual(errors.getMessage('ERR_INVALID_URL_SCHEME',
105+
[['http', 'ftp']]),
105106
'The URL must be one of scheme http or ftp');
106-
assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', [['a', 'b', 'c']]),
107+
assert.strictEqual(errors.getMessage('ERR_INVALID_URL_SCHEME',
108+
[['a', 'b', 'c']]),
107109
'The URL must be one of scheme a, b, or c');
108110
common.expectsError(
109-
() => errors.message('ERR_INVALID_URL_SCHEME', [[]]),
111+
() => errors.getMessage('ERR_INVALID_URL_SCHEME', [[]]),
110112
{
111113
code: 'ERR_ASSERTION',
112114
type: assert.AssertionError,
113115
message: /^At least one expected value needs to be specified$/
114116
});
115117

116118
// Test ERR_MISSING_ARGS
117-
assert.strictEqual(errors.message('ERR_MISSING_ARGS', ['name']),
119+
assert.strictEqual(errors.getMessage('ERR_MISSING_ARGS', ['name']),
118120
'The "name" argument must be specified');
119-
assert.strictEqual(errors.message('ERR_MISSING_ARGS', ['name', 'value']),
121+
assert.strictEqual(errors.getMessage('ERR_MISSING_ARGS', ['name', 'value']),
120122
'The "name" and "value" arguments must be specified');
121-
assert.strictEqual(errors.message('ERR_MISSING_ARGS', ['a', 'b', 'c']),
123+
assert.strictEqual(errors.getMessage('ERR_MISSING_ARGS', ['a', 'b', 'c']),
122124
'The "a", "b", and "c" arguments must be specified');
123-
common.expectsError(
124-
() => errors.message('ERR_MISSING_ARGS'),
125-
{
126-
code: 'ERR_ASSERTION',
127-
type: assert.AssertionError,
128-
message: /^At least one arg needs to be specified$/
129-
});
130125

131126
// Test ERR_SOCKET_BAD_PORT
132127
assert.strictEqual(
133-
errors.message('ERR_SOCKET_BAD_PORT', [0]),
128+
errors.getMessage('ERR_SOCKET_BAD_PORT', [0]),
134129
'Port should be > 0 and < 65536. Received 0.');
135130

136131
// Test ERR_TLS_CERT_ALTNAME_INVALID
137132
assert.strictEqual(
138-
errors.message('ERR_TLS_CERT_ALTNAME_INVALID', ['altname']),
133+
errors.getMessage('ERR_TLS_CERT_ALTNAME_INVALID', ['altname']),
139134
'Hostname/IP does not match certificate\'s altnames: altname');
140135

141136
assert.strictEqual(
142-
errors.message('ERR_INVALID_PROTOCOL', ['bad protocol', 'http']),
137+
errors.getMessage('ERR_INVALID_PROTOCOL', ['bad protocol', 'http']),
143138
'Protocol "bad protocol" not supported. Expected "http"'
144139
);
145140

146141
assert.strictEqual(
147-
errors.message('ERR_HTTP_HEADERS_SENT', ['render']),
142+
errors.getMessage('ERR_HTTP_HEADERS_SENT', ['render']),
148143
'Cannot render headers after they are sent to the client'
149144
);
150145

151146
assert.strictEqual(
152-
errors.message('ERR_INVALID_DOMAIN_NAME'),
147+
errors.getMessage('ERR_INVALID_DOMAIN_NAME', []),
153148
'Unable to determine the domain name'
154149
);
155150

156151
assert.strictEqual(
157-
errors.message('ERR_INVALID_HTTP_TOKEN', ['Method', 'foo']),
152+
errors.getMessage('ERR_INVALID_HTTP_TOKEN', ['Method', 'foo']),
158153
'Method must be a valid HTTP token ["foo"]'
159154
);
160155

161156
assert.strictEqual(
162-
errors.message('ERR_OUT_OF_RANGE', ['A', 'some values', 'B']),
157+
errors.getMessage('ERR_OUT_OF_RANGE', ['A', 'some values', 'B']),
163158
'The value of "A" is out of range. It must be some values. Received B'
164159
);
165160

166161
assert.strictEqual(
167-
errors.message('ERR_UNESCAPED_CHARACTERS', ['Request path']),
162+
errors.getMessage('ERR_UNESCAPED_CHARACTERS', ['Request path']),
168163
'Request path contains unescaped characters'
169164
);
170165

171166
// Test ERR_DNS_SET_SERVERS_FAILED
172167
assert.strictEqual(
173-
errors.message('ERR_DNS_SET_SERVERS_FAILED', ['err', 'servers']),
168+
errors.getMessage('ERR_DNS_SET_SERVERS_FAILED', ['err', 'servers']),
174169
'c-ares failed to set servers: "err" [servers]');
175170

176171
// Test ERR_ENCODING_NOT_SUPPORTED
177172
assert.strictEqual(
178-
errors.message('ERR_ENCODING_NOT_SUPPORTED', ['enc']),
173+
errors.getMessage('ERR_ENCODING_NOT_SUPPORTED', ['enc']),
179174
'The "enc" encoding is not supported');
180175

181176
// Test error messages for async_hooks
182177
assert.strictEqual(
183-
errors.message('ERR_ASYNC_CALLBACK', ['init']),
178+
errors.getMessage('ERR_ASYNC_CALLBACK', ['init']),
184179
'init must be a function');
185180
assert.strictEqual(
186-
errors.message('ERR_ASYNC_TYPE', [{}]),
181+
errors.getMessage('ERR_ASYNC_TYPE', [{}]),
187182
'Invalid name for async "type": [object Object]');
188183
assert.strictEqual(
189-
errors.message('ERR_INVALID_ASYNC_ID', ['asyncId', undefined]),
184+
errors.getMessage('ERR_INVALID_ASYNC_ID', ['asyncId', undefined]),
190185
'Invalid asyncId value: undefined');
191186

192187
{

0 commit comments

Comments
 (0)