-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
errors: extract type detection & use in ERR_INVALID_RETURN_VALUE
#43558
Merged
nodejs-github-bot
merged 7 commits into
nodejs:main
from
JakobJingleheimer:errors/account-for-specific-types-in-return-value
Jul 1, 2022
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
74be6fe
errors: extract type detection & use in `ERR_INVALID_RETURN_VALUE`
JakobJingleheimer 572c162
fixup! errors: extract type detection & use in `ERR_INVALID_RETURN_VA…
JakobJingleheimer 4a4204e
fixup! fixup! errors: extract type detection & use in `ERR_INVALID_RE…
JakobJingleheimer b757821
fixup! fixup! fixup! errors: extract type detection & use in `ERR_INV…
JakobJingleheimer 4871b85
fixup! fixup! fixup! fixup! errors: extract type detection & use in `…
JakobJingleheimer 96dbd0e
fixup! fixup! fixup! fixup! fixup! errors: extract type detection & u…
JakobJingleheimer d2eded5
fixup! fixup! fixup! fixup! fixup! fixup! errors: extract type detect…
JakobJingleheimer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -737,14 +737,15 @@ function invalidArgTypeHelper(input) { | |||||
return ` Received function ${input.name}`; | ||||||
} | ||||||
if (typeof input === 'object') { | ||||||
if (input.constructor && input.constructor.name) { | ||||||
if (input?.constructor?.name) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We know for certain that |
||||||
return ` Received an instance of ${input.constructor.name}`; | ||||||
} | ||||||
return ` Received ${inspect(input, { depth: -1 })}`; | ||||||
} | ||||||
|
||||||
let inspected = inspect(input, { colors: false }); | ||||||
if (inspected.length > 25) | ||||||
inspected = `${inspected.slice(0, 25)}...`; | ||||||
if (inspected.length > 28) { inspected = `${inspected.slice(inspected, 0, 25)}...`; } | ||||||
|
||||||
return ` Received type ${typeof input} (${inspected})`; | ||||||
} | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// Flags: --expose-internals | ||
|
||
import '../common/index.mjs'; | ||
import { strictEqual } from 'node:assert'; | ||
import errorsModule from 'internal/errors'; | ||
|
||
|
||
const { determineSpecificType } = errorsModule; | ||
|
||
strictEqual( | ||
determineSpecificType(1n), | ||
'type bigint (1n)', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(false), | ||
'type boolean (false)', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(2), | ||
'type number (2)', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(NaN), | ||
'type number (NaN)', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(Infinity), | ||
'type number (Infinity)', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(Object.create(null)), | ||
'[Object: null prototype] {}', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(''), | ||
"type string ('')", | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(Symbol('foo')), | ||
'type symbol (Symbol(foo))', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(function foo() {}), | ||
'function foo', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(null), | ||
'null', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(undefined), | ||
'undefined', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType([]), | ||
'an instance of Array', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(new Array()), | ||
'an instance of Array', | ||
); | ||
strictEqual( | ||
determineSpecificType(new BigInt64Array()), | ||
'an instance of BigInt64Array', | ||
); | ||
strictEqual( | ||
determineSpecificType(new BigUint64Array()), | ||
'an instance of BigUint64Array', | ||
); | ||
strictEqual( | ||
determineSpecificType(new Int8Array()), | ||
'an instance of Int8Array', | ||
); | ||
strictEqual( | ||
determineSpecificType(new Int16Array()), | ||
'an instance of Int16Array', | ||
); | ||
strictEqual( | ||
determineSpecificType(new Int32Array()), | ||
'an instance of Int32Array', | ||
); | ||
strictEqual( | ||
determineSpecificType(new Float32Array()), | ||
'an instance of Float32Array', | ||
); | ||
strictEqual( | ||
determineSpecificType(new Float64Array()), | ||
'an instance of Float64Array', | ||
); | ||
strictEqual( | ||
determineSpecificType(new Uint8Array()), | ||
'an instance of Uint8Array', | ||
); | ||
strictEqual( | ||
determineSpecificType(new Uint8ClampedArray()), | ||
'an instance of Uint8ClampedArray', | ||
); | ||
strictEqual( | ||
determineSpecificType(new Uint16Array()), | ||
'an instance of Uint16Array', | ||
); | ||
strictEqual( | ||
determineSpecificType(new Uint32Array()), | ||
'an instance of Uint32Array', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(new Date()), | ||
'an instance of Date', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(new Map()), | ||
'an instance of Map', | ||
); | ||
strictEqual( | ||
determineSpecificType(new WeakMap()), | ||
'an instance of WeakMap', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType({}), | ||
'an instance of Object', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(Promise.resolve('foo')), | ||
'an instance of Promise', | ||
); | ||
|
||
strictEqual( | ||
determineSpecificType(new Set()), | ||
'an instance of Set', | ||
); | ||
strictEqual( | ||
determineSpecificType(new WeakSet()), | ||
'an instance of WeakSet', | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.