DEV Community

Cover image for Add Sentry to Vercel Next.js API Routes

Add Sentry to Vercel Next.js API Routes

Aryan J on May 10, 2020

To add Sentry to Next.js, you can wrap your entire route in a try block and have the Sentry reporting done in the catch block: // /pages/api/some...
Collapse
 
fredyc profile image
Daniel K.

Hey, I wonder, were you able to figure out out how to work with Sentry scopes in an async manner? I mean when I use configureScope with some additional details from the request that fails, but another request comes in the meantime which overrides those details so I end up with error report that has wrong details.

Collapse
 
holylander profile image
Alberto Suarez

Thanks for the tuto! it was really helpful :)
qq, how would you also track the trace of the event, as well as succesful events / transcations? ( aka, when the execution has gone well or without any error ) thanks!!

Collapse
 
scefali profile image
Stephen Cefali

@aryanjnyc Thanks for writing this blog. My question to you is why are you returning the error instead of throwing it to the caller function?

Collapse
 
aryanjnyc profile image
Aryan J • Edited

Ah, I actually found the root of my error. This serverErrorHandler is heavily inspired by existing Sentry docs that were recently changed. Here is the change this all stemmed from: github.com/getsentry/sentry-docs/c...

Either way, yeah, good find! Thanks a bunch. I also updated my pull request to the Sentry docs: github.com/getsentry/sentry-docs/p...

Collapse
 
aryanjnyc profile image
Aryan J

Oh my. Good catch. There's no reason for it and, in fact, that's definitely wrong. That should be a throw.

Collapse
 
scefali profile image
Stephen Cefali

@aryanjnyc Thanks for making the update! I do apologize that the documentation we had was incorrect.

Collapse
 
vvo profile image
Vincent Voyer

Hey there, do you know if this can be achieved without having to warp the full route in a try catch? I thought Sentry would just work by default without having to try catch the whole route.

Collapse
 
vvo profile image
Vincent Voyer

Ok found out that it's documented in the with-sentry example: github.com/vercel/next.js/blob/57e...