Skip to content

Instantly share code, notes, and snippets.

@kattrali
Created June 6, 2017 21:13
Show Gist options
  • Save kattrali/1ac8dcb209fcd5b5db2e5e16afafae60 to your computer and use it in GitHub Desktop.
Save kattrali/1ac8dcb209fcd5b5db2e5e16afafae60 to your computer and use it in GitHub Desktop.
demonstration of promises breaking bugsnag requestData. Tested with node v6.9 and v8.0
const express = require("express");
const bugsnag = require("bugsnag");
const unhandledRejection = require("unhandled-rejection");
let rejectionEmitter = unhandledRejection({ timeout: 20 });
rejectionEmitter.on("unhandledRejection", (error) => {
console.log("Promise....REJECTED!");
bugsnag.notify(error);
});
const app = express();
bugsnag.register("SOME-KEY");
app.use(bugsnag.requestHandler);
bugsnag.onBeforeNotify((notification) => {
console.log("callback request data: " + process.domain._bugsnagOptions);
return false;
});
// works just fine
app.get("/", (req, res, next) => {
console.log("GET request data: " + process.domain._bugsnagOptions);
});
// fails, but because there's no domain context when the rejection emitter
// catches the promise rejection. Regular throws don't have this problem.
app.post("/", (req, res, next) => {
console.log("POST request data: " + process.domain._bugsnagOptions);
new Promise((res, rej) => {
rej(new Error("Oh no"));
});
});
app.listen(3000);
{
"dependencies": {
"bugsnag": "^1.9.1",
"express": "^4.15.3",
"unhandled-rejection": "^1.0.0"
}
}
POST request data: [object Object]
Promise....REJECTED!
/Users/delisa/Code/bugsnag/sample-apps/js/node-domain-test/index.js:18
console.log("callback request data: " + process.domain._bugsnagOptions);
^
TypeError: Cannot read property '_bugsnagOptions' of undefined
at bugsnag.onBeforeNotify (/Users/delisa/Code/bugsnag/sample-apps/js/node-domain-test/index.js:18:57)
at Notification.<anonymous> (/Users/delisa/Code/bugsnag/sample-apps/js/node-domain-test/node_modules/bugsnag/lib/notification.js:116:19)
at Array.forEach (native)
at Notification.deliver (/Users/delisa/Code/bugsnag/sample-apps/js/node-domain-test/node_modules/bugsnag/lib/notification.js:115:41)
at Object.Bugsnag.notify (/Users/delisa/Code/bugsnag/sample-apps/js/node-domain-test/node_modules/bugsnag/lib/bugsnag.js:111:22)
at process.<anonymous> (/Users/delisa/Code/bugsnag/sample-apps/js/node-domain-test/node_modules/bugsnag/lib/bugsnag.js:75:21)
at emitOne (events.js:115:13)
at process.emit (events.js:210:7)
at process._fatalException (bootstrap_node.js:329:26)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment