Search in sources :

Example 36 with ErrorData

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());
}
Also used : WebApplicationExceptionMapper(io.crnk.rs.internal.WebApplicationExceptionMapper) WebApplicationException(javax.ws.rs.WebApplicationException) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 37 with ErrorData

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();
}
Also used : CrnkBoot(io.crnk.core.boot.CrnkBoot) ArrayList(java.util.ArrayList) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) Document(io.crnk.core.engine.document.Document) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Example 38 with ErrorData

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());
}
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 39 with ErrorData

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

Example 40 with ErrorData

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

Aggregations

ErrorData (io.crnk.core.engine.document.ErrorData)50 Test (org.junit.Test)30 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)10 Document (io.crnk.core.engine.document.Document)9 ErrorDataBuilder (io.crnk.core.engine.document.ErrorDataBuilder)8 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)6 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)6 ArrayList (java.util.ArrayList)5 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)3 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)3 BadRequestException (io.crnk.core.exception.BadRequestException)3 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)2 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)2 ResourceNotFoundException (io.crnk.core.exception.ResourceNotFoundException)2 Optional (io.crnk.core.utils.Optional)2 HashMap (java.util.HashMap)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1