[英]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:
因此,要获取您的错误消息,您可以:
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.