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

Globally register error handling #1285

Open
amannn opened this issue Aug 26, 2024 · 1 comment
Open

Globally register error handling #1285

amannn opened this issue Aug 26, 2024 · 1 comment
Labels
area: ergonomics enhancement New feature or request

Comments

@amannn
Copy link
Owner

amannn commented Aug 26, 2024

Is your feature request related to a problem? Please describe.

Currently, error handling is somewhat cumbersome to set up. It needs to be set up both in i18n.ts as well as NextIntlClientProvider to cover both Server- as well as Client Components.

Most configuration from i18n.ts can automatically be inherited by Client Components, but for onError and getMessageFallback this is not the case since functions aren't serializable. Therefore this requires workaround (see e.g. #1226).

Ideally, we'd let users register error handling once and use it across a whole app without workarounds (regardless of RSC/SSR/CSR).

Describe the solution you'd like

If Next.js would allow client-side instrumentation, we could use instrumentation.ts for this use case. This would be the preferred solution from my perspective.

// instrumentation.ts

import {onError, configureMessageFallback, IntlErrorCode} from 'next-intl';

// Called for both `react-server` as well as `react-client`
export async function register() {
  // Called when an error is encountered during formatting
  onError((error) => {
    if (error.code === IntlErrorCode.MISSING_MESSAGE) {
      // Missing translations are expected and should only log an error
      console.error(error);
    } else {
      // Other errors indicate a bug in the app and should be reported
      reportToErrorTracking(error);
    }
  });

  // Called to provide a fallback message when formatting has failed
  configureMessageFallback(({namespace, key, error}) => {
    const path = [namespace, key].filter((part) => part != null).join('.');

    if (error.code === IntlErrorCode.MISSING_MESSAGE) {
      return path + ' (fallback)';
    } else {
      return path + ' (formatting error)';
    }
  });
}

Implementation-wise we'd likely write to a global store if these functions are called in order to register the handler.

Describe alternatives you've considered

Alternatively, we could explore automatically creating an onError server action that can be serialized across the network. This wouldn't work for getMessageFallback though.

(this issue was created after working on a first draft on feat/error-instrumentation)

@amannn
Copy link
Owner Author

amannn commented Aug 28, 2024

Seems like there's something cooking on the Next.js side: https://x.com/huozhi/status/1828726448892354741. We should probably wait for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: ergonomics enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant