use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class WebApplicationExceptionMapperTest method test.
@Test
public void test() {
WebApplicationExceptionMapper mapper = new WebApplicationExceptionMapper();
WebApplicationException exception = new WebApplicationException("hi");
ErrorResponse response = mapper.toErrorResponse(exception);
Iterable<ErrorData> errors = response.getErrors();
Iterator<ErrorData> iterator = errors.iterator();
ErrorData data = iterator.next();
Assert.assertFalse(iterator.hasNext());
Assert.assertEquals("500", data.getStatus());
Assert.assertEquals("hi", data.getCode());
Assert.assertTrue(mapper.accepts(response));
WebApplicationException fromErrorResponse = mapper.fromErrorResponse(response);
Assert.assertEquals("hi", fromErrorResponse.getMessage());
}
use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class JsonapiExceptionMapperBridge method toResponse.
@Override
public Response toResponse(RuntimeException exception) {
CrnkBoot boot = this.feature.getBoot();
ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
Optional<JsonApiExceptionMapper> optional = exceptionMapperRegistry.findMapperFor(exception.getClass());
if (!optional.isPresent()) {
LOGGER.error("no exception mapper found", exception);
exception = new InternalServerErrorException(exception.getMessage());
optional = exceptionMapperRegistry.findMapperFor(exception.getClass());
}
JsonApiExceptionMapper exceptionMapper = optional.get();
ErrorResponse errorResponse = exceptionMapper.toErrorResponse(exception);
// use the Crnk document mapper to create a JSON API response
Document doc = new Document();
List<ErrorData> errors = new ArrayList<>();
for (ErrorData error : errorResponse.getErrors()) {
errors.add(error);
}
doc.setErrors(errors);
return Response.status(errorResponse.getHttpStatus()).entity(doc).header("Content-Type", JsonApiMediaType.APPLICATION_JSON_API).build();
}
use of io.crnk.core.engine.document.ErrorData 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.ErrorData in project crnk-framework by crnk-project.
the class ClientStubBaseTest method checkBodyWithNoErrorsAnd500Status.
@Test
public void checkBodyWithNoErrorsAnd500Status() throws IOException {
Document document = new Document();
document.setErrors(new ArrayList<ErrorData>());
String body = client.getObjectMapper().writeValueAsString(document);
HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
Mockito.when(response.body()).thenReturn(body);
Mockito.when(response.code()).thenReturn(500);
RuntimeException exception = stub.handleError(response);
Assert.assertTrue(exception instanceof InternalServerErrorException);
}
use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class CrnkExceptionMapper method getMessage.
private String getMessage(ErrorResponse errorResponse) {
Iterator<ErrorData> errors = errorResponse.getErrors().iterator();
String message = null;
if (errors.hasNext()) {
ErrorData data = errors.next();
message = data.getDetail();
}
return message;
}
Aggregations