java 如何在Quarkus中调用异常Map器

uhry853o  于 2023-08-02  发布在  Java
关注(0)|答案(2)|浏览(89)

请参阅下面的资源。我在postjut中设置了抛出异常来测试异常Map器是否工作,但它从未被调用。相反,我得到“红屏”从 quarkus 错误信息与stacktrace请帮助

@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();
    }
}

k75qkfdt

k75qkfdt1#

Reactive Routes@Route注解的来源)支持来自RESTEasy Reactive的任何特性。
因此,最好的办法是将LoginResource重写为Jakarta REST资源。

vyswwuz2

vyswwuz22#

找到原因了。使用React式路由时,不调用异常处理程序,而应按如下方式使用

@Route(path = "/*", type = Route.HandlerType.FAILURE, produces = APPLICATION_JSON)
void authConsentExceptionHandler(AuthConsentException e, HttpServerResponse response) {
    response.setStatusCode(INTERNAL_SERVER_ERROR).end();
}

字符串

相关问题