-
Notifications
You must be signed in to change notification settings - Fork 36
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
Errors returned by getServerPage() can't be serialized to JSON #207
Comments
ok, thanks for the idea, I will explore this option |
I just discovered that this is a non-issue when using babel-plugin-superjson-next, which handles JSON serialization of Error objects automatically (and also Date objects, etc). (See alos https://github.com/blitz-js/superjson#using-with-nextjs.) Obviously probably not everyone will want to use that plugin, but I suppose this should be an optional feature for those who are using it. |
@mbrowne How did you solve this? Does implementing the babel-plugin-superjson-next automatically solve this? I would definitely agree that an error message in plain object form would be awesome |
@Inlustra Yes, babel-plugin-superjson-next automatically solves this, because it looks at the whole response and automatically serializes anything that needs it (including errors and dates). |
In
getServerSideProps()
, it's not safe to return the object returned bygetServerPage
as-is. This is because errors inres.props.error
can't be serialized to JSON.This might not be a bug, but it does mean that you have to do your own error handling in every
getServerSideProps
function rather than handling that when you're rendering your component, or inpages/_app.tsx
to keep your code more DRY. And it's confusing, because I thought that a property namedprops
would be something you could safely return as the page props without modification.The issue can be reproduced most easily by faking a GraphQL error:
This causes a next.js Server Error:
It would be great if the generated code returned an
error
prop that can be properly stringified to JSON instead of returning the ApolloError or GraphqlErrors objects directly. Or alternatively maybe generate a function you can optionally use to do the conversion, something like this:...which would grab the error message(s) and return a plain object, e.g.:
The text was updated successfully, but these errors were encountered: