Skip to content

Commit

Permalink
docs: Explain getHubFromCarrier in express
Browse files Browse the repository at this point in the history
docs: Explain getHubFromCarrier in express
  • Loading branch information
kamilogorek authored Oct 9, 2018
2 parents 2d8c7f4 + e23eebd commit da7ebbc
Showing 1 changed file with 41 additions and 7 deletions.
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.

0 comments on commit da7ebbc

Please sign in to comment.