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

Handle uncaught errors and display notification #80

Open
pyramation opened this issue Jan 14, 2022 · 2 comments
Open

Handle uncaught errors and display notification #80

pyramation opened this issue Jan 14, 2022 · 2 comments

Comments

@pyramation
Copy link
Contributor

pyramation commented Jan 14, 2022

I'm curious, how are you handling uncaught errors?

I've been getting one particular ApolloError that leads me to the mutate call inside of a graphql call, a bit low level and for some reason doesn't get caught and displayed.

Is there a way to update the data provider to catch errors and display them in the react-admin notifications?

While I would believe the data provider should likely handle this, I've been able to hook into it by customizing the apollo client to catch global errors, but I'm having trouble actually displaying a notification as I imagine there is a react-admin standard way to do this that I'm unaware of.

const errorLink = onError(({ response, graphQLErrors, networkError }) => {

  if (graphQLErrors)
    graphQLErrors.map(({ message, locations, path, extensions }) => {
      if (extensions && extensions.code === 'UNAUTHENTICATED') {
        // edit response so downstream can cat an error message
        response.errors = [new Error('UNAUTHENTICATED')];
      } else {

        // WE COULD RENDER THE NOTIFICATION HERE

        console.log(`[NOTIFICATION]: ${extensions?.code || message}`);
      }
      console.log(
        `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
      );
    });

  if (networkError) console.log(`[Network error]: ${networkError}`);
});

const link = from([
  authLink,
  errorLink,
  createUploadLink({
    uri
  }),
  new HttpLink({
    uri,
    credentials: 'include'
  })
]);

const client = new ApolloClient({
  link,
  cache: new InMemoryCache(),
  defaultOptions: {
    watchQuery: {
      fetchPolicy: 'no-cache',
      errorPolicy: 'ignore'
    },
    query: {
      fetchPolicy: 'no-cache',
      errorPolicy: 'all'
    }
  }
});
@BowlingX
Copy link
Owner

Hey :),
sorry for the late reply.

As you create the apollo client in the react root application, you could technically pass a callback and do custom client side notifications. As those are just using the standard material-ui notification components, you could use them to show a custom error notification or handle a login for example.

For authentication though I actually wrap the Root application into a hoc which does a graphql query to check the authentication state with errorPolicy: 'all'. And if the user is not logged in I (in my case) do a redirect to the oauth flow.

@pyramation
Copy link
Contributor Author

I think the issue is that some of the errors aren't getting caught in the right place for the app.

For me authentication is an easy redirect we can handle, but other simple errors are causing issues. For example, if you make a unique constraint on a postgres table, and try to insert a record that would create an error on the unique index, the ra-postgraphile and react-admin just throw an error in the console.

Ideally in the example of a unique index or any index error, would be great to propagate the error message into the normal error system that react admin seems to have in place.

This one example of unique indexes feels decently normal case to solve without special cases, I'm happy to make a PR or something if you can point me to the right place.

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

No branches or pull requests

2 participants