Search code examples
node.jsgoogle-cloud-platformerror-handlingerror-logging

Keep track of specific errors in Google Cloud Error Logs?


I am using GCP with Cloud Run, SQL etc with a Node.js application. I have a specific type of error that appear quite often, related to a Twitter API call. Quite often I get this "Rate limit exceeded" error from the API which I handle like this:

items.forEach((item) => {
if (item.body.status == 429) {
            throw Error("Rate limit exceeded")
}
}).catch((error) => console.log(error))

This appears in the logs but I would like it to be tracked in GCP Error list https://console.cloud.google.com/errors so I can see if my efforts to limit this specific error is going better. I don't want it to break the script so I also want to catch the error. How can I solve this best?


Solution

  • I haven't been able to reproduce with your exact syntax (I'm not a nodejs expert) but I've been able to get errors reported in both Error Reporting and Logging via the following code:

    try {
      items.forEach((item) => {
        if (item.body.status == 429) {
          console.log(new Error("Rate limit exceeded"));
        }
      })
    }
    catch (error) {
      console.log(error);
    }
    

    Note that instead of throwing the error in the if block I just console.log(new Error("Rate limit exceeded")) and Error Reporting automatically picks it up.

    The "Rate limit exceeded" does appear for me out of the box in the Error Reporting dashboard: enter image description here

    and in the logs: enter image description here

    Also note that the Logging page has an histogram view above the log entries that shows occurrences of the logs over time. By just filtering on the textPayload with the errors, you can view a detailed count of these errors over time in the histogram.