Skip to content

Commit 7b0b33a

Browse files
committed
Merge pull request #116 from getsentry/async-request-handler
Handle asyncrhonous errors
2 parents b725bd5 + 462af01 commit 7b0b33a

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ The callback is called **after** the event has been sent to the Sentry server.
153153

154154
## Integrations
155155
### Connect/Express middleware
156-
The Raven middleware can be used as-is with either Connect or Express in the same way. Take note that in your middlewares, Raven must appear _after_ your main handler to pick up any errors that may result from handling a request.
156+
The Raven middleware can be used as-is with either Connect or Express in the same way.
157157

158-
#### Connect
158+
#### Connect and Express
159159
```javascript
160160
var connect = require('connect');
161161
function mainHandler(req, res) {
@@ -168,22 +168,34 @@ function onError(err, req, res, next) {
168168
res.end(res.sentry+'\n');
169169
}
170170
connect(
171+
// Should be the first item listed
172+
raven.middleware.connect.requestHandler('{{ SENTRY_DSN }}'),
173+
171174
connect.bodyParser(),
172175
connect.cookieParser(),
173176
mainHandler,
174-
raven.middleware.connect('{{ SENTRY_DSN }}'),
177+
178+
// Should come before any other error middleware
179+
raven.middleware.connect.errorHandler('{{ SENTRY_DSN }}'),
175180
onError, // optional error handler if you want to display the error id to a user
176181
).listen(3000);
177182
```
178183

179184
#### Express
180185
```javascript
181186
var app = require('express')();
187+
182188
app.get('/', function mainHandler(req, res) {
183189
throw new Error('Broke!');
184190
});
185-
app.use(raven.middleware.express('{{ SENTRY_DSN }}'));
191+
192+
// Should be the first item listed
193+
app.use(raven.middleware.express.requestHandler('{{ SENTRY_DSN }}'));
194+
195+
// Should come before any other error middleware
196+
app.use(raven.middleware.express.errorHandler('{{ SENTRY_DSN }}'));
186197
app.use(onError); // optional error handler if you want to display the error id to a user
198+
187199
app.listen(3000);
188200
```
189201

lib/middleware/connect.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
var raven = require('../client');
22
var parsers = require('../parsers');
33

4-
module.exports = function connectMiddleware(client) {
4+
// Legacy support
5+
var connectMiddleware = function(client) {
6+
return connectMiddleware.errorHandler(client);
7+
};
8+
9+
// Error handler. This should be the last item listed in middleware, but
10+
// before any other error handlers.
11+
connectMiddleware.errorHandler = function(client) {
512
client = (client instanceof raven.Client) ? client : new raven.Client(client);
613
return function(err, req, res, next) {
714
var status = err.status || err.statusCode || err.status_code || 500;
@@ -16,3 +23,15 @@ module.exports = function connectMiddleware(client) {
1623
});
1724
};
1825
};
26+
27+
// Ensures asynchronous exceptions are routed to the errorHandler. This
28+
// should be the **first** item listed in middleware.
29+
connectMiddleware.requestHandler = function(client) {
30+
return function(req, res, next) {
31+
var reqDomain = domain.create();
32+
reqDomain.on('error', next);
33+
return reqDomain.run(next);
34+
};
35+
};
36+
37+
module.exports = connectMiddleware;

0 commit comments

Comments
 (0)