Spi*_*der 0 java validation spring-boot graphql graphql-spqr
目前,我正在抛出RuntimeException's 以返回 GraphQL 验证错误。它工作得非常好,除了它会在我的日志中抛出带有大量堆栈跟踪的可怕错误。
在这里您可以看到我正在检查提交的新用户注册变更,以确保密码彼此匹配并且电子邮件地址尚未使用。
在 GraphQL SPQR Spring Boot Starter 中执行此操作的正确方法是什么。
@GraphQLMutation (name="register")
public User register(@GraphQLArgument(name="firstname") String firstname, @GraphQLArgument(name="lastname") String lastname, @GraphQLArgument(name="email") String email, @GraphQLArgument(name="msisdn") String msisdn, @GraphQLArgument(name="password") String password, @GraphQLArgument (name="confirmPassword") String confirmPassword) {
if (userRepo.findByEmail(email) != null) {
throw new RuntimeException("User already exists");
}
if (!password.equals(confirmPassword)) {
throw new RuntimeException("Passwords do not match");
}
User newUser = new User();
//...
return userRepo.save(newUser);
}
Run Code Online (Sandbox Code Playgroud)
我不清楚你在问什么......但我假设你想自定义记录的内容。
对于初学者,我建议使用专用的异常类型,例如ValidationException,您可以以不同的方式捕获和处理。
至于日志记录,它可能发生在 grapqh-java 中,因为 SPQR 本身不记录任何内容。默认情况下,graphql-java 使用SimpleDataFetcherExceptionHandlerwhich记录字段解析期间捕获的异常。
您现在有几个选项,您可以ResolverInterceptor在 SPQR 中注册一个捕获验证异常并记录您想要的内容,并DataFetcherResult为用户返回带有错误消息的一个。因为没有验证异常冒泡到 graphql-java,所以DataFetcherExceptionHandler在这种情况下没有任何关系。
它看起来像:
public class ValidationInterceptor implements ResolverInterceptor {
@Override
public Object aroundInvoke(InvocationContext context, Continuation continuation) throws Exception {
try {
return continuation.proceed(context);
} catch (ValidationException e) {
log.warning(e);
return DataFetcherResult.newResult()
.error(GraphqlErrorBuilder
.newError(context.getResolutionEnvironment().dataFetchingEnvironment)
.message(e.getMessage()) //the message for the user
.build());
}
}
}
Run Code Online (Sandbox Code Playgroud)
查看此处的答案,了解有关使用 Spring Boot 注册自定义拦截器的说明。
另一种选择是替换DataFetcherExceptionHandlergraphql-java 使用。为此,您必须GraphQL自己构造对象并将其注册为 bean。
@Bean
public GraphQL graphQL(GraphQLSchema schema) {
GraphQL.Builder builder = GraphQL.newGraphQL(schema)
.queryExecutionStrategy(new AsyncExecutionStrategy(customExceptionHandler))
.mutationExecutionStrategy(new AsyncSerialExecutionStrategy(customExceptionHandler));
return builder.build();
}
Run Code Online (Sandbox Code Playgroud)
如果某个地方有一个 Spring 特性可以用于托管 bean 的异常处理,我也不会感到惊讶。
| 归档时间: |
|
| 查看次数: |
1834 次 |
| 最近记录: |