Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.