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

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

How to make exception mapper to be called in Quarkus

问题

请参考下面的资源。我设置了在post中抛出异常,只是为了测试异常映射器是否起作用,但它从未被调用。相反,我收到了quarks错误消息的“红屏幕”和堆栈跟踪。

请帮忙

  1. @RouteBase(path = "login", produces = APPLICATION_JSON)
  2. public class LoginResource {
  3. @ServerExceptionMapper
  4. public RestResponse<String> mapException(AuthConsentException x) {
  5. return RestResponse.status(Response.Status.NOT_FOUND, "message");
  6. }
  7. @Inject
  8. LoginService loginService;
  9. @Route(path = "/provider", methods = POST, consumes = APPLICATION_FORM_URLENCODED)
  10. public void post(RoutingContext routingContext,
  11. @Param(LOGIN_CHALLENGE) @NotBlank(message = LOGIN_CHALLENGE + PARAMETER_SHOULD_BE_SET) String challenge,
  12. @Param(PROVIDER) @NotBlank(message = PROVIDER + PARAMETER_SHOULD_BE_SET) String provider) {
  13. throw new AuthConsentException("test");
  14. // loginService.post(routingContext, challenge, provider);
  15. }
  16. }
  17. 我也尝试使用但同样它从未被调用
  18. @Provider
  19. public class AuthConsentExceptionMapper implements ExceptionMapper<AuthConsentException> {
  20. private static final String ERROR_LOG_PATTERN = "%s, id:%s";
  21. @Override
  22. public Response toResponse(AuthConsentException e) {
  23. ErrorDTO error = new ErrorDTO(e.getMessage());
  24. Log.errorf(ERROR_LOG_PATTERN, e.getMessage(), error.getId());
  25. return Response.status(INTERNAL_SERVER_ERROR)
  26. .type(APPLICATION_JSON_TYPE)
  27. .entity(error).build();
  28. }
  29. }
英文:

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

  1. @RouteBase(path = &quot;login&quot;, produces = APPLICATION_JSON)
  2. public class LoginResource {
  3. @ServerExceptionMapper
  4. public RestResponse&lt;String&gt; mapException(AuthConsentException x) {
  5. return RestResponse.status(Response.Status.NOT_FOUND, &quot;message&quot;);
  6. }
  7. @Inject
  8. LoginService loginService;
  9. @Route(path = &quot;/provider&quot;, methods = POST, consumes = APPLICATION_FORM_URLENCODED)
  10. public void post(RoutingContext routingContext,
  11. @Param(LOGIN_CHALLENGE) @NotBlank(message = LOGIN_CHALLENGE + PARAMETER_SHOULD_BE_SET) String challenge,
  12. @Param(PROVIDER) @NotBlank(message = PROVIDER + PARAMETER_SHOULD_BE_SET) String provider) {
  13. throw new AuthConsentException(&quot;test&quot;);
  14. // loginService.post(routingContext, challenge, provider);
  15. }
  16. }

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

  1. @Provider
  2. public class AuthConsentExceptionMapper implements ExceptionMapper&lt;AuthConsentException&gt; {
  3. private static final String ERROR_LOG_PATTERN = &quot;%s, id:%s&quot;;
  4. @Override
  5. public Response toResponse(AuthConsentException e) {
  6. ErrorDTO error = new ErrorDTO(e.getMessage());
  7. Log.errorf(ERROR_LOG_PATTERN, e.getMessage(), error.getId());
  8. return Response.status(INTERNAL_SERVER_ERROR)
  9. .type(APPLICATION_JSON_TYPE)
  10. .entity(error).build();
  11. }
  12. }

答案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

找到原因。当使用响应式路由时,异常处理程序不会被调用,而应该按照以下方式使用:

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

Found the reason. when using reactive routes exception handlers are not called, rather it should be used as follow

  1. @Route(path = &quot;/*&quot;, type = Route.HandlerType.FAILURE, produces = APPLICATION_JSON)
  2. void authConsentExceptionHandler(AuthConsentException e, HttpServerResponse response) {
  3. response.setStatusCode(INTERNAL_SERVER_ERROR).end();
  4. }

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:

确定