Search in sources :

Example 46 with ErrorData

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

the class ErrorDataBuilderTest method shouldSetStatus.

@Test
public void shouldSetStatus() throws Exception {
    ErrorData error = ErrorData.builder().setStatus(ErrorDataMother.STATUS).build();
    assertThat(error.getStatus()).isEqualTo(ErrorDataMother.STATUS);
}
Also used : ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 47 with ErrorData

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

the class AbstractWrappedExceptionMapper method toErrorResponse.

@Override
public ErrorResponse toErrorResponse(E exception) {
    Throwable cause = exception.getCause();
    if (cause != null) {
        Optional<JsonApiExceptionMapper> mapper = context.getExceptionMapperRegistry().findMapperFor(cause.getClass());
        if (mapper.isPresent()) {
            return mapper.get().toErrorResponse(cause);
        }
    }
    LOGGER.error("failed to process request due to jpa exception", exception);
    // no mapper found, return default error
    int status = getStatus();
    ErrorData errorData = ErrorData.builder().setStatus(Integer.toString(status)).setTitle(exception.getMessage()).build();
    return ErrorResponse.builder().setSingleErrorData(errorData).setStatus(status).build();
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ErrorData(io.crnk.core.engine.document.ErrorData)

Example 48 with ErrorData

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

the class HibernateConstraintViolationExceptionMapper method accepts.

@Override
public boolean accepts(ErrorResponse errorResponse) {
    if (errorResponse.getHttpStatus() != HttpStatus.UNPROCESSABLE_ENTITY_422) {
        return false;
    }
    Iterable<ErrorData> errors = errorResponse.getErrors();
    Iterator<ErrorData> iterator = errors.iterator();
    if (!iterator.hasNext())
        return false;
    ErrorData errorData = iterator.next();
    Map<String, Object> meta = errorData.getMeta();
    return meta != null && HIBERNATE_CONSTRAINT_VIOLATION_EXCEPTION.equals(meta.get(META_TYPE_KEY));
}
Also used : ErrorData(io.crnk.core.engine.document.ErrorData)

Example 49 with ErrorData

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

the class BraveResponseFilterTest method statusCodeNotOkWhenEmptyErrors.

@Test
public void statusCodeNotOkWhenEmptyErrors() {
    ErrorData errorData = new ErrorDataBuilder().setId("test").build();
    JsonApiResponse response = new JsonApiResponse();
    response.setErrors(Arrays.asList(errorData));
    Mockito.when(filterChain.doFilter(Mockito.any(RepositoryFilterContext.class))).thenReturn(response);
    filter.filterRequest(filterContext, filterChain);
    Mockito.verify(span, Mockito.times(1)).tag(BraveRepositoryFilter.STATUS_CODE_ANNOTATION, BraveRepositoryFilter.STRING_EXCEPTION);
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 50 with ErrorData

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

the class BraveResponseFilterTest method statusCodeOkWhenEmptyErrors.

@Test
public void statusCodeOkWhenEmptyErrors() {
    JsonApiResponse response = new JsonApiResponse();
    response.setErrors(new ArrayList<ErrorData>());
    Mockito.when(filterChain.doFilter(Mockito.any(RepositoryFilterContext.class))).thenReturn(response);
    filter.filterRequest(filterContext, filterChain);
    Mockito.verify(span, Mockito.times(1)).tag(BraveRepositoryFilter.STATUS_CODE_ANNOTATION, BraveRepositoryFilter.STRING_OK);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

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