Search in sources :

Example 1 with ErrorDataBuilder

use of io.crnk.core.engine.document.ErrorDataBuilder 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 2 with ErrorDataBuilder

use of io.crnk.core.engine.document.ErrorDataBuilder 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 3 with ErrorDataBuilder

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

the class CustomExceptionMapper method toErrorResponse.

@Override
public ErrorResponse toErrorResponse(CustomException e) {
    ErrorDataBuilder builder = ErrorData.builder();
    builder.setStatus(String.valueOf(CUSTOM_ERROR_STATUS_CODE));
    builder.setTitle(e.getMessage());
    ErrorData error = builder.build();
    List<ErrorData> errors = Arrays.asList(error);
    return ErrorResponse.builder().setStatus(CUSTOM_ERROR_STATUS_CODE).setErrorData(errors).build();
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 4 with ErrorDataBuilder

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

the class ClientStubBaseTest method checkBodyWithErrors.

@Test
public void checkBodyWithErrors() 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(HttpHeaders.JSONAPI_CONTENT_TYPE);
    Mockito.when(response.code()).thenReturn(404);
    RuntimeException exception = stub.handleError(response);
    Assert.assertTrue(exception instanceof ResourceNotFoundException);
    Assert.assertEquals("detail", 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 5 with ErrorDataBuilder

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

the class ExceptionMapperHelper method toErrorResponse.

public static ErrorResponse toErrorResponse(Throwable exception, int statusCode, String metaTypeValue) {
    List<ErrorData> errors = new ArrayList<>();
    ErrorDataBuilder builder = ErrorData.builder();
    builder = builder.addMetaField(ExceptionMapperHelper.META_TYPE_KEY, metaTypeValue);
    builder = builder.setStatus(String.valueOf(statusCode));
    builder = builder.setCode(exception.getMessage());
    builder = builder.setTitle(exception.getLocalizedMessage());
    ErrorData error = builder.build();
    errors.add(error);
    return ErrorResponse.builder().setStatus(statusCode).setErrorData(errors).build();
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) ArrayList(java.util.ArrayList) ErrorData(io.crnk.core.engine.document.ErrorData)

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