Allow error handling on the server
See original GitHub issueAt 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:
- Created 3 years ago
- Reactions:4
- Comments:15 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That would be great, as now we can’t show a 404 page becuase we always get a 200 😕
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