当在 HotChocolate GraphQL 服务器中抛出异常时,如何获取更多错误详细信息或日志记录?
How can I get more error details or logging, when an exception is thrown in a HotChocolate GraphQL server?
我正在构建一个简单的 HotChocolate GraphQl 服务器,HotChocolate 会抛出一个 Unexpected Execution Error
,但不会公开任何有关错误的信息,只要我 post 对它提出请求。
我如何 post 对后端的请求(BananaCakePop、Postman、Insomnia,...)并不重要。
响应如下所示:
{
"errors": [
{
"message": "Unexpected Execution Error",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"pong"
]
}
],
"data": {
"pong": null
}
}
请求响应不包含更多信息,也没有任何内容记录到应用程序控制台。
尝试找出问题所在的下一步合理步骤是什么?
如果没有附加调试器,默认情况下 HotChocolate 不会公开异常的详细信息。
因此,要获取错误消息,您可以:
- 将调试器附加到您的服务器,然后它将在请求中公开异常详细信息(a.k.a。在调试中启动您的服务器 :D)
- 以更适合您的开发风格的方式更改此行为。
这是更改 V11 或 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());
}
}
您可以通过以下方式更改 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()}
);
}
}
您还可以向您的应用程序添加一个 IErrorFilter,例如,它可以将您的异常记录到您的区域设置控制台,或将异常转换为 GraphQlErrors。
有关此主题的更多信息,请查看:
- V12:TODO(目前没有关于此主题的官方 V12 文档)
- V11:TODO(目前没有关于此主题的官方 V11 文档)
- V10: https://chillicream.com/docs/hotchocolate/v10/execution-engine/error-filter/#exception-details
我正在构建一个简单的 HotChocolate GraphQl 服务器,HotChocolate 会抛出一个 Unexpected Execution Error
,但不会公开任何有关错误的信息,只要我 post 对它提出请求。
我如何 post 对后端的请求(BananaCakePop、Postman、Insomnia,...)并不重要。
响应如下所示:
{
"errors": [
{
"message": "Unexpected Execution Error",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"pong"
]
}
],
"data": {
"pong": null
}
}
请求响应不包含更多信息,也没有任何内容记录到应用程序控制台。 尝试找出问题所在的下一步合理步骤是什么?
如果没有附加调试器,默认情况下 HotChocolate 不会公开异常的详细信息。 因此,要获取错误消息,您可以:
- 将调试器附加到您的服务器,然后它将在请求中公开异常详细信息(a.k.a。在调试中启动您的服务器 :D)
- 以更适合您的开发风格的方式更改此行为。
这是更改 V11 或 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());
}
}
您可以通过以下方式更改 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()}
);
}
}
您还可以向您的应用程序添加一个 IErrorFilter,例如,它可以将您的异常记录到您的区域设置控制台,或将异常转换为 GraphQlErrors。 有关此主题的更多信息,请查看:
- V12:TODO(目前没有关于此主题的官方 V12 文档)
- V11:TODO(目前没有关于此主题的官方 V11 文档)
- V10: https://chillicream.com/docs/hotchocolate/v10/execution-engine/error-filter/#exception-details