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