Search in sources :

Example 31 with ErrorData

use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.

the class ConstraintViolationExceptionMapper method fromErrorResponse.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public ConstraintViolationException fromErrorResponse(ErrorResponse errorResponse) {
    Set violations = new HashSet();
    Iterable<ErrorData> errors = errorResponse.getErrors();
    for (ErrorData error : errors) {
        ConstraintViolationImpl violation = ConstraintViolationImpl.fromError(context.getResourceRegistry(), error);
        violations.add(violation);
    }
    return new ConstraintViolationException(null, violations);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ConstraintViolationException(javax.validation.ConstraintViolationException) ErrorData(io.crnk.core.engine.document.ErrorData) HashSet(java.util.HashSet)

Example 32 with ErrorData

use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.

the class ConstraintViolationExceptionMapper method toErrorResponse.

@Override
public ErrorResponse toErrorResponse(ConstraintViolationException cve) {
    LOGGER.warn("a ConstraintViolationException occured", cve);
    List<ErrorData> errors = new ArrayList<>();
    for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
        ErrorDataBuilder builder = ErrorData.builder();
        builder = builder.addMetaField(META_TYPE_KEY, META_TYPE_VALUE);
        builder = builder.setStatus(String.valueOf(HttpStatus.UNPROCESSABLE_ENTITY_422));
        builder = builder.setDetail(violation.getMessage());
        builder = builder.setCode(toCode(violation));
        if (violation.getMessageTemplate() != null) {
            builder = builder.addMetaField(META_MESSAGE_TEMPLATE, violation.getMessageTemplate());
        }
        // depending on bulk update spec, we might also provide the leaf information in the future
        if (violation.getRootBean() != null) {
            ResourceRef resourceRef = resolvePath(violation);
            builder = builder.addMetaField(META_RESOURCE_ID, resourceRef.getRootResourceId());
            builder = builder.addMetaField(META_RESOURCE_TYPE, resourceRef.getRootResourceType());
            builder = builder.setSourcePointer(resourceRef.getRootSourcePointer());
        }
        ErrorData error = builder.build();
        errors.add(error);
    }
    return ErrorResponse.builder().setStatus(HttpStatus.UNPROCESSABLE_ENTITY_422).setErrorData(errors).build();
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) ArrayList(java.util.ArrayList) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 33 with ErrorData

use of io.crnk.core.engine.document.ErrorData 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 34 with ErrorData

use of io.crnk.core.engine.document.ErrorData 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 35 with ErrorData

use of io.crnk.core.engine.document.ErrorData 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)

Aggregations

ErrorData (io.crnk.core.engine.document.ErrorData)50 Test (org.junit.Test)30 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)10 Document (io.crnk.core.engine.document.Document)9 ErrorDataBuilder (io.crnk.core.engine.document.ErrorDataBuilder)8 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)6 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)6 ArrayList (java.util.ArrayList)5 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)3 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)3 BadRequestException (io.crnk.core.exception.BadRequestException)3 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)2 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)2 ResourceNotFoundException (io.crnk.core.exception.ResourceNotFoundException)2 Optional (io.crnk.core.utils.Optional)2 HashMap (java.util.HashMap)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1