Search code examples
javaquarkusreactive

How to make exception mapper to be called in Quarkus


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

Solution

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