[bug] event emitters cause uncaught errors
See original GitHub issueDescribe 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:
- Send an async API request that causes an error (e.g. “429 Too Many Requests”).
- Wrap that API request in a
try
,catch
clause and log the caught error (but don’t re-throw). - 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:
- Created a year ago
- Comments:6
Should be fixed in 1.11.2.
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