Skip to content

Weird Proxy internal util.inspect error #25212

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
LJNeon opened this issue Dec 25, 2018 · 1 comment
Closed

Weird Proxy internal util.inspect error #25212

LJNeon opened this issue Dec 25, 2018 · 1 comment

Comments

@LJNeon
Copy link

LJNeon commented Dec 25, 2018

  • Version: v10.14.2 & v11.5.0
  • Platform: Linux
  • Subsystem: inspect?

The following code:

let thing;
thing = new Proxy(() => {}, {
  get() {
    return thing;
  },
  apply() {
    return thing;
  }
});
console.info(thing);

emits the following error when executed:

internal/util/inspect.js:594
      const name = `${type}${value.name ? `: ${value.name}` : ''}`;
                                                     ^

TypeError: Cannot convert object to primitive value
    at formatRaw (internal/util/inspect.js:594:54)
    at formatValue (internal/util/inspect.js:508:10)
    at inspect (internal/util/inspect.js:191:10)
    at Object.formatWithOptions (util.js:84:12)
    at Console.(anonymous function) (console.js:188:15)
    at Console.log (console.js:199:31)
    at Object.<anonymous> (/home/ubuntu/workspace/thread/index.js:12:9)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)

If I remove either the get() or the apply() it works, but when both are set it errors.

@starkwang
Copy link
Contributor

starkwang commented Dec 28, 2018

The reason is that the Proxy makes all properties in thing return itself. So when we inspect it, thing.name will return a function, not a primitive string.

And it can be reproduced in chrome:

let thing;
thing = new Proxy(() => {}, {
  get() {
    return thing;
  },
  apply() {
    return thing;
  }
});

console.log(String(thing))
// Uncaught TypeError: Cannot convert object to primitive value
//    at String (<anonymous>)
//    at <anonymous>:11:13

targos pushed a commit that referenced this issue Jan 1, 2019
PR-URL: #25244
Fixes: #25212
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
refack pushed a commit to refack/node that referenced this issue Jan 14, 2019
PR-URL: nodejs#25244
Fixes: nodejs#25212
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
BridgeAR added a commit to BridgeAR/node that referenced this issue Feb 28, 2019
This prevents any proxy traps from being called while inspecting
proxy objects. That guarantees a side-effect free way of inspecting
proxies.

Refs: nodejs#25212
Refs: nodejs#24765
Fixes: nodejs#10731
Fixes: nodejs#26231
BridgeAR added a commit to BridgeAR/node that referenced this issue Feb 28, 2019
This prevents any proxy traps from being called while inspecting
proxy objects. That guarantees a side-effect free way of inspecting
proxies.

PR-URL: nodejs#26241
Fixes: nodejs#10731
Fixes: nodejs#26231
Refs: nodejs#25212
Refs: nodejs#24765
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
BethGriggs pushed a commit that referenced this issue Apr 28, 2019
PR-URL: #25244
Fixes: #25212
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
BethGriggs pushed a commit that referenced this issue May 10, 2019
PR-URL: #25244
Fixes: #25212
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
MylesBorins pushed a commit that referenced this issue May 16, 2019
PR-URL: #25244
Fixes: #25212
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants