Search in sources :

Example 11 with ErrorData

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

the class ErrorDataTest method testSerialization.

@Test
public void testSerialization() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(JacksonModule.createJacksonModule());
    ErrorDataBuilder builder = new ErrorDataBuilder();
    builder.setAboutLink("about");
    builder.setCode("code");
    builder.setDetail("detail");
    builder.setId("id");
    builder.setSourcePointer("sourcePointer");
    builder.setSourceParameter("sourceParameter");
    builder.setStatus("status");
    builder.setTitle("title");
    builder.addMetaField("meta1", "value1");
    ErrorData errorData = builder.build();
    String json = mapper.writeValueAsString(errorData);
    Assert.assertTrue(json.contains("\"parameter\":\"sourceParameter\""));
    Assert.assertTrue(json.contains("\"pointer\":\"sourcePointer\""));
    Assert.assertTrue(json.contains("\"meta\":{\"meta1\":\"value1\"}"));
    Assert.assertTrue(json.contains("\"links\":{\"about\":\"about\"}"));
    ErrorData copy = mapper.readerFor(ErrorData.class).readValue(json);
    Assert.assertEquals(errorData, copy);
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) ErrorData(io.crnk.core.engine.document.ErrorData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 12 with ErrorData

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

the class ObjectLinkErrorDataTest method testSerialization.

@Test
@Override
public void testSerialization() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(JacksonModule.createJacksonModule(true));
    ErrorDataBuilder builder = new ErrorDataBuilder();
    builder.setAboutLink("about");
    builder.setCode("code");
    builder.setDetail("detail");
    builder.setId("id");
    builder.setSourcePointer("sourcePointer");
    builder.setSourceParameter("sourceParameter");
    builder.setStatus("status");
    builder.setTitle("title");
    builder.addMetaField("meta1", "value1");
    ErrorData errorData = builder.build();
    String json = mapper.writeValueAsString(errorData);
    Assert.assertTrue(json.contains("{\"about\":{\"href\":\"about\"}}"));
    ErrorData copy = mapper.readerFor(ErrorData.class).readValue(json);
    Assert.assertEquals(errorData, copy);
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) ErrorData(io.crnk.core.engine.document.ErrorData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 13 with ErrorData

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

the class HibernateConstraintViolationExceptionMapper method fromErrorResponse.

@Override
public ConstraintViolationException fromErrorResponse(ErrorResponse errorResponse) {
    Iterable<ErrorData> errors = errorResponse.getErrors();
    ErrorData error = errors.iterator().next();
    String msg = error.getDetail();
    String constraintName = error.getCode();
    return new ConstraintViolationException(msg, null, constraintName);
}
Also used : ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 14 with ErrorData

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

the class HibernateConstraintViolationExceptionMapper method toErrorResponse.

@Override
public ErrorResponse toErrorResponse(ConstraintViolationException cve) {
    HashMap<String, Object> meta = new HashMap<>();
    meta.put(META_TYPE_KEY, HIBERNATE_CONSTRAINT_VIOLATION_EXCEPTION);
    ErrorData error = ErrorData.builder().setMeta(meta).setStatus(Integer.toString(HttpStatus.UNPROCESSABLE_ENTITY_422)).setCode(cve.getConstraintName()).setDetail(cve.getCause() != null ? cve.getCause().getMessage() : cve.getMessage()).build();
    return ErrorResponse.builder().setStatus(HttpStatus.UNPROCESSABLE_ENTITY_422).setSingleErrorData(error).build();
}
Also used : HashMap(java.util.HashMap) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 15 with ErrorData

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

the class OptimisticLockExceptionMapper method accepts.

@Override
public boolean accepts(ErrorResponse errorResponse) {
    if (errorResponse.getHttpStatus() != HttpStatus.CONFLICT_409) {
        return false;
    }
    Iterable<ErrorData> errors = errorResponse.getErrors();
    Iterator<ErrorData> iterator = errors.iterator();
    if (!iterator.hasNext()) {
        return false;
    }
    ErrorData errorData = iterator.next();
    Map<String, Object> meta = errorData.getMeta();
    return meta != null && JPA_OPTIMISTIC_LOCK_EXCEPTION_TYPE.equals(meta.get(META_TYPE_KEY));
}
Also used : 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