Accessing Error Digest On Server
Unanswered
Northeast Congo Lion posted this in #help-forum
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
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 spansIf you print out the error from the parameter of
error.tsx
, then you can get the digest codeNortheast 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
Is there a way to do a global try/catch? I was thinking middleware but I dont think that would help here
have you checked the vercel logs?
i usually find the error there
Northeast Congo LionOP
This is a self hosted site running in a container
huh, does any console.log gets logged in there?
if so you can just make your own digest code
Northeast Congo LionOP
Not that I can see
How would I make the digest code?
const digest = crypto.randomUUID(),
console.log it,
then also
console.log it,
then also
return
<div>
{ digest }
</div>
Northeast Congo LionOP
on the server side?
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
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
no there isn't afaik
but server error should dynamically reroute to the errors.js
if its an SSR
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 errorunless I'm doing something wrong there
well it should be logged in the server too :/
does any console log gets logged in your console?
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
this is
pnpm start
, on prod, the error and the server log is displayedNortheast Congo LionOP
So where is the digest on your server log?
there
look at the bottom line
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
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
Northeast Congo LionOP
Makes sense, thanks!
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!
Thanks again for all the help!