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);
}
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);
}
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);
}
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();
}
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));
}
Aggregations