use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.
the class ExceptionMapperRegistryTest method shouldFindDescendantExceptionMapperFromError.
@Test
public void shouldFindDescendantExceptionMapperFromError() throws Exception {
// two exception mapper will match (IllegalStateException and SomeIllegalStateException)
// subtype should be choosen.
ErrorData errorData = ErrorData.builder().setId("someId").build();
ErrorResponse response = ErrorResponse.builder().setStatus(HttpStatus.BAD_REQUEST_400).setSingleErrorData(errorData).build();
Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(response);
assertThat(mapper.isPresent()).isTrue();
assertThat(mapper.get()).isExactlyInstanceOf(SomeIllegalStateExceptionMapper.class);
Throwable throwable = mapper.get().fromErrorResponse(response);
assertThat(throwable).isExactlyInstanceOf(SomeIllegalStateException.class);
}
use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.
the class ExceptionMapperRegistryTest method shouldNotFindDescendantExceptionMapperFromError.
@Test
public void shouldNotFindDescendantExceptionMapperFromError() throws Exception {
ErrorData errorData = ErrorData.builder().setId("someOtherId").build();
ErrorResponse response = ErrorResponse.builder().setStatus(HttpStatus.BAD_REQUEST_400).setSingleErrorData(errorData).build();
Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(response);
assertThat(mapper.isPresent()).isTrue();
assertThat(mapper.get()).isExactlyInstanceOf(IllegalStateExceptionMapper.class);
}
use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.
the class ExceptionMapperRegistryTest method shouldFindDirectExceptionMapperFromError.
@Test
public void shouldFindDirectExceptionMapperFromError() throws Exception {
ErrorResponse response = ErrorResponse.builder().setStatus(HttpStatus.BAD_REQUEST_400).build();
Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(response);
assertThat(mapper.isPresent()).isTrue();
assertThat(mapper.get()).isExactlyInstanceOf(IllegalStateExceptionMapper.class);
Throwable throwable = mapper.get().fromErrorResponse(response);
assertThat(throwable).isExactlyInstanceOf(IllegalStateException.class);
}
use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.
the class ExceptionMapperRegistry method findMapperFor.
@SuppressWarnings({ "rawtypes", "unchecked" })
public <E extends Throwable> Optional<ExceptionMapper<E>> findMapperFor(ErrorResponse errorResponse) {
int currentDepth = -1;
ExceptionMapper closestExceptionMapper = null;
for (ExceptionMapperType mapperType : exceptionMappers) {
JsonApiExceptionMapper mapperObj = mapperType.getExceptionMapper();
if (mapperObj instanceof ExceptionMapper) {
ExceptionMapper mapper = (ExceptionMapper) mapperObj;
boolean accepted = mapper.accepts(errorResponse);
if (accepted) {
// the exception with the most super types is chosen
int tempDepth = countSuperTypes(mapperType.getExceptionClass());
if (tempDepth > currentDepth) {
currentDepth = tempDepth;
closestExceptionMapper = mapper;
}
}
}
}
return (Optional) Optional.ofNullable(closestExceptionMapper);
}
Aggregations