Search in sources :

Example 21 with ErrorData

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

the class ErrorDataDeserializer method deserialize.

@Override
public ErrorData deserialize(JsonParser jp, DeserializationContext context) throws IOException {
    JsonNode errorNode = jp.readValueAsTree();
    String id = SerializerUtil.readStringIfExists(ErrorDataSerializer.ID, errorNode);
    String aboutLink = readAboutLink(errorNode);
    String status = SerializerUtil.readStringIfExists(ErrorDataSerializer.STATUS, errorNode);
    String code = SerializerUtil.readStringIfExists(ErrorDataSerializer.CODE, errorNode);
    String title = SerializerUtil.readStringIfExists(ErrorDataSerializer.TITLE, errorNode);
    String detail = SerializerUtil.readStringIfExists(ErrorDataSerializer.DETAIL, errorNode);
    Map<String, Object> meta = readMeta(errorNode, jp);
    String sourcePointer = readSourcePointer(errorNode);
    String sourceParameter = readSourceParameter(errorNode);
    return new ErrorData(id, aboutLink, status, code, title, detail, sourcePointer, sourceParameter, meta);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 22 with ErrorData

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

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

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

the class ExceptionMapperHelper method createErrorMessage.

public static String createErrorMessage(ErrorResponse errorResponse) {
    Iterator<ErrorData> errors = errorResponse.getErrors().iterator();
    String message = null;
    if (errors.hasNext()) {
        ErrorData data = errors.next();
        message = data.getCode();
    }
    return message;
}
Also used : ErrorData(io.crnk.core.engine.document.ErrorData)

Example 25 with ErrorData

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

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