Search in sources :

Example 16 with ErrorData

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

the class OptimisticLockExceptionMapper method toErrorResponse.

@Override
public ErrorResponse toErrorResponse(OptimisticLockException cve) {
    HashMap<String, Object> meta = new HashMap<>();
    meta.put(META_TYPE_KEY, JPA_OPTIMISTIC_LOCK_EXCEPTION_TYPE);
    ErrorData error = ErrorData.builder().setMeta(meta).setCode(ERROR_TYPE).setStatus(Integer.toString(HttpStatus.CONFLICT_409)).setDetail(cve.getMessage()).build();
    return ErrorResponse.builder().setStatus(HttpStatus.CONFLICT_409).setSingleErrorData(error).build();
}
Also used : HashMap(java.util.HashMap) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 17 with ErrorData

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

the class OptimisticLockExceptionMapper method fromErrorResponse.

@Override
public OptimisticLockException fromErrorResponse(ErrorResponse errorResponse) {
    Iterable<ErrorData> errors = errorResponse.getErrors();
    ErrorData error = errors.iterator().next();
    String msg = error.getDetail();
    return new OptimisticLockException(msg);
}
Also used : OptimisticLockException(javax.persistence.OptimisticLockException) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 18 with ErrorData

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

the class JpaExceptionMapperTests method testPersistenceException.

@Test
public void testPersistenceException() {
    PersistenceException exception = new PersistenceException(new BadRequestException("test"));
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    PersistenceExceptionMapper mapper = (PersistenceExceptionMapper) exceptionMapperRegistry.findMapperFor(PersistenceException.class).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 : PersistenceException(javax.persistence.PersistenceException) BadRequestException(io.crnk.core.exception.BadRequestException) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) PersistenceExceptionMapper(io.crnk.jpa.internal.PersistenceExceptionMapper) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 19 with ErrorData

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

the class JpaExceptionMapperTests method testPersistenceRollbackException.

@Test
public void testPersistenceRollbackException() {
    javax.persistence.RollbackException exception = new javax.persistence.RollbackException(new BadRequestException("test"));
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    PersistenceRollbackExceptionMapper mapper = (PersistenceRollbackExceptionMapper) exceptionMapperRegistry.findMapperFor(javax.persistence.RollbackException.class).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 : BadRequestException(io.crnk.core.exception.BadRequestException) PersistenceRollbackExceptionMapper(io.crnk.jpa.internal.PersistenceRollbackExceptionMapper) 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 20 with ErrorData

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

the class DocumentMapper method addErrors.

private void addErrors(Document doc, Iterable<ErrorData> errors) {
    if (errors != null) {
        List<ErrorData> errorList = new ArrayList<>();
        for (ErrorData error : errors) {
            errorList.add(error);
        }
        doc.setErrors(errorList);
    }
}
Also used : ArrayList(java.util.ArrayList) ErrorData(io.crnk.core.engine.document.ErrorData)

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