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.

[bug] event emitters cause uncaught errors

See original GitHub issue

Describe the bug A clear and concise description of what the bug is.

Because of some event emitter that either twitter-api-v2 is using or a dependency of twitter-api-v2, errors are thrown twice: once that can be caught within an async function but then again when they are emitted by that EventEmitter and cannot be caught, resulting in an uncaught error and the Node.js process terminating the an error code (1).

To Reproduce Please indicate all steps that lead to this bug:

  1. Send an async API request that causes an error (e.g. “429 Too Many Requests”).
  2. Wrap that API request in a try, catch clause and log the caught error (but don’t re-throw).
  3. Notice that the error is correctly logged but it is still re-emitted later in an uncaught exception:
[dev:remix] [info] Fetching followed lists for user (1329661759020363778) lists...
[dev:remix] [info] Fetching owned lists for user (1329661759020363778) lists...
[dev:remix] [error] You just hit the rate limit! Limit for this endpoint is 15 requests!
[dev:remix] [error] Request counter will reset at timestamp 3/21/2022, 9:13:38 PM.
[dev:remix] Error: You defined an action for route "routes/sync/lists" but didn't return anything from your `action` function. Please return a value or `null`.
[dev:remix]     at Object.callRouteAction (/home/nchiang/repos/tweetscape/node_modules/@remix-run/server-runtime/data.js:54:11)
[dev:remix]     at processTicksAndRejections (node:internal/process/task_queues:96:5)
[dev:remix]     at handleDataRequest (/home/nchiang/repos/tweetscape/node_modules/@remix-run/server-runtime/server.js:110:18)
[dev:remix]     at requestHandler (/home/nchiang/repos/tweetscape/node_modules/@remix-run/server-runtime/server.js:45:20)
[dev:remix]     at /home/nchiang/repos/tweetscape/node_modules/@remix-run/express/server.js:43:22
[dev:remix] PATCH /sync/lists?_data=routes%2Fsync%2Flists 500 - - 266.739 ms
[dev:remix] Error: Request failed with code 429
[dev:remix]     at RequestHandlerHelper.createResponseError (/home/nchiang/repos/tweetscape/node_modules/twitter-api-v2/dist/client-mixins/request-handler.helper.js:99:16)
[dev:remix]     at RequestHandlerHelper.onResponseEndHandler (/home/nchiang/repos/tweetscape/node_modules/twitter-api-v2/dist/client-mixins/request-handler.helper.js:248:25)
[dev:remix]     at Gunzip.emit (node:events:520:28)
[dev:remix]     at endReadableNT (node:internal/streams/readable:1346:12)
[dev:remix]     at processTicksAndRejections (node:internal/process/task_queues:83:21)
[dev:remix] yarn run dev:remix exited with code 1

That ☝️ is the result of the following code (I’ve omitted lines for brevity):

export const action: ActionFunction = async ({ request }) => {
  try {
    const api = new TwitterApi(accessToken);
    log.info(`Fetching followed lists for ${context}...`);
    const res = await api.v2.listFollowed(uid);
  } catch (e) {
    if (e instanceof ApiResponseError && e.rateLimitError && e.rateLimit) {
      const msg1 = 
        `You just hit the rate limit! Limit for this endpoint is ` +
        `${e.rateLimit.limit} requests!`
      const msg2 =
        `Request counter will reset at timestamp ` +
        `${new Date(e.rateLimit.reset * 1000).toLocaleString()}.`
      log.error(msg1); // This is logged.
      log.error(msg2); // This is also logged, meaning the error was caught.
    }
  }
};

Expected behavior A clear and concise description of what you expected to happen.

Errors should only be thrown once and, once caught, shouldn’t result in a non-zero Node.js process exit.

Version

  • Node.js version: 16.14.0
  • Lib version: 1.11.1
  • OS (especially if you use Windows): Pop_OS! 20.04 (Ubuntu 20.04 LTS)

Additional context Add any other context about the problem here.

This may have something to do with this.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
alkihiscommented, Mar 23, 2022

Should be fixed in 1.11.2.

1reaction
nicholaschiangcommented, Mar 22, 2022

Yeah, I’ll see if I can create a repro for you @alkihis. The project I’m working on where I’m seeing that error is open-source, if you wanna take a peek in the meantime (you should be able to recreate my issue with it): https://github.com/roote/tweetscape

Read more comments on GitHub >

github_iconTop Results From Across the Web

Intermittent uncaught errors · Issue #631 · sindresorhus/got
The error is in my original message: there's some EventEmitter that emits an error event and the argument of that is the TimeoutError...
Read more >
possible EventEmitter memory leak detected - node.js
I found this page because I got this warning and in my case there was a bug in some code I was using...
Read more >
Promises and Node.js event emitters don't mix - Qubyte Codes
Node.js event emitters exhibit some unexpected behaviour, with error events in particular, which does not mix well with promises.
Read more >
Uncaught errors in Node.js and the browser
Programmer errors are caused by bugs in our code. A common example is trying to access a property of undefined.
Read more >
A Comprehensive Guide To Error Handling In Node.js
The emitCount() function returns a new event emitter that reports both success and failure events in the asynchronous operation. The function ...
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