Ash*_*ngh 5 java error-handling graphql graphql-java
所以,我一直在编写一个微服务,它使用 GraphQL 的 API 的 java 实现。GraphQL 对客户端提供的查询强制执行某种级别的验证。但是,如果在解决查询时出现问题,我已经看到 graphql 显示了暴露 micr 服务内部结构的消息。
我需要什么?一种处理从解析器函数抛出的所有异常/错误的方法,这样我就可以在 GraphQL 创建相应的响应之前清理异常/错误。
我查阅了官方文档和许多堆栈溢出问题,但没有找到任何关于处理的地方。如果我找到了,它们适用于以前的版本,不再受支持。
我提到的一些链接 - 1. https://www.howtographql.com/graphql-java/7-error-handling/ 2. GraphQL java 以 json 格式发送自定义错误 3. https://www.graphql-java .com/documentation/v13/execution/
我已经完成了以下事情,例如 -
创建自定义处理程序
@Bean
public GraphQLErrorHandler errorHandler() {
return new CustomGraphQLErrorHandler();
}
Run Code Online (Sandbox Code Playgroud)
public class CustomGraphQLErrorHandler implements GraphQLErrorHandler {
@Override
public List<GraphQLError> processErrors(List<GraphQLError> errors) {
List<GraphQLError> clientErrors = errors.stream()
.filter(this::isClientError)
.collect(Collectors.toList());
List<GraphQLError> serverErrors = errors.stream()
.filter(this::isSystemError)
.map(GraphQLErrorAdapter::new)
.collect(Collectors.toList());
List<GraphQLError> e = new ArrayList<>();
e.addAll(clientErrors);
e.addAll(serverErrors);
return e;
}
private boolean isSystemError(GraphQLError error) {
return !isClientError(error);
}
private boolean isClientError(GraphQLError error) {
return !(error instanceof ExceptionWhileDataFetching || error instanceof Throwable);
}
}```
Expected behavior - The control would reach to `processErrors` method. Actual - It doesn't reach there.
Run Code Online (Sandbox Code Playgroud)
小智 0
您需要重写errorsPresent
in 方法,GraphQLErrorHandler
以便在将错误传递到该方法时返回 true。就像是:
@Override
public boolean errorsPresent(List<GraphQLError> errors) {
return !CollectionUtils.isEmpty(errors);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1919 次 |
最近记录: |