Skip to content

@RestControllerAdvice and @ExceptionHandler #10033

@arilsonsantos

Description

@arilsonsantos

Hy guys,
I'm migrating a Spring Boot service to Quarkus, everything has been going almost 100% ok.

Describe the bug
I'm not sure if it is even a bug or I need to set up something else, but I have not been seing any problem like that, so... here am I.

In the test class, the ApiExceptionHandler (RestControllerAdvice), workes well, and all messages of success and fail are showed correctly, but when it is called by a rest client, it has not been working so well (for me).

Expected behavior
Return the custom json error message in the body.

Actual behavior

ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (executor-thread-1) HTTP Request to /payment failed, error id: 67f928e4-3de0-4468-9b66-4becc00dc4b7-1: java.lang.NullPointerException

To Reproduce
Steps to reproduce the behavior:

  1. Test class runs OK
  2. Resut test
  3. Test rest client fail
  4. Throw exception
  5. My ExceptionHandler

Screenshots.

  1. The test is executed with no problem
    @Test
    public void validate_balance_insuficient_test()  {
        PaymentDto json = new PaymentDto();
        json.setCardNumber(12345678);
        json.setSecurityCode(90);
        json.setBillAmount(new BigDecimal(5000));

        given()
            .contentType("application/json")
            .body(json)
            .post("/payment")        
        .then()
            .assertThat()   
            .statusCode(406)
            .body("message", is("There is no enough balance"))
            .log()
            .all();

The result:
Captura de Tela 2020-06-15 às 20 20 13

The same test executed by rest client

image.

The result:
Captura de Tela 2020-06-15 às 20 06 39

This is the handler called in both of tests.
`@RestControllerAdvice
public class ApiExceptionHandler {

@ExceptionHandler(PaymentException.class)
public ResponseEntity<?> handlerPaymentException(PaymentException ex) {
    MessageReturnDto messageReturn = MessageReturnDto.builder().message(ex.getMessage()).build();

    return new ResponseEntity<>(messageReturn, HttpStatus.NOT_ACCEPTABLE);
}`  

Environment:

  • openjdk 11.0.7 2020-04-14
  • OpenJDK Runtime Environment GraalVM CE 20.1.0 (build 11.0.7+10-jvmci-20.1-b02)
  • OpenJDK 64-Bit Server VM GraalVM CE 20.1.0 (build 11.0.7+10-jvmci-20.1-b02, mixed mode, sharing)
  • Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
  • OS name: "mac os x", version: "10.15.5", arch: "x86_64", family: "mac"

Metadata

Metadata

Assignees

Labels

area/springIssues relating to the Spring integrationkind/bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions