Skip to content

Instantly share code, notes, and snippets.

@kamilogorek
Created February 14, 2019 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kamilogorek/ca0fce8cddaffc0a2177b765df4fea9a to your computer and use it in GitHub Desktop.
Save kamilogorek/ca0fce8cddaffc0a2177b765df4fea9a to your computer and use it in GitHub Desktop.
const Sentry = require("@sentry/node");
const DELIVERY_TIME = process.argv[2];
const TIMEOUT = process.argv[3];
console.log(`\nDelivery time: ${DELIVERY_TIME / 1000}s`);
console.log(`Timeout: ${TIMEOUT / 1000}s`);
// @ignore-start Helper function
async function sleep() {
let i = 1;
const log = setInterval(
() => (console.log(`Delivery takes ${i}s...`), i++),
1000
);
return new Promise(resolve =>
setTimeout(() => {
clearInterval(log);
resolve();
}, DELIVERY_TIME)
);
}
class SlowNetwork extends Sentry.Transports.BaseTransport {
async captureEvent(event) {
// Simulating a long delivery time
await sleep();
console.log("\n", event);
return {
status: "success"
};
}
}
// @ignore-end
Sentry.init({
dsn: "https://example@sentry.io/1337",
transport: SlowNetwork,
async beforeSend(event) {
console.log("\nCaught an exception!");
return event;
}
});
async function handler() {
try {
// Some user-land code
Sentry.configureScope(scope => scope.setExtra("answer", "42"));
throw new Error("omg it broke");
} catch (e) {
Sentry.captureException(e);
if (await Sentry.flush(TIMEOUT)) {
console.log("\nException delivered.\n");
} else {
console.log("\nTimeout reached, terminate the process.\n");
process.exit(1);
}
}
}
handler();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment