-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Some variable-related errors should be BAD_USER_INPUT rather than INTERNAL_SERVER_ERROR #5353
Comments
#5091 shows how to fix issues like this with tests. PRs welcome! (Note that right now before 3.0.0 has been released, |
I can take this up.
I couldn’t find any issue in graphql-js related to providing a way to differentiate bad input user validation errors from other errors. Wouldn’t it be great if graphql-js provides us a code in extensions like : onError(
new GraphQLError(
`Variable "$${varName}" of non-null type "${varTypeStr}" must not be null.`,
varDefNode,
undefined,
undefined,
undefined,
undefined,
{code:"BAD_USER_INPUT"}
), (I confirmed it by making this change and checking e.extensions.code == "BAD_USER_INPUT" and returning UserInputError in requestPipeline.js file. it works )
And I'm not sure if we also need to handle the errors coming from getArgumentValues cause they too look like bad user input errors :
Let me know you inputs on this. if(e.message.includes(`Variable`) && e.message.includes(`of non-null type`) && e.message.includes(`must not be null`) Reason for doing this instead of fetching the varName and varTypeStr and replacing it and then matching the string it includes a bit of effort with interpolation like for varTypeStr it would be: e.message.startsWith(
`Variable "$${e.nodes[0].variable.name.value}" of non-null type "${nodes[0].type.type.name.value}!" must not be null`,
) |
I think for the long term it would be great if graphql-js had more machine-parseable errors (presumably adding something on In the short term we should probably do something like #5091 so we can make progress. The main reason I feel comfortable with #5091 is that it does have a test so we will learn if new graphql-js versions change the error messages. I do also like how we link together the node from the error to the error message... I don't think we have to worry about the errors in
Note that right now, active development is being done on |
Hey @glasser I've linked a PR do let me know if there are any changes to be made in it. :) |
The function
coerceVariableValues
in https://github.com/graphql/graphql-js/blob/main/src/execution/values.ts can throw several errors if the provided variable values aren't good. These are all bad user input, not server bugs, so Apollo Server should throw them as UserInputError rather than internal server errors. Unfortunatelygraphql-js
doesn't give a great way of differentiating these errors from errors in server code and so we treat them like internal server errors.In #5091 we fixed one of these errors (
Variable "$${varName}" got invalid value
) to throw UserInputError but other errors likeVariable "$${varName}" of required type "${varTypeStr}" was not provided
andVariable "$${varName}" of non-null type "${varTypeStr}" must not be null
still show up as internal server errors. See #3498 (comment) for a codesandbox that demonstrates both issues. We should make them both into UserInputError.The text was updated successfully, but these errors were encountered: