简体   繁体   English

当 HotChocolate GraphQL 服务器中引发异常时,如何获取更多错误详细信息或日志记录?

[英]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.我正在构建一个简单的 HotChocolate GraphQl 服务器,HotChocolate 会抛出Unexpected Execution Error ,但不会公开有关该错误的任何信息,只要我发布针对它的请求。 It doesn't matter how I post the request against the backend (BananaCakePop, Postman, Insomnia, ...).我如何向后端发布请求并不重要(BananaCakePop、Postman、失眠……)。
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?尝试找出问题所在的合理下一步是什么?

Per default HotChocolate does not expose the details of your exceptions, if no debugger is attached.如果没有附加调试器,默认情况下 HotChocolate 不会公开异常的详细信息。 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 (aka start your server in debug:D)将调试器附加到您的服务器,然后它将在请求中公开异常详细信息(也就是在调试中启动您的服务器:D)
  • Change this behavior in a way that better suits your development style.以更适合您的开发风格的方式更改此行为。

This is how you can change the default behavior in V11:这是您可以在 V11 中更改默认行为的方法:

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:这是您可以在 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.您还可以将 IErrorFilter 添加到您的应用程序,例如,可以将您的异常记录到您的语言环境控制台,或将异常转换为 GraphQlErrors。 For more information about this topic check:有关此主题的更多信息,请查看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 HotChocolate 和 EFCore 创建 GraphQL 部分更新 - How can I create a GraphQL partial update with HotChocolate and EFCore 如何在 GraphQL HotChocolate 中实现订阅? - How do I implement subscriptions in GraphQL HotChocolate? 如何使用服务器模式在网格视图中获取异常详细信息? - How can I get exception details in grid view using server mode? 如何从CompletedEventArgs中获取抛出的异常类型? - How can I get the type of thrown exception from CompletedEventArgs? 如何处理 HotChocolate 中的输入错误? - How can I handle input errors in HotChocolate? 无法获得REST请求响应的实际错误。 Fiddler向我展示了更多错误细节。 在.NET中,我得到的是500 - 内部服务器错误 - Can't get actual error in response for REST request. Fiddler shows me more error details. In .NET all I get is 500 - Internal Server Error 我如何知道是否故意抛出了异常? - How can i tell if an Exception was deliberately thrown? 如何使用 HotChocolate C# 通过 GrahpQl 中的特定参数获取 object - How can I get object by specific parameter in GrahpQl with HotChocolate C# 如何将抛出的异常映射到事件ID以进行日志记录? - How to map a thrown exception to an event ID for logging? 抛出异常的详细信息 - Thrown exception details
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM