英文:
How to make exception mapper to be called in Quarkus
问题
请参考下面的资源。我设置了在post中抛出异常,只是为了测试异常映射器是否起作用,但它从未被调用。相反,我收到了quarks错误消息的“红屏幕”和堆栈跟踪。
请帮忙
@RouteBase(path = "login", produces = APPLICATION_JSON)
public class LoginResource {
@ServerExceptionMapper
public RestResponse<String> mapException(AuthConsentException x) {
return RestResponse.status(Response.Status.NOT_FOUND, "message");
}
@Inject
LoginService loginService;
@Route(path = "/provider", methods = POST, consumes = APPLICATION_FORM_URLENCODED)
public void post(RoutingContext routingContext,
@Param(LOGIN_CHALLENGE) @NotBlank(message = LOGIN_CHALLENGE + PARAMETER_SHOULD_BE_SET) String challenge,
@Param(PROVIDER) @NotBlank(message = PROVIDER + PARAMETER_SHOULD_BE_SET) String provider) {
throw new AuthConsentException("test");
// loginService.post(routingContext, challenge, provider);
}
}
我也尝试使用,但同样它从未被调用
@Provider
public class AuthConsentExceptionMapper implements ExceptionMapper<AuthConsentException> {
private static final String ERROR_LOG_PATTERN = "%s, id:%s";
@Override
public Response toResponse(AuthConsentException e) {
ErrorDTO error = new ErrorDTO(e.getMessage());
Log.errorf(ERROR_LOG_PATTERN, e.getMessage(), error.getId());
return Response.status(INTERNAL_SERVER_ERROR)
.type(APPLICATION_JSON_TYPE)
.entity(error).build();
}
}
英文:
Please see the resource below. I set to throw exception in post jut to test that exception mapper works, but it never get called. Instead i am getting "red screen" from quarks error message with stacktrace
Please help
@RouteBase(path = "login", produces = APPLICATION_JSON)
public class LoginResource {
@ServerExceptionMapper
public RestResponse<String> mapException(AuthConsentException x) {
return RestResponse.status(Response.Status.NOT_FOUND, "message");
}
@Inject
LoginService loginService;
@Route(path = "/provider", methods = POST, consumes = APPLICATION_FORM_URLENCODED)
public void post(RoutingContext routingContext,
@Param(LOGIN_CHALLENGE) @NotBlank(message = LOGIN_CHALLENGE + PARAMETER_SHOULD_BE_SET) String challenge,
@Param(PROVIDER) @NotBlank(message = PROVIDER + PARAMETER_SHOULD_BE_SET) String provider) {
throw new AuthConsentException("test");
// loginService.post(routingContext, challenge, provider);
}
}
I was also trying to use, but the same it never gets called
@Provider
public class AuthConsentExceptionMapper implements ExceptionMapper<AuthConsentException> {
private static final String ERROR_LOG_PATTERN = "%s, id:%s";
@Override
public Response toResponse(AuthConsentException e) {
ErrorDTO error = new ErrorDTO(e.getMessage());
Log.errorf(ERROR_LOG_PATTERN, e.getMessage(), error.getId());
return Response.status(INTERNAL_SERVER_ERROR)
.type(APPLICATION_JSON_TYPE)
.entity(error).build();
}
}
答案1
得分: 1
Reactive Routes(从中获得@Route
注释的)不支持来自RESTEasy Reactive的任何功能。
因此,您最好的选择是将LoginResource
重写为Jakarta REST资源。
英文:
Reactive Routes (from which the @Route
annotation comes) does not support any of the features coming from RESTEasy Reactive.
So your best bet would be to rewrite LoginResource
as a Jakarta REST Resource.
答案2
得分: 1
找到原因。当使用响应式路由时,异常处理程序不会被调用,而应该按照以下方式使用:
@Route(path = "/*", type = Route.HandlerType.FAILURE, produces = APPLICATION_JSON)
void authConsentExceptionHandler(AuthConsentException e, HttpServerResponse response) {
response.setStatusCode(INTERNAL_SERVER_ERROR).end();
}
英文:
Found the reason. when using reactive routes exception handlers are not called, rather it should be used as follow
@Route(path = "/*", type = Route.HandlerType.FAILURE, produces = APPLICATION_JSON)
void authConsentExceptionHandler(AuthConsentException e, HttpServerResponse response) {
response.setStatusCode(INTERNAL_SERVER_ERROR).end();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论