★ wanayoo — archive 1999 https://github.com/nodejs/node/commit/1ed3c54ecbNouvelle recherche | Portail wanayoo
Skip to content
Permalink
Browse files
errors: update error name
This updates all Node.js errors by removing the `code` being part
of the `name` property. Instead, the name is just changed once on
instantiation, the stack is accessed to create the stack as expected
and then the `name` property is set back to it's original form.

PR-URL: #26738
Fixes: #26669
Fixes: #20253
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
BridgeAR committed Mar 23, 2019
1 parent c757cb1 commit 1ed3c54ecbd72a33693e5954f86bcc9fd9b1cc09
Showing with 283 additions and 284 deletions.
  1. +14 −1 lib/internal/assert/assertion_error.js
  2. +34 −31 lib/internal/errors.js
  3. +6 −0 lib/internal/http2/util.js
  4. +3 −3 test/parallel/test-assert-async.js
  5. +12 −12 test/parallel/test-assert-deep.js
  6. +3 −3 test/parallel/test-assert-fail-deprecation.js
  7. +2 −2 test/parallel/test-assert-fail.js
  8. +2 −2 test/parallel/test-assert-first-line.js
  9. +15 −15 test/parallel/test-assert.js
  10. +2 −2 test/parallel/test-buffer-alloc.js
  11. +7 −7 test/parallel/test-buffer-arraybuffer.js
  12. +2 −2 test/parallel/test-buffer-read.js
  13. +3 −3 test/parallel/test-buffer-readdouble.js
  14. +3 −3 test/parallel/test-buffer-readfloat.js
  15. +7 −7 test/parallel/test-buffer-readint.js
  16. +7 −7 test/parallel/test-buffer-readuint.js
  17. +2 −2 test/parallel/test-buffer-slow.js
  18. +3 −3 test/parallel/test-buffer-writedouble.js
  19. +3 −3 test/parallel/test-buffer-writefloat.js
  20. +5 −5 test/parallel/test-buffer-writeint.js
  21. +5 −5 test/parallel/test-buffer-writeuint.js
  22. +3 −3 test/parallel/test-child-process-fork.js
  23. +15 −15 test/parallel/test-crypto-pbkdf2.js
  24. +6 −6 test/parallel/test-crypto-random.js
  25. +2 −2 test/parallel/test-crypto-sign-verify.js
  26. +1 −1 test/parallel/test-dgram-send-address-types.js
  27. +1 −1 test/parallel/test-dgram-sendto.js
  28. +2 −2 test/parallel/test-dns-setservers-type-check.js
  29. +1 −1 test/parallel/test-dns.js
  30. +1 −1 test/parallel/test-error-serdes.js
  31. +6 −6 test/parallel/test-errors-systemerror.js
  32. +1 −1 test/parallel/test-fs-chmod.js
  33. +1 −1 test/parallel/test-fs-close-errors.js
  34. +6 −6 test/parallel/test-fs-fchmod.js
  35. +3 −3 test/parallel/test-fs-fchown.js
  36. +1 −1 test/parallel/test-fs-fsync.js
  37. +2 −2 test/parallel/test-fs-lchmod.js
  38. +1 −1 test/parallel/test-fs-open.js
  39. +4 −4 test/parallel/test-fs-promises.js
  40. +8 −8 test/parallel/test-fs-read-type.js
  41. +4 −4 test/parallel/test-fs-rename-type-check.js
  42. +5 −5 test/parallel/test-fs-stat.js
  43. +2 −2 test/parallel/test-fs-symlink.js
  44. +6 −6 test/parallel/test-fs-truncate.js
  45. +1 −1 test/parallel/test-http-res-write-end-dont-take-array.js
  46. +6 −6 test/parallel/test-http2-altsvc.js
  47. +1 −1 test/parallel/test-http2-client-http1-server.js
  48. +1 −1 test/parallel/test-http2-client-onconnect-errors.js
  49. +1 −1 test/parallel/test-http2-client-rststream-before-connect.js
  50. +2 −2 test/parallel/test-http2-compat-serverrequest-headers.js
  51. +1 −1 test/parallel/test-http2-compat-serverresponse-headers.js
  52. +1 −1 test/parallel/test-http2-createsecureserver-nooptions.js
  53. +1 −1 test/parallel/test-http2-info-headers-errors.js
  54. +4 −4 test/parallel/test-http2-origin.js
  55. +1 −1 test/parallel/test-http2-respond-nghttperrors.js
  56. +1 −1 test/parallel/test-http2-respond-with-fd-errors.js
  57. +1 −1 test/parallel/test-http2-server-push-stream-errors-args.js
  58. +1 −1 test/parallel/test-http2-server-push-stream-errors.js
  59. +3 −3 test/parallel/test-http2-util-headers-list.js
  60. +3 −3 test/parallel/test-https-options-boolean-check.js
  61. +2 −2 test/parallel/test-internal-error-original-names.js
  62. +5 −26 test/parallel/test-internal-errors.js
  63. +1 −1 test/parallel/test-next-tick-errors.js
  64. +5 −5 test/parallel/test-process-cpuUsage.js
  65. +2 −2 test/parallel/test-process-initgroups.js
  66. +1 −1 test/parallel/test-process-kill-pid.js
  67. +3 −3 test/parallel/test-process-setgroups.js
  68. +3 −3 test/parallel/test-stream-finished.js
  69. +4 −4 test/parallel/test-ttywrap-invalid-fd.js
  70. +1 −1 test/parallel/test-url-format-whatwg.js
  71. +5 −7 test/parallel/test-whatwg-url-custom-parsing.js
@@ -388,12 +388,25 @@ class AssertionError extends Error {
}

this.generatedMessage = !message;
this.name = 'AssertionError [ERR_ASSERTION]';
Object.defineProperty(this, 'name', {
value: 'AssertionError [ERR_ASSERTION]',
enumerable: false,
writable: true,
configurable: true
});
this.code = 'ERR_ASSERTION';
this.actual = actual;
this.expected = expected;
this.operator = operator;
Error.captureStackTrace(this, stackStartFn);
// Create error message including the error code in the name.
this.stack;
// Reset the name.
this.name = 'AssertionError';
}

toString() {
return `${this.name} [${this.code}]: ${this.message}`;
}

[inspect.custom](recurseTimes, ctx) {
@@ -18,6 +18,8 @@ const codes = {};
const { kMaxLength } = internalBinding('buffer');
const { defineProperty } = Object;

let useOriginalName = false;

// Lazily loaded
let util;
let assert;
@@ -74,19 +76,7 @@ class SystemError extends Error {
value: key,
writable: true
});
}

get name() {
return `SystemError [${this[kCode]}]`;
}

set name(value) {
defineProperty(this, 'name', {
configurable: true,
enumerable: true,
value,
writable: true
});
addCodeToName(this, 'SystemError', key);
}

get code() {
@@ -141,6 +131,10 @@ class SystemError extends Error {
this[kInfo].dest = val ?
lazyBuffer().from(val.toString()) : undefined;
}

toString() {
return `${this.name} [${this.code}]: ${this.message}`;
}
}

function makeSystemErrorWithCode(key) {
@@ -151,8 +145,6 @@ function makeSystemErrorWithCode(key) {
};
}

let useOriginalName = false;

function makeNodeErrorWithCode(Base, key) {
return class NodeError extends Base {
constructor(...args) {
@@ -164,22 +156,7 @@ function makeNodeErrorWithCode(Base, key) {
writable: true,
configurable: true
});
}

get name() {
if (useOriginalName) {
return super.name;
}
return `${super.name} [${key}]`;
}

set name(value) {
defineProperty(this, 'name', {
configurable: true,
enumerable: true,
value,
writable: true
});
addCodeToName(this, super.name, key);
}

get code() {
@@ -194,9 +171,35 @@ function makeNodeErrorWithCode(Base, key) {
writable: true
});
}

toString() {
return `${this.name} [${key}]: ${this.message}`;
}
};
}

function addCodeToName(err, name, code) {
if (useOriginalName) {
return;
}
// Add the error code to the name to include it in the stack trace.
err.name = `${name} [${code}]`;
// Access the stack to generate the error message including the error code
// from the name.
err.stack;
// Reset the name to the actual name.
if (name === 'SystemError') {
defineProperty(err, 'name', {
value: name,
enumerable: false,
writable: true,
configurable: true
});
} else {
delete err.name;
}
}

// Utility function for registering the error codes. Only used here. Exported
// *only* to allow for testing.
function E(sym, val, def, ...otherClasses) {
@@ -498,6 +498,12 @@ class NghttpError extends Error {
this.code = 'ERR_HTTP2_ERROR';
this.name = 'Error [ERR_HTTP2_ERROR]';
this.errno = ret;
this.stack;
delete this.name;
}

toString() {
return `${this.name} [${this.code}]: ${this.message}`;
}
}

@@ -13,7 +13,7 @@ const promises = [];
const rejectingFn = async () => assert.fail();
const errObj = {
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: 'Failed'
};
// `assert.rejects` accepts a function or a promise as first argument.
@@ -38,7 +38,7 @@ const promises = [];

promise = assert.rejects(() => {}, common.mustNotCall());
promises.push(assert.rejects(promise, {
name: 'TypeError [ERR_INVALID_RETURN_VALUE]',
name: 'TypeError',
code: 'ERR_INVALID_RETURN_VALUE',
message: 'Expected instance of Promise to be returned ' +
'from the "promiseFn" function but got type undefined.'
@@ -75,7 +75,7 @@ promises.push(assert.rejects(
message: 'Expected instance of Promise to be returned ' +
'from the "promiseFn" function but got instance of Map.',
code: 'ERR_INVALID_RETURN_VALUE',
name: 'TypeError [ERR_INVALID_RETURN_VALUE]'
name: 'TypeError'
}));
promises.push(assert.doesNotReject(async () => {}));
promises.push(assert.doesNotReject(Promise.resolve()));
@@ -778,7 +778,7 @@ assert.throws(
assert.throws(
() => assert.notDeepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)),
{
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: 'Expected "actual" not to be strictly deep-equal to: ' +
util.inspect(new Date(2000, 3, 14))
}
@@ -790,35 +790,35 @@ assert.throws(
() => assert.deepStrictEqual(/ab/, /a/),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: `${defaultMsgStartFull}\n\n+ /ab/\n- /a/`
});
assert.throws(
() => assert.deepStrictEqual(/a/g, /a/),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: `${defaultMsgStartFull}\n\n+ /a/g\n- /a/`
});
assert.throws(
() => assert.deepStrictEqual(/a/i, /a/),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: `${defaultMsgStartFull}\n\n+ /a/i\n- /a/`
});
assert.throws(
() => assert.deepStrictEqual(/a/m, /a/),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: `${defaultMsgStartFull}\n\n+ /a/m\n- /a/`
});
assert.throws(
() => assert.deepStrictEqual(/a/igm, /a/im),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: `${defaultMsgStartFull}\n\n+ /a/gim\n- /a/im\n ^`
});

@@ -844,22 +844,22 @@ assert.deepStrictEqual({ a: 4, b: '2' }, { a: 4, b: '2' });
assert.throws(() => assert.deepStrictEqual([4], ['4']),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: `${defaultMsgStartFull}\n\n [\n+ 4\n- '4'\n ]`
});
assert.throws(
() => assert.deepStrictEqual({ a: 4 }, { a: 4, b: true }),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: `${defaultMsgStartFull}\n\n ` +
'{\n a: 4,\n- b: true\n }'
});
assert.throws(
() => assert.deepStrictEqual(['a'], { 0: 'a' }),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: `${defaultMsgStartFull}\n\n` +
"+ [\n+ 'a'\n+ ]\n- {\n- '0': 'a'\n- }"
});
@@ -953,7 +953,7 @@ assert.deepStrictEqual(obj1, obj2);
() => assert.deepStrictEqual(a, b),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: /\.\.\./g
}
);
@@ -977,7 +977,7 @@ assert.throws(
() => assert.deepStrictEqual([1, 2, 3], [1, 2]),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: `${defaultMsgStartFull}\n\n` +
' [\n' +
' 1,\n' +
@@ -1063,7 +1063,7 @@ assert.throws(
() => assert.deepStrictEqual(a, b),
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: /a: \[Getter: 5]\n- a: \[Getter: 6]\n /
}
);
@@ -15,7 +15,7 @@ assert.throws(() => {
assert.fail('first', 'second');
}, {
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: '\'first\' != \'second\'',
operator: '!=',
actual: 'first',
@@ -28,7 +28,7 @@ assert.throws(() => {
assert.fail('ignored', 'ignored', 'another custom message');
}, {
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: 'another custom message',
operator: 'fail',
actual: 'ignored',
@@ -49,7 +49,7 @@ assert.throws(() => {
assert.fail('first', 'second', undefined, 'operator');
}, {
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: '\'first\' operator \'second\'',
operator: 'operator',
actual: 'first',
@@ -8,7 +8,7 @@ assert.throws(
() => { assert.fail(); },
{
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: 'Failed',
operator: 'fail',
actual: undefined,
@@ -22,7 +22,7 @@ assert.throws(() => {
assert.fail('custom message');
}, {
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: 'custom message',
operator: 'fail',
actual: undefined,
@@ -9,15 +9,15 @@ const { path } = require('../common/fixtures');
assert.throws(
() => require(path('assert-first-line')),
{
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: "The expression evaluated to a falsy value:\n\n ässört.ok('')\n"
}
);

assert.throws(
() => require(path('assert-long-line')),
{
name: 'AssertionError [ERR_ASSERTION]',
name: 'AssertionError',
message: "The expression evaluated to a falsy value:\n\n assert.ok('')\n"
}
);

0 comments on commit 1ed3c54

Please sign in to comment.