-
-
Notifications
You must be signed in to change notification settings - Fork 749
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
Please add sufficent error handling example with HTTP status codes to documentation #5549
Comments
This actually is a wrong assumption. When we talk about known error, or lets say domain errors. Things that are expected with stage 6a then you do not report it as an error et all.
Stage 6a is about exposing errors in you schema which we would do by either annotating the exceptions that are domain errors or using the Essentially ... [Error<SomeInputHasWrongFormatException>]
public async Task<SomeResult> DoSomething(string someInput) |
Does this mean that both FairyBread and AppAny.HotChocolate.FluentValidation are incorrectly calling
If that's true:
As mentioned on Slack, a validation error when using the AppAny library is currently resulting in a 500 code, and an errors-only response (even when submitting multiple mutations in the same request, where one or more are successful). This is not good at all. I really am looking forward to having comprehensive error handling documentation for v13, as well as part two of the video, covering error handling for queries. Edit: There are other cases where we'd like to surface domain exceptions without having to annotate every single mutation. Maybe the mutation convention configuration could allow you to specify a list of exceptions to be included "globally"? |
I'm currently running into this wall as well. I, personally, would prefer not to use the 6a convention (because of style preference). throw new GraphQLException(new ErrorBuilder().SetCode("AccountAlreadyExists").SetMessage("Account already exists.").Build()); But that doesn't seem to go along well with the apollo client (doesn't expose the errors information to the mutate function). Probably because of the 500 status code? |
What about Domain Errors on queries? [Error] seem to work only for mutations. |
Postman and BCP behaves differently in terms of response http status code with FairyBread. Postman returns 200 whereas BCP returns 500 if there is any input validator fails. The reason is that Postman in GraphQL mode always sends "Accept: application/json" header in the request, which tells HC server to use legacy mode (explained here https://chillicream.com/docs/hotchocolate/v13/server/http-transport). If you add the same header in BCP it doesn't return 500 but returns 200 just like Postman. If you don't want to add the header to client request you can configure the server.
|
Is your feature request related to a problem?
In the current state of docs, it's unclear how the HTTP status interceptor determines the status code related to exception/returned data value in the resolver.
If you want to implement the 6a pattern and return 200 OK every time a "known" error occurs (for example validation issue), you have to do one of these things:
a) always return nullable! (otherwise, you'll get 500)
b) try-catch the exception -> report exception with
resolverCtx.ReportError(...)
-> return data/null.Some people (including myself) don't automatically implement nullable return values, which results in non-sense 500 errors. This behavior of the HotChocolate pipeline should be documented in docs.
Note
This also affects errors related to authentication, if - for example - I return user data that are C# record (non-nullable by nature) and the user isn't authorized, it shoots out 500 HTTP code. When you make the user data record nullable, you get 200 HTTP code. (correct behavior, but this 'authorize' attribute should be namely documented because normally you don't even have to return null in resolver).
The solution you'd like
Add HotChocolate HTTP status code and error behavior in docs.
Product
Hot Chocolate
The text was updated successfully, but these errors were encountered: