Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Explain getHubFromCarrier in express #447

Merged
merged 1 commit into from
Oct 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions src/collections/_documentation/platforms/javascript/express.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,32 @@ sidebar_order: 1010
---

<!-- WIZARD -->

Our Express integration only requires _@sentry/node_ to be installed then you can use it like this:

```javascript
const express = require('express');
const app = express();
const Sentry = require('@sentry/node') ;
const Sentry = require('@sentry/node');

Sentry.init({ dsn:'___PUBLIC_DSN___' });
Sentry.init({ dsn: '___PUBLIC_DSN___' });

// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());

app.get('/', function mainHandler(req, res) {
throw new Error('Broke!');
throw new Error('Broke!');
});

// The error handler must be before any other error middleware
app.use(Sentry.Handlers.errorHandler());

// Optional fallthrough error handler
app.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + '\n');
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + '\n');
});

app.listen(3000);
Expand All @@ -55,4 +56,37 @@ app.use(Sentry.Handlers.errorHandler());
// to
app.use(Sentry.Handlers.errorHandler() as express.ErrorRequestHandler);
```

<!-- ENDWIZARD -->

## Working with Scopes and Hubs

Because of pipeline-like nature of Express.js and it's middlewares, the user has to pass the scope through all the pipes and carry it over with the request. It's required in order to isolate requests from each other.

If you want to set per-requests user data and send it over to Sentry, you need to use `getHubFromCarrier` on the `request` object, instead of directly calling methods like `configureScope` or `captureException`. Here are some examples:

```javascript
app.get('/foo', (req, res, next) => {
Sentry.getHubFromCarrier(req).configureScope(scope => {
scope.setTag('foo', 'my-value');
});

throw new Error('foo');
});

app.get('/foo', (req, res, next) => {
Sentry.getHubFromCarrier(req).configureScope(scope => {
scope.setTag('foo', 'my-value');
});

if (someCheck()) {
Sentry.getHubFromCarrier(req).captureMessage('Invalid eventId');
next();
} else {
res.end();
}
});
```

All `configureScope` calls that were done directly on initialized `Sentry` object, will be carried to each request, so it's a good way to set some data globally.