【发布时间】:2020-09-16 23:14:36
【问题描述】:
如何处理我的 graphQL API 中的错误?我正在使用 graphql-java-tools 和 graphql-spring-boot-starter。我创建了错误处理程序,但每次我得到响应 200,即使抛出异常也是如此。你能告诉我应该如何设置错误代码,例如 400 作为响应?
@Component
public class CustomGraphQLErrorHandler implements GraphQLErrorHandler {
@Override
public List<GraphQLError> processErrors(List<GraphQLError> list) {
return list.stream().map(this::getNested).collect(Collectors.toList());
}
private GraphQLError getNested(GraphQLError error) {
if (error instanceof ExceptionWhileDataFetching) {
ExceptionWhileDataFetching exceptionError = (ExceptionWhileDataFetching) error;
if (exceptionError.getException() instanceof GraphQLError) {
return (GraphQLError) exceptionError.getException();
}
}
return error;
}
}
【问题讨论】:
标签: java spring graphql graphql-java-tools