Skip to content
Prev Previous commit
Next Next commit
fixup! fixup! fixup! fixup! fixup! errors: extract type detection & u…
…se in `ERR_INVALID_RETURN_VALUE`

revert type determination output for objects without a constructor
  • Loading branch information
JakobJingleheimer committed Jul 1, 2022
commit 96dbd0e2e8a36259bc23c4347e033e5559ea0117
7 changes: 5 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,11 @@ function determineSpecificType(value) {
if (typeof value === 'function' && value.name) {
return `function ${value.name}`;
}
if (typeof value === 'object' && value.constructor?.name) {
return `an instance of ${value.constructor.name}`;
if (typeof value === 'object') {
if (value?.constructor?.name) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (value?.constructor?.name) {
if (value.constructor?.name) {

return `an instance of ${value.constructor.name}`;
}
return `${lazyInternalUtilInspect().inspect(value, { depth: -1 })}`;
}
let inspected = lazyInternalUtilInspect()
.inspect(value, { colors: false });
Expand Down
7 changes: 5 additions & 2 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,11 @@ function invalidArgTypeHelper(input) {
if (typeof input === 'function' && input.name) {
return ` Received function ${input.name}`;
}
if (typeof input === 'object' && input.constructor?.name) {
return ` Received an instance of ${input.constructor.name}`;
if (typeof input === 'object') {
if (input?.constructor?.name) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (input?.constructor?.name) {
if (input.constructor?.name) {

We know for certain that input is an object of some kind (null is already checked above).

return ` Received an instance of ${input.constructor.name}`;
}
return ` Received ${inspect(input, { depth: -1 })}`;
}

let inspected = inspect(input, { colors: false });
Expand Down
2 changes: 1 addition & 1 deletion test/errors/test-error-value-type-detection.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ strictEqual(

strictEqual(
determineSpecificType(Object.create(null)),
'type object ([Object: null prototype] {})',
'[Object: null prototype] {}',
);

strictEqual(
Expand Down