Pass req in Sentry.captureException
It looks like Raven was able to take context information including a req parameter, but Sentry isn't allowing me to to do this. So something like this:
Sentry.captureException(Error("An error"), {
user: { id: 123 },
req: req, // Request object from HTTP web server
tags: { component: 'main', method: 'doSomething' },
extra: { catType: cat.type },
level: 'error'
})
Is this still possible with Sentry? If not, how can I achieve the same thing (especially sending the req)?
It is in captureEvent, but I'm not sure what extra work captureException does.
I am also in the process of migrating our error reporting from Raven to Sentry and have run into this same issue. I am using koa2 as our web framework.
Previously, when I passed in the request object - Raven would magically parse it and attach all relevant fingerprint and etc. data for it to be formatted.
I attempted to do the same using captureEvent, but it doesn't seem to have the same behavior.
Is there something that I'm missing when passing the request to captureEvent or is there a different way I should be adding this data?
I found this: https://github.com/hydra-newmedia/hapi-sentry/blob/master/index.js
But imo we shouldn't have to deal with internal functions like that.
Yah, the main thing I am trying to do is pass API errors to Sentry. These are "caught" by us but I was hoping to use Sentry to track them.
What we are passing right now is resulting in the "Non-Error exception captured with keys" message and the following extra info getting passed:
{
config: {
headers: {},
jsonpCallbackParam: callback,
method: GET,
params: {},
paramSerializer: [Function: ngParamSerializer],
transformRequest: [
[Function]
],
transformResponse: [
[Function: defaultHttpResponseTransform]
],
url: http://example.test
},
data:
<!DOCTYPE html>
<html lang="en">
<head>…
,
headers: [Function],
status: 404,
statusText: Not Found,
xhrStatus: complete
}
Anyone have recommendations on how to handle this? I was working to make a custom javascript error type when I hit this issue (because there is no easy way to pass these additional fields along).
We dropped the second "magic" arbitrary object in captureException the new official way of doing this is setting stuff on the scope like:
Sentry.withScope(scope => {
scope.setExtra('debug', false);
// scope.setTag
// scope.setUser
// scope.setLevel
Sentry.captureException(e);
});
Sending the req or something else, either requires to add it in the beforeSend option.
Or add a custom eventProcessor to the scope:
Sentry.withScope(scope => {
scope.addEventProcessor(event => {
// change anything in the event here
});
Sentry.captureException(e);
});
Hope this helps.
Btw. this should help to migrate: https://github.com/getsentry/sentry-javascript/releases/tag/4.0.0
Then why does captureEvent provide this API but doesn't explain how to use it to get the desired behaviour?
It seems like you already implemented the bits we want but now are telling us implement it yourself which leads to code duplication, I'm not sure if I can follow that logic.
You can call captureEvent but you need to fully build the event by yourself.
This function basically exists only if someone wants to build their own SDK which does something like that.
captureException under the hood does way more to extract the stack trace and so on.
So what's the best way to replicate what happened before without us needing to build the event ourselves? It seems even with the event processor I would still need to fill in exact fields manually.
For example (in koa2), when I passed the request using Raven.captureException, I would simply pass in the ctx.request and it would correctly auto-populate the user tags and headers.
Currently, when I pass in ctx.request in scope.addEventProcessor as event.request the only tag that shows up correctly is the url.
@vhu I guess our docs can be improved here: https://docs.sentry.io/platforms/node/koa/
But you would do something in the lines of in your custom scope.addEventProcessor do:
Sentry.Handlers.parseRequest(event, req);
If that solves it we can add it to the docs.
@HazAT Thanks, it's works.
My solutions for Restify 4.x:
const server = restify.createServer({
handleUncaughtExceptions: true,
})
const onError = (req, res, route, error) => {
Sentry.withScope(scope => {
// scope.setTag()
// scope.setUser()
scope.addEventProcessor(async event => {
return Sentry.Handlers.parseRequest(event, req)
})
Sentry.captureException(error)
})
}
server.app.on('uncaughtException', (req, res, route, error) => {
onError(req, res, route, error)
res.send(new restify.errors.InternalServerError(error))
})
// Loggin RestError/NotFound/etc
// server.app.on('after', onError)
Thanks, @HazAT! That solved it for koa2 as well. The added line is:
Sentry.Handlers.parseRequest(event, ctx.request);
Hi @HazAT! I'm not sure if this is the correct place to ask this question 😅, but yesterday I was trying to migrate my project to the new API, but now when I do something like:
Sentry.withScope(scope => {
scope.setTag('type', 'client');
scope.setExtra('custom-message', 'hello');
Sentry.captureException(error.originalError || error);
});
The error is being send successfully but without the data I just attached to the scope (in this case a tag and an extra).
So, I was wondering if there is something I'm missing or doing incorrectly.
@juanhenriquez this is perfectly fine and should work just fine. Can you provide a repr-case where it fails?
Sure @kamilogorek! Here you go https://codesandbox.io/s/jl4nx3yp7v
I'm throwing an error manually from my app.component.ts for testing purposes.
I can absolutely second that updating the Koa documentation with that snippet would be a huge help. Took me quite a bit of time to figure out why my captured exceptions were missing the request meta data.
Right now it only advocates:
app.on('error', err => {
Sentry.captureException(err);
});
but changing that to:
app.on('error', (err, ctx) => {
Sentry.withScope((scope) => {
scope.addEventProcessor(async (event) => Sentry.Handlers.parseRequest(event, ctx.request));
Sentry.captureException(err);
});
});
would be a huge improvement and ensure people get all the request info attached to their requests.
@mirague Thanks, here is the PR https://github.com/getsentry/sentry-docs/pull/901