Skip to content

Instantly share code, notes, and snippets.

@kamilogorek kamilogorek/1939.js
Created May 21, 2019

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