In JavaScript applications with Rollbar integrated for reporting, you might see a common recurrence of the following error:
(unknown): "[unhandledrejection] error getting `reason` from event"
Additionally, later on, some other unhandled rejections are also grouped together under the same heading, although they did provide “reason,” such as “Object Not Found Matching Id:3” or “Script error.”
While trying to resolve this issue, I saw that “Object Not Found Matching Id:3” (ending is sometimes Id:2, Id:5) also occurs for Sentry JavaScript users. Though this post doesn’t address a Sentry solution, it might still be helpful.
Below, I have listed my findings and possible solutions.
The Reason
This event occurs when:
-
No rejection handler is provided for a JavaScript Promise (as explained in the docs),
-
We have set
captureUnhandledRejections: true
in Rollbar configurations, -
catch
block to handle the rejection is missing in the code, and -
No message is passed to
resolve()
orthrow
(which does not throw anError
instance).
Example Code to Regenerate the Error
import Rollbar from "rollbar";
new Rollbar({
accessToken: "app_access_token",
captureUncaught: true,
captureUnhandledRejections: true,
payload: {
environment: "local"
}
});
// simulate promise reject with
// empty string and without catch block
new Promise((resolve, reject) => {
reject("");
});
Running this test code, you should get this error on the Rollbar dashboard:
(unknown): "[unhandledrejection] error getting `reason` from event"
If you pass a message to reject
a different error will appear on the dashboard. Try:
new Promise((resolve, reject) => {
reject("Reject With Error");
});
Similarly, we can simulate Object Not Found Matching Id:3
as well:
new Promise((resolve, reject) => {
reject("Object Not Found Matching Id:3");
});
Note that Object Not Found Matching Id:3
will be grouped under the first error heading in Rollbar.
Possible Solutions
1. Provide Catch Block
If it’s your code that is causing this, then the good practice is to add the missing catch block:
new Promise((resolve, reject) => {
reject("Reject With Error");
})
// .then()
// .then()
.catch(e => {
// do something (or not) with e
})
2. Ignore Third Party Errors With CheckIgnore
When the errors are not in your control and belong to a third party, you can simply intercept the error messages and filter out the ones you want to ignore with the additional configuration method checkIgnore.
One such error is “Object Not Found Matching Id:3”, which is somehow related to Chrome + Windows + Outlook combination (from what I’ve found).
new Rollbar({
accessToken: "app_access_token",
captureUncaught: true,
captureUnhandledRejections: true,
payload: {
environment: "local"
},
checkIgnore: function (isUncaught, args, payload) {
if (args.length
&& args[1].includes("Object Not Found Matching Id")) {
return true;
}
}
});
Notice in both solutions 1 and 2 that we’re not looking for the exact string but finding the common part because the error also comes with the variations such as “Object Not Found Matching Id:1”, “Object Not Found Matching Id:2”, and “Object Not Found Matching Id:5”.
3. Handle and Log Uncaught Rejections Yourself
Using the method handleUncaughtException on Rollbar instance, we can either skip the known unwanted errors, or log them as info
or debug
for record keeping. This will not trigger error alerts on the Rollbar dashboard and other integrated places like Slack channels. We can log the rest of the uncaught rejection errors as error
with our custom message.
const rollbar = new Rollbar({
accessToken: "app_access_token",
captureUncaught: true,
captureUnhandledRejections: true,
payload: {
environment: "local"
}
});
rollbar.handleUncaughtException = (exceptionMsg = "") => {
const logMsg = `Unhandled Rejection - ${exceptionMsg}`;
if (exceptionMsg.includes("Object Not Found Matching Id")) {
rollbar.info(logMsg);
}
else {
rollbar.error(logMsg);
}
}
See also
- Node JS Mongo Client for Atlas Data API
- SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your key and signing method.
- Yup Date Format Validation With Moment JS
- Yup Number Validation: Allow Empty String
- Exactly Same Query Behaving Differently in Mongo Client and Mongoose
- JavaScript Unit Testing JSON Schema Validation
- AWS Layer: Generate nodejs Zip Layer File Based on the Lambda's Dependencies