Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get more error details or logging, when an exception is thrown in a HotChocolate GraphQL server?

I’m building out a simple HotChocolate GraphQl server and HotChocolate throws an Unexpected Execution Error, but doesn't expose any information about the error, as soon as I post a request against it. It doesn't matter how I post the request against the backend (BananaCakePop, Postman, Insomnia, ...).
The reponse looks like this:

{
  "errors": [
    {
      "message": "Unexpected Execution Error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "pong"
      ]
    }
  ],
  "data": {
    "pong": null
  }
}

The request response contains no further information and nothing is logged to the applications console. What would be a reasonable next step to try and figure out what went wrong?

like image 301
FredericBirke Avatar asked Jan 17 '21 18:01

FredericBirke


People also ask

How does a GraphQL server deal with failures?

On GraphQL errorsThe onError link can retry a failed operation based on the type of GraphQL error that's returned. For example, when using token-based authentication, you might want to automatically handle re-authentication when the token expires.


1 Answers

Per default HotChocolate does not expose the details of your exceptions, if no debugger is attached. So to get your error message you could either:

  • Attach a debugger to your server, which in turn will then expose the exceptions details in the request (a.k.a. start your server in debug :D)
  • Change this behavior in a way that better suits your development style.

This is how you can change the default behavior in V11 or V12:

public class Startup
{
    private readonly IWebHostEnvironment _env;

    public Startup(IWebHostEnvironment env)
    {
        _env = env;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddGraphQLServer()
            ...
            // You can change _env.IsDevelopment() to whatever condition you want.
            // If the condition evaluates to true, the server will expose it's exceptions details
            // within the reponse.
            .ModifyRequestOptions(opt => opt.IncludeExceptionDetails = _env.IsDevelopment());  
    }
}

This is how you can change the default behavior in V10:

public class Startup
{
    private readonly IWebHostEnvironment _env;

    public Startup(IWebHostEnvironment env)
    {
        _env = env;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGraphQL(
            Schema.Create(builder =>
            {
                ...
            }),
            // You can change _env.IsDevelopment() to whatever condition you want.
            // If the condition evaluates to true, the server will expose it's exceptions details
            // within the reponse.
            new QueryExecutionOptions {IncludeExceptionDetails = _env.IsDevelopment()}
        );
    }
}

You can also add an IErrorFilter to you application, that could, for example, log your exceptions to your locale console, or transform Exceptions to GraphQlErrors. For more information about this topic check:

  • V12: TODO (There are currently no official V12 docs about this topic)
  • V11: TODO (There are currently no official V11 docs about this topic)
  • V10: https://chillicream.com/docs/hotchocolate/v10/execution-engine/error-filter/#exception-details
like image 183
FredericBirke Avatar answered Oct 17 '22 03:10

FredericBirke