0

I have a REST-Backend created with JHipster. There are different exception-classes in the service layer and the web-rest layer. This service-exceptions are translated by an ExceptionTranslator which implements the ProblemHandling interface from org.zalando.problem.spring.web.advice

I have the following ExceptionTranslator:

@ControllerAdvice
public class ExceptionTranslator implements ProblemHandling, SecurityAdviceTrait {

    @Override
    public ResponseEntity<Problem> process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {
    //generated by jHipster
    }

    @ExceptionHandler(HouseWithoutOwnerServiceException.class)
    public ResponseEntity<Problem> handleHouseWithoutOwnerException(HouseWithoutOwnerServiceException ex, NativeWebRequest request) {
        return create(new HouseWithoutOwnerException(), request);
    }
}    

The service-exception class:

public class HouseWithoutOwnerServiceException extends RuntimeException {

    public HouseWithoutOwnerServiceException() {
        super("House without owner!");
    }
}

The rest-error class:

public class HouseWithoutOwnerException extends AbstractThrowableProblem {

    private static final long serialVersionUID = 1L;

    public HouseWithoutOwnerException() {
        super(ErrorConstants.HOUSE_WITHOUT_OWNER_TYPE, "House does not have an owner", Status.CONFLICT);
    }
}

In my test the HouseWithoutOwnerServiceException is thrown but not translated into a HouseWithoutOwnerException:

@SpringBootTest(classes = HouseApp.class)
public class HouseControllerIT {

    @Autowired
    private MappingJackson2HttpMessageConverter jacksonMessageConverter;

    @Autowired
    private PageableHandlerMethodArgumentResolver pageableArgumentResolver;

    @Autowired
    private ExceptionTranslator exceptionTranslator;

    private MockMvc restHouseMockMvc;

    @BeforeEach
    public void setup() {
        HouseController houseController = new HouseController(houseService);

        this.restHouseMockMvc = MockMvcBuilders.standaloneSetup(houseController)
            .setCustomArgumentResolvers(pageableArgumentResolver)
            .setControllerAdvice(exceptionTranslator)
            .setMessageConverters(jacksonMessageConverter)
            .build();
    }

    @Test
    @Transactional
    public void createHouseWithoutExistingOwner() throws Exception {
        HouseDTO houseDTO = createHouseDTOWithoutOwner();
        houseDTO.setOwnerId(ownerId + 1); //not existing

        restHouseMockMvc.perform(post("/api/v1/houses")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(houseDTO)))
            .andExpect(status().isConflict());
    }
}

Therefore I always get 500 Internal Server Error instead of 409 Conflict. I debugged it already and the method in the ExceptionTranslator is not entered.

Steve2Fish
  • 187
  • 4
  • 15
  • which version spring you are using ? I think it was bug and fixed as 4.2.0.BUILD-SNAPSHOT , so check it out https://github.com/spring-projects/spring-framework/issues/17348 and also this commit https://github.com/spring-projects/spring-framework/commit/2dd587596437a4bbe9f62ba0dc9f7b13382fb533 – Mithat Konuk Oct 25 '19 at 21:24
  • I am using 5.1.10.release – Steve2Fish Oct 27 '19 at 09:26

0 Answers0