如何使异常映射器在Quarkus中被调用

huangapple go评论86阅读模式
英文:

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 = &quot;login&quot;, produces = APPLICATION_JSON)
public class LoginResource {

@ServerExceptionMapper
public RestResponse&lt;String&gt; mapException(AuthConsentException x) {
    return RestResponse.status(Response.Status.NOT_FOUND, &quot;message&quot;);
}

@Inject
LoginService loginService;

@Route(path = &quot;/provider&quot;, 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(&quot;test&quot;);
   // loginService.post(routingContext, challenge, provider);
}
}

I was also trying to use, but the same it never gets called

@Provider
public class AuthConsentExceptionMapper implements ExceptionMapper&lt;AuthConsentException&gt; {
    private static final String ERROR_LOG_PATTERN = &quot;%s, id:%s&quot;;

    @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 = &quot;/*&quot;, type = Route.HandlerType.FAILURE, produces = APPLICATION_JSON)
void authConsentExceptionHandler(AuthConsentException e, HttpServerResponse response) {
    response.setStatusCode(INTERNAL_SERVER_ERROR).end();
}

huangapple
  • 本文由 发表于 2023年7月27日 23:15:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定