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

Viewed 6407

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?

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:

Related