Fre*_*rke 4 c# graphql asp.net-core hotchocolate
我正在构建一个简单的 HotChocolate GraphQl 服务器,HotChocolate 会抛出一个Unexpected Execution Error,但不会公开任何有关错误的信息,只要我针对它发布请求。我如何向后端(BananaCakePop、Postman、Insomnia 等)发布请求并不重要。
响应如下所示:
{
"errors": [
{
"message": "Unexpected Execution Error",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"pong"
]
}
],
"data": {
"pong": null
}
}
Run Code Online (Sandbox Code Playgroud)
请求响应不包含更多信息,应用程序控制台也没有记录任何内容。尝试找出问题所在的合理下一步是什么?
如果没有附加调试器,默认情况下 HotChocolate 不会公开您的异常的详细信息。因此,要获取错误消息,您可以:
您可以通过以下方式更改 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());
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式更改 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()}
);
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以向您的应用程序添加一个 IErrorFilter,例如,可以将您的异常记录到您的语言环境控制台,或将异常转换为 GraphQlErrors。有关此主题的更多信息,请查看:
我在同一条船上,也需要例外详细信息
并且应用程序控制台没有记录任何内容
我建议我们应该在此处添加错误日志记录,而不是返回到 GraphQL 客户端的响应。
如果我们在生产和开发环境中遇到问题,设置适当的日志记录将为我们提供帮助。
我们需要连接到 Hot Chocolate 的Diagnostics。有多种类型的诊断事件,我只想为执行事件设置一种,因为这就是我的情况下的错误所在。我只能测试 ResolverError,但其余的应该可以工作。
public class ErrorLoggingDiagnosticsEventListener : ExecutionDiagnosticEventListener
{
private readonly ILogger<ErrorLoggingDiagnosticsEventListener> log;
public ErrorLoggingDiagnosticsEventListener(
ILogger<ErrorLoggingDiagnosticsEventListener> log)
{
this.log = log;
}
public override void ResolverError(
IMiddlewareContext context,
IError error)
{
log.LogError(error.Exception, error.Message);
}
public override void TaskError(
IExecutionTask task,
IError error)
{
log.LogError(error.Exception, error.Message);
}
public override void RequestError(
IRequestContext context,
Exception exception)
{
log.LogError(exception, "RequestError");
}
public override void SubscriptionEventError(
SubscriptionEventContext context,
Exception exception)
{
log.LogError(exception, "SubscriptionEventError");
}
public override void SubscriptionTransportError(
ISubscription subscription,
Exception exception)
{
log.LogError(exception, "SubscriptionTransportError");
}
}
Run Code Online (Sandbox Code Playgroud)
现在将其连接到启动配置中。
public void ConfigureServices(IServiceCollection services)
{
services
.AddGraphQLServer()
.AddDiagnosticEventListener<ErrorLoggingDiagnosticsEventListener>()
...
;
}
Run Code Online (Sandbox Code Playgroud)
现在,异常已记录到您为 ILogger 配置的接收器中。
| 归档时间: |
|
| 查看次数: |
1288 次 |
| 最近记录: |