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.

How to Catch Rate Limit errors?

See original GitHub issue

Hi,

we use your great nodejs library to integrate with Google Drive. We wrap your library basically in https://github.com/simatec/ioBroker.backitup/blob/master/lib/googleDriveLib.js

Now we have some users that alw<ys get Rate Limiting errors and in fact these are ok, but something seems to be wrong because they are thrown as unhandled promise rejections in the end and are not catchable. I also did a dive through your code and basically yes it should work, but we still get these reports and also our Sentry instance shows them:

One case: https://sentry.iobroker.net/share/issue/cdcb2ff3d08042f7829bb551e73a7043/

Error: User Rate Limit Exceeded. Rate of requests for user exceed configured project quota. You may consider re-evaluating expected per-user traffic to the API and adjust project quota limits accordingly. You may monitor aggregate quota usage and adjust lim...
  File "/opt/iobroker/node_modules/gaxios/src/gaxios.ts", line 117, col 15, in Gaxios._request
  ?, in runMicrotasks
  File "internal/process/task_queues.js", line 97, col 5, in processTicksAndRejections
  File "/opt/iobroker/node_modules/google-auth-library/build/src/auth/oauth2client.js", line 343, col 18, in OAuth2Client.requestAsync
    r2 = await this.transporter.request(opts);

Second case: https://sentry.iobroker.net/share/issue/a325278331b14b81ac79337e65a6be86/

Error: Rate Limit Exceeded
  File "/opt/iobroker/node_modules/iobroker.backitup/node_modules/gaxios/src/gaxios.ts", line 117, col 15, in Gaxios._request
  File "internal/process/next_tick.js", line 68, col 7, in process._tickCallback

We use the most current version of googleaps here: 59.0.0

In fact yes we need to optimize the timing when (as it seems) we delete many files (https://github.com/simatec/ioBroker.backitup/blob/master/lib/scripts/75-googledrive.js#L43 …) but in fact the error should come to the callback as expected and should not be uncatchable thrown …

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JustinBeckwithcommented, Sep 4, 2020

Greetings! I took a look at your code, and it has a few issues that could be leading to the problem here. Your code is mixing promises and call backs in some confusing ways. This code:

 deleteFile(folderOrFileId, fileName) {
        return new Promise((resolve, reject) => {
            if (folderOrFileId && !fileName) {
                this._getDrive()
                    .then(drive => drive.files.delete({fileId: folderOrFileId}, err => {
                        if (err) {
                            // Handle error
                            reject(err);
                        } else {
                            resolve();
                        }
                    }));
            } else {
                this.getFileOrFolderId(fileName, folderOrFileId)
                    .then(fileId => {
                        this._getDrive()
                            .then(drive => drive.files.delete({fileId}, err => {
                                if (err) {
                                    // Handle error
                                    reject(err);
                                } else {
                                    resolve();
                                }
                            }));
                    });
            }
        });

Could be simplified to:

async function deleteFile(folderOrFileId, fileName) {
  const drive = await this._getDrive()
  if (folderOrFileId && !fileName) {
    await drive.files.delete({fileId: folderOrFileId});
  } else {
    const fileId = await this.getFileOrFolderId(fileName, folderOrFileId);
    await drive.files.delete({fileId});
  }
}

When you call one of these methods as a promise, whether with .then chaining or async functions, it’s going to throw if you hit an exception. If you want to handle specific cases, you can always wrap this in a try/catch, and then look for err.response.status, which should tell you what caused this specific problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practices for avoiding rate limiting | Zendesk Developer ...
Monitoring API activity against your rate limit · Catching errors caused by rate limiting · Reducing the number of API requests · Regulating...
Read more >
Be prepared: 3 ways to handle rate limits | by Cathal Horan
A better way: randomization​​ If you want more control over your rate limiting handling then you can check for the specific rate limit...
Read more >
How to handle the rate limit reached error
One solution is to try to slow down the number of requests you are making. Another is to use a technique that reduces...
Read more >
How to handle rate limits & errors - Klaviyo Developers
All endpoints are rate limited. Check the response codes for a 429 error, which indicates you have hit a rate limit, and retry...
Read more >
Rate Limit Exceeded errors | Plaid Docs
Errors of type RATE_LIMIT_EXCEEDED will occur when the rate limit for a particular endpoint has been exceeded. Default rate limit thresholds for some...
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