use of io.crnk.core.engine.internal.exception.ExceptionMapperRegistry in project crnk-framework by crnk-project.
the class JpaExceptionMapperTests method testConstraintException.
@Test
public void testConstraintException() {
ConstraintViolationException exception = new ConstraintViolationException("message", null, "constraint");
ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
HibernateConstraintViolationExceptionMapper mapper = (HibernateConstraintViolationExceptionMapper) exceptionMapperRegistry.findMapperFor(ConstraintViolationException.class).get();
ErrorResponse response = mapper.toErrorResponse(exception);
ErrorData errorData = response.getErrors().iterator().next();
Assert.assertEquals(Integer.toString(HttpStatus.UNPROCESSABLE_ENTITY_422), errorData.getStatus());
Assert.assertEquals(exception.getConstraintName(), errorData.getCode());
Assert.assertEquals(exception.getMessage(), errorData.getDetail());
Assert.assertTrue(mapper.accepts(response));
ConstraintViolationException deserializedException = mapper.fromErrorResponse(response);
Assert.assertEquals(exception.getMessage(), deserializedException.getMessage());
Assert.assertEquals(exception.getConstraintName(), deserializedException.getConstraintName());
}
use of io.crnk.core.engine.internal.exception.ExceptionMapperRegistry in project crnk-framework by crnk-project.
the class JsonapiExceptionMapperBridge method toResponse.
@Override
public Response toResponse(RuntimeException exception) {
CrnkBoot boot = this.feature.getBoot();
ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
Optional<JsonApiExceptionMapper> optional = exceptionMapperRegistry.findMapperFor(exception.getClass());
if (!optional.isPresent()) {
LOGGER.error("no exception mapper found", exception);
exception = new InternalServerErrorException(exception.getMessage());
optional = exceptionMapperRegistry.findMapperFor(exception.getClass());
}
JsonApiExceptionMapper exceptionMapper = optional.get();
ErrorResponse errorResponse = exceptionMapper.toErrorResponse(exception);
// use the Crnk document mapper to create a JSON API response
Document doc = new Document();
List<ErrorData> errors = new ArrayList<>();
for (ErrorData error : errorResponse.getErrors()) {
errors.add(error);
}
doc.setErrors(errors);
return Response.status(errorResponse.getHttpStatus()).entity(doc).header("Content-Type", JsonApiMediaType.APPLICATION_JSON_API).build();
}
Aggregations