question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When I try to print `msg.text` in 'console' event with type 'error', I got `JSHandle@error`

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 1.9.0
  • Platform / OS version: macos
  • URLs (if applicable):
  • Node.js version: 8.11.3

What steps will reproduce the problem?

Please include code that reproduces the issue.

  1. Simply add these code in your page
try {
    // try to print a value doesn't exist
    // and it will throw an error
    console.log(a)
} catch (e) {
    // catch the error and print this with `console.error`
    console.error(e)
}
  1. puppeteer script:
...
page.on('console', msg => {
    console.log(msg.text())
})
...

What is the expected result? It prints JSHandle@error

What happens instead? The exact error object like ReferenceError: a is not defined...

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:18
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

22reactions
tomperecommented, Oct 12, 2018

@SidKwok you can use msg.args() to retrieve the array of arguments passed to console.

BUT, since errors are not serializeable it won’t return the desired value. you can however, use the following as a workaround:

page.on('console', async msg => {
  const args = await msg.args()
  args.forEach(async (arg) => {
    const val = await arg.jsonValue()
    // value is serializable
    if (JSON.stringify(val) !== JSON.stringify({})) console.log(val)
    // value is unserializable (or an empty oject)
    else {
      const { type, subtype, description } = arg._remoteObject
      console.log(`type: ${type}, subtype: ${subtype}, description:\n ${description}`)
    }
  })
});
14reactions
aslushnikovcommented, Nov 1, 2018

Yeah I agree, we should probably do a better job on getting error texts and everything. The way it’s supposed to be done atm is like this:

page.on('console', async msg => {
  // serialize my args the way I want
  const args = await Promise.all(msg.args.map(arg => arg.executionContext().evaluate(arg => {
    // I'm in a page context now. If my arg is an error - get me its message.
    if (arg instanceof Error)
      return arg.message;
    // return arg right away. since we use `executionContext.evaluate`, it'll return JSON value of
    // the argument if possible, or `undefined` if it fails to stringify it.
    return arg;
  }, arg)));
  console.log(...args);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get all console messages with puppeteer? including ...
I am fetching a page with puppeteer that has some errors in the browser console but the puppeteer's console event is not ...
Read more >
A mostly complete guide to error handling in JavaScript.
Learn how to deal with errors and exceptions in synchronous and asynchronous JavaScript code.
Read more >
You receive an error message when you try to print a ...
If you click Yes, your document is printed, but some text may be truncated and may not appear on the printed page. If...
Read more >
Control flow and error handling - JavaScript - MDN Web Docs
(In C or Java, the equivalent code would have output 1 .) ... Depending on the type of error, you may be able...
Read more >
JavaScript Console.log() Example – How to Print to the ...
Logging messages to the console is a very basic way to diagnose and troubleshoot minor issues in your code. But, did you know...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found