Skip to content

Commit d0f4035

Browse files
committed
docs: Explain getHubFromCarrier in express
1 parent 2d8c7f4 commit d0f4035

File tree

1 file changed

+40
-7
lines changed
  • src/collections/_documentation/platforms/javascript

1 file changed

+40
-7
lines changed

src/collections/_documentation/platforms/javascript/express.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@ sidebar_order: 1010
44
---
55

66
<!-- WIZARD -->
7+
78
Our Express integration only requires _@sentry/node_ to be installed then you can use it like this:
89

910
```javascript
1011
const express = require('express');
1112
const app = express();
12-
const Sentry = require('@sentry/node') ;
13+
const Sentry = require('@sentry/node');
1314

14-
Sentry.init({ dsn:'___PUBLIC_DSN___' });
15+
Sentry.init({ dsn: '___PUBLIC_DSN___' });
1516

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

1920
app.get('/', function mainHandler(req, res) {
20-
throw new Error('Broke!');
21+
throw new Error('Broke!');
2122
});
2223

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

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

3435
app.listen(3000);
@@ -55,4 +56,36 @@ app.use(Sentry.Handlers.errorHandler());
5556
// to
5657
app.use(Sentry.Handlers.errorHandler() as express.ErrorRequestHandler);
5758
```
59+
60+
## Working with Scopes and Hubs
61+
62+
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.
63+
64+
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:
65+
66+
```javascript
67+
app.get('/foo', (req, res, next) => {
68+
Sentry.getHubFromCarrier(req).configureScope(scope => {
69+
scope.setTag('foo', 'my-value');
70+
});
71+
72+
throw new Error('foo');
73+
});
74+
75+
app.get('/foo', (req, res, next) => {
76+
Sentry.getHubFromCarrier(req).configureScope(scope => {
77+
scope.setTag('foo', 'my-value');
78+
});
79+
80+
if (someCheck()) {
81+
Sentry.getHubFromCarrier(req).captureMessage('Invalid eventId');
82+
next();
83+
} else {
84+
res.end();
85+
}
86+
});
87+
```
88+
89+
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.
90+
5891
<!-- ENDWIZARD -->

0 commit comments

Comments
 (0)