Search in sources :

Example 11 with ErrorResponse

use of io.crnk.core.engine.error.ErrorResponse in project crnk-framework by crnk-project.

the class RegistryEntry method toResource.

private Object toResource(JsonApiResponse response) {
    if (response.getErrors() != null && response.getErrors().iterator().hasNext()) {
        List<ErrorData> errorList = new ArrayList<>();
        response.getErrors().forEach(it -> errorList.add(it));
        Optional<Integer> errorCode = errorList.stream().filter(it -> it.getStatus() != null).map(it -> Integer.parseInt(it.getStatus())).collect(Collectors.maxBy(Integer::compare));
        ErrorResponse errorResponse = new ErrorResponse(errorList, errorCode.get());
        ExceptionMapperRegistry exceptionMapperRegistry = moduleRegistry.getExceptionMapperRegistry();
        ExceptionMapper<Throwable> exceptionMapper = exceptionMapperRegistry.findMapperFor(errorResponse).get();
        return exceptionMapper.fromErrorResponse(errorResponse);
    }
    return response.getEntity();
}
Also used : ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) ResourceList(io.crnk.core.resource.list.ResourceList) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) HashMap(java.util.HashMap) ResourceField(io.crnk.core.engine.information.resource.ResourceField) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) ArrayList(java.util.ArrayList) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceFieldNotFoundException(io.crnk.core.exception.ResourceFieldNotFoundException) Map(java.util.Map) AnnotatedResourceEntry(io.crnk.legacy.registry.AnnotatedResourceEntry) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) QuerySpec(io.crnk.core.queryspec.QuerySpec) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Collection(java.util.Collection) ModuleRegistry(io.crnk.core.module.ModuleRegistry) Collectors(java.util.stream.Collectors) AnnotatedRelationshipEntryBuilder(io.crnk.legacy.registry.AnnotatedRelationshipEntryBuilder) ErrorData(io.crnk.core.engine.document.ErrorData) Serializable(java.io.Serializable) List(java.util.List) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) Optional(java.util.Optional) PreconditionUtil(io.crnk.core.engine.internal.utils.PreconditionUtil) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) RelationshipRepositoryNotFoundException(io.crnk.core.exception.RelationshipRepositoryNotFoundException) ArrayList(java.util.ArrayList) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Example 12 with ErrorResponse

use of io.crnk.core.engine.error.ErrorResponse in project crnk-framework by crnk-project.

the class JpaExceptionMapperTests method testTransactionRollbackException.

@Test
public void testTransactionRollbackException() {
    javax.transaction.RollbackException exception = new javax.transaction.RollbackException() {

        public Throwable getCause() {
            return new BadRequestException("test");
        }
    };
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    TransactionRollbackExceptionMapper mapper = (TransactionRollbackExceptionMapper) exceptionMapperRegistry.findMapperFor(exception.getClass()).get();
    ErrorResponse response = mapper.toErrorResponse(exception);
    ErrorData errorData = response.getErrors().iterator().next();
    Assert.assertEquals(Integer.toString(HttpStatus.BAD_REQUEST_400), errorData.getStatus());
    Assert.assertEquals("test", errorData.getDetail());
}
Also used : TransactionRollbackExceptionMapper(io.crnk.jpa.internal.TransactionRollbackExceptionMapper) BadRequestException(io.crnk.core.exception.BadRequestException) 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 13 with ErrorResponse

use of io.crnk.core.engine.error.ErrorResponse 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 14 with ErrorResponse

use of io.crnk.core.engine.error.ErrorResponse in project crnk-framework by crnk-project.

the class WebApplicationExceptionMapperTest method test.

@Test
public void test() {
    WebApplicationExceptionMapper mapper = new WebApplicationExceptionMapper();
    WebApplicationException exception = new WebApplicationException("hi");
    ErrorResponse response = mapper.toErrorResponse(exception);
    Iterable<ErrorData> errors = response.getErrors();
    Iterator<ErrorData> iterator = errors.iterator();
    ErrorData data = iterator.next();
    Assert.assertFalse(iterator.hasNext());
    Assert.assertEquals("500", data.getStatus());
    Assert.assertEquals("hi", data.getCode());
    Assert.assertTrue(mapper.accepts(response));
    WebApplicationException fromErrorResponse = mapper.fromErrorResponse(response);
    Assert.assertEquals("hi", fromErrorResponse.getMessage());
}
Also used : WebApplicationExceptionMapper(io.crnk.rs.internal.WebApplicationExceptionMapper) WebApplicationException(javax.ws.rs.WebApplicationException) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 15 with ErrorResponse

use of io.crnk.core.engine.error.ErrorResponse 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)18 Test (org.junit.Test)15 ErrorData (io.crnk.core.engine.document.ErrorData)10 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)7 CrnkExceptionMapper (io.crnk.core.engine.internal.exception.CrnkExceptionMapper)6 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)5 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)4 Optional (io.crnk.core.utils.Optional)4 BadRequestException (io.crnk.core.exception.BadRequestException)3 Document (io.crnk.core.engine.document.Document)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 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