Next.js Discord

Discord Forum

Accessing Error Digest On Server

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Avatar
Northeast Congo LionOP
Hello! I have a NextJS application using OpenTelemetry for serverside tracing. I'd like to add the error digest generated on server errors to the spans that are created by NextJS. How can I access this error digest on the server, or how can I access the real error on the client?

41 Replies

Avatar
you can't naturally get real error on the client in production. You can use try catch to catch the error and manually redirect to an error page with querystring as the error message
Make sure to re-throw error if error.message === NEXT_REDIRECT
I'd like to add the error digest generated on server errors to the spans
If you print out the error from the parameter of error.tsx, then you can get the digest code
Avatar
Northeast Congo LionOP
I got the digest code on the client, but I can't find it anywhere on the server to make the correlation.

Is there a way to do a global try/catch? I was thinking middleware but I dont think that would help here
Avatar
have you checked the vercel logs?
i usually find the error there
Avatar
Northeast Congo LionOP
This is a self hosted site running in a container
Avatar
huh, does any console.log gets logged in there?
if so you can just make your own digest code
Avatar
Northeast Congo LionOP
Not that I can see
How would I make the digest code?
Avatar
const digest = crypto.randomUUID(),
console.log it,
then also
return 
  <div>
    { digest } 
  </div>
Avatar
Northeast Congo LionOP
on the server side?
Avatar
yeah
because the error happens in the server. It can't risk leaking sensitive server info thats why it gets converted to digest before going to the client side
Avatar
Northeast Congo LionOP
Ok interesting, I'll look into this. Is there a way to catch all server errors? Sort of like errors.js on the client?
I have quite a few pages and I dont want to manually wrap try/catch blocks in each of them
Avatar
no there isn't afaik
but server error should dynamically reroute to the errors.js
if its an SSR
Avatar
Northeast Congo LionOP
right but once the error hits errors.js it's already been converted to the digest, so I dont have access to the originally error
unless I'm doing something wrong there
Avatar
well it should be logged in the server too :/
does any console log gets logged in your console?
Avatar
Northeast Congo LionOP
yeah 😦 I'll probably switch my focus to figuring that part out
yeah we have console logs, and I see some server logs but I dont see any that look to be generated by next
Avatar
Image
this is pnpm start, on prod, the error and the server log is displayed
Avatar
Northeast Congo LionOP
So where is the digest on your server log?
Avatar
there
look at the bottom line
Avatar
Northeast Congo LionOP
oh this is the server log, ok
yeah that would be perfect. Ok, I need to figure that out then
I appreciate the help!
One last question though, what did you mean by this?
Make sure to re-throw error if error.message === NEXT_REDIRECT
Avatar
if you use redirect('/something') in your server code,
what its actually doing is to throw an error
and it gets consumed by next.js and redirect to the proper url
if you catch it then you will just print out the instruction to redirect. so you need to re-throw it :v
Avatar
Northeast Congo LionOP
Makes sense, thanks!
Avatar
Northeast Congo LionOP
I ran a build locally and I can see the server error log, including the digest. Not sure why that isn't making it into Datadog. So that definitely is what I need to focus on.

Thanks again for all the help!