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.

Conditional catch for errors

See original GitHub issue

Issue template didn’t really apply.

I can’t find anything in the api docs page about error handling. The main type of error that’s encountered is a timeout. The issue is that there’s no documented way to distinguish the type of error received in the catch after awaiting a promise that rejects.

The issue is that good code (most of the time) needs to do conditional catches to avoid silencing unexpected errors. There doesn’t seem to be a good, stable way to do this currently.

I tried inspecting some of the errors. For these tests, the output is logging error and then logging {...error}.

await page.click('doesntexist');

// Result
{ AssertionError [ERR_ASSERTION]: No node found for selector: doesntexist
    at Console.assert (console.js:188:23)
    at Page.click (/private/tmp/temp-puppet-7695/node_modules/puppeteer/lib/Page.js:770:13)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
  generatedMessage: false,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '==' }
{ generatedMessage: false,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '==' }

Gives a generic AssertionError with no information about the cause, except if you want to parse the error message, which seems very unstable as it can change without much notice.

The next issue is waitFor:

await page.waitFor('doesntexist');

// Result
Error: waiting failed: timeout 30000ms exceeded
    at Timeout.WaitTask._timeoutTimer.setTimeout (/private/tmp/temp-puppet-7695/node_modules/puppeteer/lib/FrameManager.js:593:58)
    at ontimeout (timers.js:475:11)
    at tryOnTimeout (timers.js:310:5)
    at Timer.listOnTimeout (timers.js:270:5)
{}

Again, the only way I see to extract information is the error message.


There are a few potential solutions here:

  1. document the error messages and give an example regex to check them. This requires no code changes, but makes it clear that the error messages changing would be a breaking change.
  2. don’t change the errors, but give official utility functions like puppeteer.isTimeoutError(error). Very little code changes, but the api will be stable across versions. Better than 1.
  3. make each error either an Error subclass, or give it properties e.g. type: 'PuppeteerTimeout', selector: 'doesntexist'. This is about tied with 2, since it gives a readable way to check the error type.

Thoughts? Sorry if this has come up before, but I couldn’t find anything relevant.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:16
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
aslushnikovcommented, Jan 11, 2018

I currently regexp-match errors’ message property, and I’d sleep better at night if I had a trustworthy filter for puppeteer-originated errors.

@pwnall why puppeteer-specific error.code values aren’t good enough for your usecase? I prefer them to custom error classes for a few reasons:

  • error.code values are lightweight
  • error.code values fit nicer with the rest of the API: ATM we have no classes exposed, only instances.
2reactions
aslushnikovcommented, Jan 4, 2018

This sounds reasonable. How about using error.code for the purpose?

if (err.code === 'PPTR_TIMEOUT') {
  // .. handle timeout error
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Conditional catch clauses - browsers support
Conditional catch clauses - browsers support · 1. MDN is a JavaScript reference. · 1 · Use the second way, it's just as...
Read more >
try...catch - JavaScript - MDN Web Docs - Mozilla
Unconditional catch-block ... When a catch -block is used, the catch -block is executed when any exception is thrown from within the try...
Read more >
conditional-catch - npm
This module exports a class ConditionalCatch which should only be created via the static function ConditionalCatch.createFrom(error: Error) .
Read more >
Conditional catch clause - ESDiscuss.org
Hello,. In SpiderMonkey (and perhaps other JS engines?), there are conditional catch clauses: catch (exception if condition).
Read more >
Error handling, "try...catch" - The Modern JavaScript Tutorial
If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err) . The err variable...
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