nextjs handling server-side errors in error page
I'm using Nextjs, and I've setup a catch all error page to handle all errors, I've set it up to show formatted convex Errors if it's
instanceof ConvexError
see code below:
This works fine when the error comes from client side, but it completely breaks for server side requests like fetchQuery
. I've looked through the docs and this is not addressed at all.
here's the error. As you can see it's not the usual that has the data
key.
Any help with this would be appreciated.
Thanks5 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
Next.js tampers with your server Error, for your protection. https://nextjs.org/docs/14/app/building-your-application/routing/error-handling#handling-server-errors
This is a server component, so handle it in a server way: with a try/catch. You have access to your data object there - you can decide what to send to the client.
Routing: Error Handling | Next.js
Handle runtime errors by automatically wrapping route segments and their nested children in a React Error Boundary.
Thanks that was helpful.
I figured rather than using
fetchQuery
and not be able to access the ConvexError
data, I'll just use prefetchQuery
. This has the benefit of avoiding loading state, while still being able to handle query errors.It's a great option as long as your needs aren't too complex - you can't change the query args, and it doesn't work with paginated queries. You'll need to use fetchQuery for those.
I hadn't used this set up in a while so I made a little demo in my app to check it. It looked like the data object had been moved in the message by Next. Maybe you could check for an instanceof ConvexError that lacks a data prop - in that case you could try to JSON.parse the data out of the message.
Not sure how reliable that would be, but it's something you could try if you needed a fetchQuery. But dealing with it directly in a server component much more reliable, in my opinion.
for my use cases so far the
prefetchQuery
is good enough. I won't be willing to JSON.parse
the error object nextjs give you. It seems to finiky and wrapping a react component in a trycatch
seems odd. Thank you so much for the suggestions though. I think I'll be dealing with pagination soon so I'll do some experimentation.