lib: move extra properties into error creation · nodejs/node@bff5d30 (original) (raw)
`@@ -47,7 +47,8 @@ function lazyBuffer() {
`
47
47
`// and may have .path and .dest.
`
48
48
`class SystemError extends Error {
`
49
49
`constructor(key, context) {
`
50
``
`-
const prefix = getMessage(key, []);
`
``
50
`+
super();
`
``
51
`+
const prefix = getMessage(key, [], this);
`
51
52
`` let message = ${prefix}: ${context.syscall} returned
+
``
52
53
`` ${context.code} (${context.message})
;
``
53
54
``
`@@ -56,7 +57,12 @@ class SystemError extends Error {
`
56
57
`if (context.dest !== undefined)
`
57
58
`` message += => ${context.dest}
;
``
58
59
``
59
``
`-
super(message);
`
``
60
`+
Object.defineProperty(this, 'message', {
`
``
61
`+
value: message,
`
``
62
`+
enumerable: false,
`
``
63
`+
writable: true,
`
``
64
`+
configurable: true
`
``
65
`+
});
`
60
66
`Object.defineProperty(this, kInfo, {
`
61
67
`configurable: false,
`
62
68
`enumerable: false,
`
`@@ -150,7 +156,14 @@ let useOriginalName = false;
`
150
156
`function makeNodeErrorWithCode(Base, key) {
`
151
157
`return class NodeError extends Base {
`
152
158
`constructor(...args) {
`
153
``
`-
super(getMessage(key, args));
`
``
159
`+
super();
`
``
160
`+
const message = getMessage(key, args, this);
`
``
161
`+
Object.defineProperty(this, 'message', {
`
``
162
`+
value: message,
`
``
163
`+
enumerable: false,
`
``
164
`+
writable: true,
`
``
165
`+
configurable: true
`
``
166
`+
});
`
154
167
`}
`
155
168
``
156
169
`get name() {
`
`@@ -204,7 +217,7 @@ function E(sym, val, def, ...otherClasses) {
`
204
217
`codes[sym] = def;
`
205
218
`}
`
206
219
``
207
``
`-
function getMessage(key, args) {
`
``
220
`+
function getMessage(key, args, self) {
`
208
221
`const msg = messages.get(key);
`
209
222
``
210
223
`if (util === undefined) util = require('util');
`
`@@ -216,7 +229,7 @@ function getMessage(key, args) {
`
216
229
`` Code: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>k</mi><mi>e</mi><mi>y</mi></mrow><mo separator="true">;</mo><mi>T</mi><mi>h</mi><mi>e</mi><mi>p</mi><mi>r</mi><mi>o</mi><mi>v</mi><mi>i</mi><mi>d</mi><mi>e</mi><mi>d</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>s</mi><mi>l</mi><mi>e</mi><mi>n</mi><mi>g</mi><mi>t</mi><mi>h</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">{key}; The provided arguments length (</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="mord mathnormal" style="margin-right:0.03588em;">ey</span></span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">T</span><span class="mord mathnormal">h</span><span class="mord mathnormal">e</span><span class="mord mathnormal">p</span><span class="mord mathnormal">ro</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">i</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">gu</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal">s</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span><span class="mopen">(</span></span></span></span>{args.length}) does not
+
``
217
230
`` match the required ones (${msg.length}).
``
218
231
`);
`
219
``
`-
return msg.apply(null, args);
`
``
232
`+
return msg.apply(self, args);
`
220
233
`}
`
221
234
``
222
235
`const expectedLength = (msg.match(/%[dfijoOs]/g) || []).length;
`
`@@ -608,8 +621,10 @@ E('ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE',
`
608
621
`` 'The domain
module is in use, which is mutually exclusive with calling ' +
``
609
622
`'process.setUncaughtExceptionCaptureCallback()',
`
610
623
`Error);
`
611
``
`-
E('ERR_ENCODING_INVALID_ENCODED_DATA',
`
612
``
`-
'The encoded data was not valid for encoding %s', TypeError);
`
``
624
`+
E('ERR_ENCODING_INVALID_ENCODED_DATA', function(encoding, ret) {
`
``
625
`+
this.errno = ret;
`
``
626
`` +
return The encoded data was not valid for encoding ${encoding}
;
``
``
627
`+
}, TypeError);
`
613
628
`E('ERR_ENCODING_NOT_SUPPORTED', 'The "%s" encoding is not supported',
`
614
629
`RangeError);
`
615
630
`E('ERR_FALSY_VALUE_REJECTION', 'Promise was rejected with falsy value', Error);
`
`@@ -652,7 +667,16 @@ E('ERR_HTTP2_INVALID_PSEUDOHEADER',
`
652
667
`'"%s" is an invalid pseudoheader or is used incorrectly', TypeError);
`
653
668
`E('ERR_HTTP2_INVALID_SESSION', 'The session has been destroyed', Error);
`
654
669
`E('ERR_HTTP2_INVALID_SETTING_VALUE',
`
655
``
`-
'Invalid value for setting "%s": %s', TypeError, RangeError);
`
``
670
`+
// Using default arguments here is important so the arguments are not counted
`
``
671
`` +
// towards Function#length
.
``
``
672
`+
function(name, actual, min = undefined, max = undefined) {
`
``
673
`+
this.actual = actual;
`
``
674
`+
if (min !== undefined) {
`
``
675
`+
this.min = min;
`
``
676
`+
this.max = max;
`
``
677
`+
}
`
``
678
`` +
return Invalid value for setting "${name}": ${actual}
;
``
``
679
`+
}, TypeError, RangeError);
`
656
680
`E('ERR_HTTP2_INVALID_STREAM', 'The stream has been destroyed', Error);
`
657
681
`E('ERR_HTTP2_MAX_PENDING_SETTINGS_ACK',
`
658
682
`'Maximum number of pending settings acknowledgements', Error);
`
`@@ -685,7 +709,15 @@ E('ERR_HTTP2_SOCKET_UNBOUND',
`
685
709
`E('ERR_HTTP2_STATUS_101',
`
686
710
`'HTTP status code 101 (Switching Protocols) is forbidden in HTTP/2', Error);
`
687
711
`E('ERR_HTTP2_STATUS_INVALID', 'Invalid status code: %s', RangeError);
`
688
``
`-
E('ERR_HTTP2_STREAM_CANCEL', 'The pending stream has been canceled', Error);
`
``
712
`+
E('ERR_HTTP2_STREAM_CANCEL', function(error) {
`
``
713
`+
let msg = 'The pending stream has been canceled';
`
``
714
`+
if (error) {
`
``
715
`+
this.cause = error;
`
``
716
`+
if (typeof error.message === 'string')
`
``
717
`` +
msg += (caused by: ${error.message})
;
``
``
718
`+
}
`
``
719
`+
return msg;
`
``
720
`+
}, Error);
`
689
721
`E('ERR_HTTP2_STREAM_ERROR', 'Stream closed with error code %s', Error);
`
690
722
`E('ERR_HTTP2_STREAM_SELF_DEPENDENCY',
`
691
723
`'A stream cannot depend on itself', Error);
`
`@@ -709,7 +741,11 @@ E('ERR_INSPECTOR_CLOSED', 'Session was closed', Error);
`
709
741
`E('ERR_INSPECTOR_COMMAND', 'Inspector error %d: %s', Error);
`
710
742
`E('ERR_INSPECTOR_NOT_AVAILABLE', 'Inspector is not available', Error);
`
711
743
`E('ERR_INSPECTOR_NOT_CONNECTED', 'Session is not connected', Error);
`
712
``
`-
E('ERR_INVALID_ADDRESS_FAMILY', 'Invalid address family: %s', RangeError);
`
``
744
`+
E('ERR_INVALID_ADDRESS_FAMILY', function(addressType, host, port) {
`
``
745
`+
this.host = host;
`
``
746
`+
this.port = port;
`
``
747
`` +
return Invalid address family: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>a</mi><mi>d</mi><mi>d</mi><mi>r</mi><mi>e</mi><mi>s</mi><mi>s</mi><mi>T</mi><mi>y</mi><mi>p</mi><mi>e</mi></mrow><annotation encoding="application/x-tex">{addressType} </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">a</span><span class="mord mathnormal">dd</span><span class="mord mathnormal">ress</span><span class="mord mathnormal" style="margin-right:0.13889em;">T</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mord mathnormal">p</span><span class="mord mathnormal">e</span></span></span></span></span>{host}:${port}
;
``
``
748
`+
}, RangeError);
`
713
749
`E('ERR_INVALID_ARG_TYPE',
`
714
750
`(name, expected, actual) => {
`
715
751
`assert(typeof name === 'string', "'name' must be a string");
`
`@@ -812,7 +848,10 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
`
812
848
`E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
`
813
849
`E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
`
814
850
`E('ERR_INVALID_URI', 'URI malformed', URIError);
`
815
``
`-
E('ERR_INVALID_URL', 'Invalid URL: %s', TypeError);
`
``
851
`+
E('ERR_INVALID_URL', function(input) {
`
``
852
`+
this.input = input;
`
``
853
`` +
return Invalid URL: ${input}
;
``
``
854
`+
}, TypeError);
`
816
855
`E('ERR_INVALID_URL_SCHEME',
`
817
856
`` (expected) => The URL must be ${oneOf(expected, 'scheme')}
, TypeError);
``
818
857
`E('ERR_IPC_CHANNEL_CLOSED', 'Channel closed', Error);
`
`@@ -926,8 +965,12 @@ E('ERR_STREAM_WRAP', 'Stream has StringDecoder set or is in objectMode', Error);
`
926
965
`E('ERR_STREAM_WRITE_AFTER_END', 'write after end', Error);
`
927
966
`E('ERR_SYNTHETIC', 'JavaScript Callstack', Error);
`
928
967
`E('ERR_SYSTEM_ERROR', 'A system error occurred', SystemError);
`
929
``
`-
E('ERR_TLS_CERT_ALTNAME_INVALID',
`
930
``
`-
'Hostname/IP does not match certificate's altnames: %s', Error);
`
``
968
`+
E('ERR_TLS_CERT_ALTNAME_INVALID', function(reason, host, cert) {
`
``
969
`+
this.reason = reason;
`
``
970
`+
this.host = host;
`
``
971
`+
this.cert = cert;
`
``
972
`` +
return Hostname/IP does not match certificate's altnames: ${reason}
;
``
``
973
`+
}, Error);
`
931
974
`E('ERR_TLS_DH_PARAM_SIZE', 'DH parameter size %s is less than 2048', Error);
`
932
975
`E('ERR_TLS_HANDSHAKE_TIMEOUT', 'TLS handshake timeout', Error);
`
933
976
`E('ERR_TLS_INVALID_PROTOCOL_VERSION',
`