Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: RSC error - allow error.code or an other specialized prop to be forwarded to the client #27362

Closed
benji24290 opened this issue Sep 12, 2023 · 1 comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@benji24290
Copy link

benji24290 commented Sep 12, 2023

I've observed that React Server Components currently send a digest to the client in case of an error, without forwarding the exact error. This behavior can sometimes be limiting, especially when we want to handle specific error cases on the client-side, like the 401 Unauthorized status.

To tackle this, I've introduced a mechanism that selectively forwards specific error codes to the client, without compromising on security or exposing sensitive error messages.

Possible solutions:

  1. Forwarding the error.code Property:
  • Altered the server component's response handler to recognize and extract the error.code property during error scenarios.
  • If the error.code is present and matches our predefined whitelist, it's forwarded to the client. This allows the client to understand specific error types without exposing detailed server messages.
  1. Custom Digest Configuration:
  • Implemented a mechanism for developers to provide a custom digest.
  • The default behavior remains to send the generic digest unless the developer specifies this custom function, providing flexibility without compromising security.
  1. Introduction of a Specific Error Prop:
  • Introduced a new property to the error object, named clientHint. This prop can be set on the server-side and is designed to provide hints or specific behaviors that the client should adopt upon encountering the error.
  • On the client-side, React components can now access this clientHint prop and act accordingly, giving developers a structured way to pass actionable instructions from server to client during error scenarios.

Benefits of this Change:

  • Efficient Error Handling: Allows client-side code to react immediately based on the error code, without making an additional request.
  • Enhanced User Experience: Enables smoother user redirects or prompts based on specific error scenarios.
  • Developer Flexibility: By making the whitelist configurable, we provide developers control over error handling without compromising on security.
    Looking Forward:

Looking forward to the community's thoughts!

Thank you!

@benji24290 benji24290 added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Sep 12, 2023
@benji24290 benji24290 changed the title Request: RSC error - allow error.code or an other specialized prop to be forwarded to the client [Feature Request]: RSC error - allow error.code or an other specialized prop to be forwarded to the client Sep 12, 2023
@sophiebits
Copy link
Collaborator

The onError function in the configuration you pass can return an error digest, which should solve your use case.

For example Next.js does it here for a couple custom error types: https://github.com/vercel/next.js/blob/ab7b0f59fb0d793fb4ee593ae93f7e08bab23b5b/packages/next/src/server/app-render/create-error-handler.tsx#L37-L45

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants