Search in sources :

Example 6 with ErrorDataBuilder

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();
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) ArrayList(java.util.ArrayList) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 7 with ErrorDataBuilder

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);
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) Document(io.crnk.core.engine.document.Document) Map(java.util.Map) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with ErrorDataBuilder

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());
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) Document(io.crnk.core.engine.document.Document) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 9 with ErrorDataBuilder

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);
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) Test(org.junit.Test)

Example 10 with ErrorDataBuilder

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);
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) CrnkBoot(io.crnk.core.boot.CrnkBoot) ReflectionsServiceDiscovery(io.crnk.core.module.discovery.ReflectionsServiceDiscovery) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) Before(org.junit.Before)

Aggregations

ErrorDataBuilder (io.crnk.core.engine.document.ErrorDataBuilder)12 ErrorData (io.crnk.core.engine.document.ErrorData)8 Test (org.junit.Test)7 Document (io.crnk.core.engine.document.Document)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)2 ResourceNotFoundException (io.crnk.core.exception.ResourceNotFoundException)2 ArrayList (java.util.ArrayList)2 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)1 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)1 ReflectionsServiceDiscovery (io.crnk.core.module.discovery.ReflectionsServiceDiscovery)1 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)1 Map (java.util.Map)1 Before (org.junit.Before)1 HttpStatus (org.springframework.http.HttpStatus)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1