I have a Node.js Cloud Function and am trying to be alerted to unhandled rejections. If I throw an unhandled rejection and cause a crash in my index.js
entry point, the error shows up in Error Reporting (although I don't receive a notification for some reason). This also shows up as log level "critical":
If I throw an identical unhandled rejection in a different .js file in the same Cloud Function (which is being imported by index.js
), it does not show up as log level "critical" and the error is never reported in Error Reporting:
What is the correct way to ensure I capture all unhandled rejections across my entire Cloud Function (and ensure I receive alerts)? This is how I am initializing Error Reporting in my index.js
entry point:
const {ErrorReporting} = require('@google-cloud/error-reporting');
const errors = new ErrorReporting({
reportUnhandledRejections: true
});
I got clarification from the Error Reporting team per this github issue: https://github.com/googleapis/nodejs-error-reporting/issues/548.
In the end, it was likely because I was only initializing my error reporting object in my main index.js
and not passing it to or doing so again in my other .js
files (where the actual unhandled rejections were occurring). One workaround was wrapping my entire index.js
code in a try / catch(e)
and in the catch(e) block put in errors.report(e)
. Then, even if an unhandled rejection occurs in another file during execution, I get notified in Error Reporting.