question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow error handling on the server

See original GitHub issue

At the moment, errors are silently caught and ignored on the server when running getDataFromTree here. This means even if the graphql server sends back an error in query results, the NextJS server does not know this and will always return 200 which is bad for SEO.

It would be nice to be able to pass an error handler to the options to be able to handle this.

For example, this is what’s happening in my app:

// ComponentA.tsx

const ComponentA = () => {
  const { data, error } = useGetUserQuery();

  if( error ){
    // I want to throw an error with code "500" in here to be caught when running on the server
    // On the client, it shows an error page
    return <Error500 />
  }

  if( !data ||  !data.user ) {
    // I want to throw an error with code "404" in here to be caught when running on the server
   // On the client, it shows an error page
    return <Error404 />
  }
  ...
}

If withApollo uses a function passed in from the options, we can update the context so NextJS returns correct status code:

const MyAppWithApollo = withApollo(
  ({ initialState, headers }) => createApolloClient({ uri: process.env.GRAPHQL_ENDPOINT, initialState, ssrHeaders: headers }),
  {
    getDataFromTree,
    handleServerError: ( ctx, err ) => {
         ctx.res.statusCode = err.code; // This is an example, may need more checks in prod
    }
  }
)(MyApp);

Happy to create a PR if this makes sense 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
Emiliano-Buccicommented, May 12, 2020

That would be great, as now we can’t show a 404 page becuase we always get a 200 😕

2reactions
rajingtoncommented, May 27, 2020

thanks, any idea when we can merge it and re-publish a package? we’re currently using this and don’t want to use patch-package

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices for REST API Error Handling - Baeldung
3.1.​​ The simplest way we handle errors is to respond with an appropriate status code. Here are some common response codes: 400 Bad...
Read more >
Web API Error Handling: How To Make Debugging Easier
Whether you're the consumer or producer of an API, you've no doubt seen that good error handling can make debugging easier.
Read more >
How to implement error handling in SQL Server - SQLShack
This article explains how to implement SQL error handling using the SQL server try-catch statement and what can be done in general when...
Read more >
Handling operation errors - Apollo GraphQL Docs
Apollo Client helps you handle these errors according to their type, enabling you to show appropriate information to the user when an error...
Read more >
Guide to Spring Boot REST API Error Handling - Toptal
Spring Boot Error Handler. Let's explore some Spring annotations used to handle exceptions. RestController is the base annotation for classes that handle ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found