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

[React 19] Export SuspenseException #30993

Open
cevek opened this issue Sep 18, 2024 · 2 comments
Open

[React 19] Export SuspenseException #30993

cevek opened this issue Sep 18, 2024 · 2 comments

Comments

@cevek
Copy link

cevek commented Sep 18, 2024

Problem Statement

Currently, in React's use(promise) mechanism, there is no straightforward way to determine whether an exception originates from a promise suspended within the use hook. This makes it challenging for developers to:

  • Accurately catch and handle errors related to suspended promises.
  • Differentiate between errors caused by use(promise) and other unrelated errors, complicating error handling logic.

Proposal

React should export either:

  1. A SuspenseException class that can be used to identify errors originating from a promise suspension, or
  2. A utility function to check whether a given error is caused by a suspended promise in use(promise).

Example Usage

  1. SuspenseException Class:

    import { SuspenseException } from 'react';
    
    try {
      use(fetchData());
    } catch (error) {
      if (error === SuspenseException) {
        // Handle Suspense-related logic
      } else {
        // Handle other types of errors
      }
    }
  2. Utility Function:

    import { isSuspenseException } from 'react';
    
    try {
      use(fetchData());
    } catch (error) {
      if (isSuspenseException(error)) {
        // Handle Suspense-related logic
      } else {
        // Handle other types of errors
      }
    }

Benefits

  • Error differentiation: Clear distinction between promise suspensions and other errors.
  • Enhanced debugging: Easier diagnosis of Suspense-related issues in both development and production.
  • Safer error handling: Prevents unintended catches of non-Suspense errors during Suspense management.

Conclusion

By exporting either a SuspenseException or a utility function, React would offer developers more control over managing Suspense-related errors, improving both error handling and debugging in applications.

@cevek cevek added the React 19 label Sep 18, 2024
@artalar
Copy link

artalar commented Sep 18, 2024

Agree. Currently, the handling of suspense looks very unreliable, here is an example from a real code: https://github.com/artalar/reatom/blob/e6b042f273dbacb221ed77923f6c8582e22236dc/packages/npm-react/src/index.ts#L209-L210

@eps1lon
Copy link
Collaborator

eps1lon commented Sep 25, 2024

Have you considered the suggestions in the exception?

Suspense Exception: This is not a real error! It's an implementation detail of use to interrupt the current render. You must either rethrow it immediately, or move the use call outside of the try/catch block. Capturing without rethrowing will lead to unexpected behavior.\n\nTo handle async errors, wrap your component in an error boundary, or call the promise's .catch method and pass the result to use

Which use case are these suggestions not covering?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants