Skip to content
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

Error code from GraphQL is not correct in StrawberryShake #4596

Closed
1 task done
thomastvedt opened this issue Dec 27, 2021 · 8 comments
Closed
1 task done

Error code from GraphQL is not correct in StrawberryShake #4596

thomastvedt opened this issue Dec 27, 2021 · 8 comments

Comments

@thomastvedt
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I expected my StrawberryShake client to return error codes from the GraphQL response, but result.Errors[0].Code is null. The error code can be found inside result.Errors[0].Extensions["body"], but I assume that this is not how this is intended to work? Perhaps some sort of serialization bug?

I made a quick reproduction here: https://github.com/thomastvedt/HotChocolateBug

The following mutation:

mutation {
  saveUserEmail(userId:"123", email:"incalid_email") {
    id
    name
    age
    email
  }
}

results in the following GraphQL response:

{
  "errors": [
    {
      "message": "Invalid email",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "saveUserEmail"
      ],
      "extensions": {
        "message": "Invalid email",
        "stackTrace": "   at Server.Domain.UserService.SaveUserEmail(String id, String email) in /Users/thomastvedt/projects/HotChocolateBug/Server/Domain/UserService.cs:line 13\n   at Server.UserMutation.SaveUserEmail(String userId, String email, UserService userService) in /Users/thomastvedt/projects/HotChocolateBug/Server/UserMutation.cs:line 10\n   at HotChocolate.Resolvers.Expressions.ExpressionHelper.AwaitTaskHelper[T](Task`1 task)\n   at HotChocolate.Types.Helpers.FieldMiddlewareCompiler.<>c__DisplayClass9_0.<<CreateResolverMiddleware>b__0>d.MoveNext()\n--- End of stack trace from previous location ---\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)",
        "code": "CUSTOM_ERROR_CODE"
      }
    }
  ]
}

When running the console app with the generated StrawberryShake client I get the following result:

image

I was expecting to find my CUSTOM_ERROR_CODE under result.Errors[0].Code, but this is null.

Is this a bug or expected?

Steps to reproduce

I made a quick reproduction here: https://github.com/thomastvedt/HotChocolateBug

  1. Run server (HotChocolate server)
  2. Run console app (StrawberryShake client)

Relevant log output

No response

Additional Context?

No response

Product

Strawberry Shake

Version

12.4.1

@thomastvedt
Copy link
Author

In the generated code:

        public global::StrawberryShake.IOperationResult<ISaveUserEmailResult> Build(global::StrawberryShake.Response<global::System.Text.Json.JsonDocument> response)
        {
            (ISaveUserEmailResult Result, SaveUserEmailResultInfo Info)? data = null;
            global::System.Collections.Generic.IReadOnlyList<global::StrawberryShake.IClientError>? errors = null;
            if (response.Exception is null)
            {
                try
                {
                    if (response.Body != null)
                    {
                        if (response.Body.RootElement.TryGetProperty("data", out global::System.Text.Json.JsonElement dataElement) && dataElement.ValueKind == global::System.Text.Json.JsonValueKind.Object)
                        {
                            data = BuildData(dataElement);
                        }

                        if (response.Body.RootElement.TryGetProperty("errors", out global::System.Text.Json.JsonElement errorsElement))
                        {
                            errors = global::StrawberryShake.Json.JsonErrorParser.ParseErrors(errorsElement);
                        }
                    }
                }
                catch (global::System.Exception ex)
                {
                    errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(ex.Message, exception: ex, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
                }
            }
            else
            {
                errors = new global::StrawberryShake.IClientError[]{new global::StrawberryShake.ClientError(response.Exception.Message, exception: response.Exception, extensions: new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>{{"body", response.Body?.RootElement.ToString()}})};
            }

            return new global::StrawberryShake.OperationResult<ISaveUserEmailResult>(data?.Result, data?.Info, _resultDataFactory, errors);
        }

I guess response.Exception is not null. The GraphQL request returns http status 500. So the strawberryshake client handles this as a "serious" unknown 500 bad exception? Am I supposed to return http status code 200 from the server in this case?

@thomastvedt
Copy link
Author

https://github.com/thomastvedt/HotChocolateBug/blob/8a4723e40afddd18fe40577b44b45bf72f92602d/Server/CustomHttpResultSerializer.cs#L25-L27

If I add a custom IHttpResultSerializer and make sure that my server returns http status code 200 for my custom, "expected" errors, everything is mapped ok in StrawberryShake.

But I'm still not sure how this is supposed to work.
Am I supposed to return http status 200 for "known errors"?
GraphQL doesn't really care about http status codes, perhaps SS shouldn't care either?

@michaelstaib michaelstaib added this to the HC-12.6.0 milestone Jan 5, 2022
@michaelstaib michaelstaib modified the milestones: HC-12.6.0, HC-12.7.0 Jan 23, 2022
@michaelstaib michaelstaib modified the milestones: HC-12.7.0, HC-13.0.0 Mar 9, 2022
@PascalSenn PascalSenn added the 🙋 good first issue Good for newcomers label Mar 16, 2022
@stale
Copy link

stale bot commented May 15, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the ⌛ stale Nothing happened with this issue in quite a while label May 15, 2022
@PascalSenn PascalSenn added 📌 pinned and removed ⌛ stale Nothing happened with this issue in quite a while labels May 15, 2022
@michaelstaib michaelstaib modified the milestones: HC-13.0.0, HC-13.1.0 Feb 7, 2023
@chrisdrobison
Copy link
Contributor

@michaelstaib @PascalSenn I'd like to take a crack at this one

@michaelstaib
Copy link
Member

Are you on slack? Slack.Chillicream.com? Ping me there

@chrisdrobison
Copy link
Contributor

Just pinged you

chrisdrobison added a commit to chrisdrobison/graphql-platform that referenced this issue Apr 10, 2023
michaelstaib added a commit that referenced this issue Apr 11, 2023
Co-authored-by: Michael Staib <michael@chillicream.com>
michaelstaib added a commit that referenced this issue Apr 11, 2023
Co-authored-by: Michael Staib <michael@chillicream.com>
@michaelstaib
Copy link
Member

This issue is now fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants