Search in sources :

Example 6 with ExceptionMapperRegistry

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());
}
Also used : HibernateConstraintViolationExceptionMapper(io.crnk.jpa.internal.HibernateConstraintViolationExceptionMapper) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 7 with ExceptionMapperRegistry

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();
}
Also used : CrnkBoot(io.crnk.core.boot.CrnkBoot) ArrayList(java.util.ArrayList) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) Document(io.crnk.core.engine.document.Document) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Aggregations

ErrorResponse (io.crnk.core.engine.error.ErrorResponse)7 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)7 ErrorData (io.crnk.core.engine.document.ErrorData)6 Test (org.junit.Test)4 BadRequestException (io.crnk.core.exception.BadRequestException)3 Document (io.crnk.core.engine.document.Document)2 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)2 ArrayList (java.util.ArrayList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ClientException (io.crnk.client.ClientException)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)1 ResourceRepositoryInformation (io.crnk.core.engine.information.repository.ResourceRepositoryInformation)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)1 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)1 PreconditionUtil (io.crnk.core.engine.internal.utils.PreconditionUtil)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)1