Skip to content

Instantly share code, notes, and snippets.

@kamilogorek
Created May 21, 2019 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamilogorek/654d471ebbcab3dc20a81d7a1bfe25a7 to your computer and use it in GitHub Desktop.
Save kamilogorek/654d471ebbcab3dc20a81d7a1bfe25a7 to your computer and use it in GitHub Desktop.
const express = require("express");
const Sentry = require("@sentry/node");
const api = express();
Sentry.init({
dsn: "http://whatever@sa.com/12",
environment: "development",
beforeSend(event) {
console.log("beforeSend", event.user.id);
return null;
}
});
api.use(Sentry.Handlers.requestHandler());
api.use((req, res, next) => {
req.user = { id: Math.ceil(Math.random() * 1000) };
next();
});
// Set user for Sentry context
api.use((req, res, next) => {
if (req.user) {
console.log("Point A", req.user.id);
Sentry.getCurrentHub().configureScope(scope => {
console.log("Point B", req.user.id);
scope.setUser({ id: req.user.id });
});
}
next();
});
// Throw an error for all requests
api.use((req, res, next) => {
throw new Error("Uh-oh Test");
});
api.use(Sentry.Handlers.errorHandler());
api.use((err, req, res, next) => {
const status = err.status || 500;
const msg = err.message || "Internal Server Error";
res.status(status).json({ name: "API Error", message: msg });
});
api.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment