use of io.crnk.core.engine.document.ErrorDataBuilder 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();
}
use of io.crnk.core.engine.document.ErrorDataBuilder in project crnk-framework by crnk-project.
the class CrnkErrorController method errorToJsonApi.
// TODO for whatever reason this is not called directly
@RequestMapping(produces = HttpHeaders.JSONAPI_CONTENT_TYPE)
@ResponseBody
public ResponseEntity<Document> errorToJsonApi(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
ErrorDataBuilder errorDataBuilder = ErrorData.builder();
for (Map.Entry<String, Object> attribute : body.entrySet()) {
if (attribute.getKey().equals("status")) {
errorDataBuilder.setStatus(attribute.getValue().toString());
} else if (attribute.getKey().equals("error")) {
errorDataBuilder.setTitle(attribute.getValue().toString());
} else if (attribute.getKey().equals("message")) {
errorDataBuilder.setDetail(attribute.getValue().toString());
} else {
errorDataBuilder.addMetaField(attribute.getKey(), attribute.getValue());
}
}
Document document = new Document();
document.setErrors(Arrays.asList(errorDataBuilder.build()));
return new ResponseEntity<>(document, status);
}
use of io.crnk.core.engine.document.ErrorDataBuilder in project crnk-framework by crnk-project.
the class ClientStubBaseTest method checkBodyWithErrorsButInvalidContentType.
@Test
public void checkBodyWithErrorsButInvalidContentType() throws IOException {
Document document = new Document();
ErrorData errorData = new ErrorDataBuilder().setCode("404").setDetail("detail").build();
document.setErrors(Arrays.asList(errorData));
String body = client.getObjectMapper().writeValueAsString(document);
HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
Mockito.when(response.body()).thenReturn(body);
Mockito.when(response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE)).thenReturn("not json api");
Mockito.when(response.code()).thenReturn(404);
RuntimeException exception = stub.handleError(response);
Assert.assertTrue(exception instanceof ResourceNotFoundException);
Assert.assertNull(exception.getMessage());
}
use of io.crnk.core.engine.document.ErrorDataBuilder in project crnk-framework by crnk-project.
the class ErrorDataTest method testToString.
@Test
public void testToString() {
ErrorDataBuilder builder = new ErrorDataBuilder();
builder.setTitle("title");
builder.setCode("code");
builder.setStatus("status");
builder.setDetail("detail");
builder.setSourcePointer("sourcePointer");
builder.setSourceParameter("sourceParameter");
String actual = builder.build().toString();
Assert.assertEquals("ErrorData{id='null', aboutLink='null', status='status', code='code', title='title', detail='detail', " + "sourcePointer='sourcePointer', sourceParameter='sourceParameter', meta=null}", actual);
}
use of io.crnk.core.engine.document.ErrorDataBuilder in project crnk-framework by crnk-project.
the class ConstraintViolationImplTest method setup.
@Before
public void setup() {
boot = new CrnkBoot();
boot.addModule(ValidationModule.create());
boot.setServiceDiscovery(new ReflectionsServiceDiscovery("io.crnk.validation.mock.repository"));
boot.boot();
errorData = Mockito.spy(new ErrorDataBuilder().setDetail("testMessage").addMetaField(ConstraintViolationExceptionMapper.META_RESOURCE_TYPE, "tasks").setSourcePointer("name").build());
ResourceRegistry resourceRegistry = boot.getResourceRegistry();
violation = ConstraintViolationImpl.fromError(resourceRegistry, errorData);
}
Aggregations