user19448827
user19448827

Reputation: 245

Next14 server component error - passing error metadata to client error boundary

The app router convention is to define an error.tsx error boundary (client component) that will receive a thrown error. I would like to throw an error on the server component and pass error metadata (status_code, type, and my own message). When I throw the error from the server component it gets passed to the error boundary, but my custom info gets stripped and I only get a simple message and digest. The message is:

Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.

Is there anyway to pass custom error metadata to the error boundary client component? I would like to show different things on the client based on whether its a 404, 422, or 500. Previously I was trying to stringify an error object by setting the error message:

// server component
error.message = JSON.stringify({'status_code":404, type: "NotFound", message: "resource not found"})
throw new error


//client error boundary component
JSON.parse(error) // gives error metadata

But this did not work and Next still seems to strip this info and give the generic error message shown above. Any suggestions?

Upvotes: 0

Views: 294

Answers (0)

Related Questions