Search in sources :

Example 1 with ExceptionMapperRegistry

use of io.crnk.core.engine.internal.exception.ExceptionMapperRegistry in project crnk-framework by crnk-project.

the class JpaExceptionMapperTests method testPersistenceException.

@Test
public void testPersistenceException() {
    PersistenceException exception = new PersistenceException(new BadRequestException("test"));
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    PersistenceExceptionMapper mapper = (PersistenceExceptionMapper) exceptionMapperRegistry.findMapperFor(PersistenceException.class).get();
    ErrorResponse response = mapper.toErrorResponse(exception);
    ErrorData errorData = response.getErrors().iterator().next();
    Assert.assertEquals(Integer.toString(HttpStatus.BAD_REQUEST_400), errorData.getStatus());
    Assert.assertEquals("test", errorData.getDetail());
}
Also used : PersistenceException(javax.persistence.PersistenceException) BadRequestException(io.crnk.core.exception.BadRequestException) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) PersistenceExceptionMapper(io.crnk.jpa.internal.PersistenceExceptionMapper) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 2 with ExceptionMapperRegistry

use of io.crnk.core.engine.internal.exception.ExceptionMapperRegistry in project crnk-framework by crnk-project.

the class JpaExceptionMapperTests method testPersistenceRollbackException.

@Test
public void testPersistenceRollbackException() {
    javax.persistence.RollbackException exception = new javax.persistence.RollbackException(new BadRequestException("test"));
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    PersistenceRollbackExceptionMapper mapper = (PersistenceRollbackExceptionMapper) exceptionMapperRegistry.findMapperFor(javax.persistence.RollbackException.class).get();
    ErrorResponse response = mapper.toErrorResponse(exception);
    ErrorData errorData = response.getErrors().iterator().next();
    Assert.assertEquals(Integer.toString(HttpStatus.BAD_REQUEST_400), errorData.getStatus());
    Assert.assertEquals("test", errorData.getDetail());
}
Also used : BadRequestException(io.crnk.core.exception.BadRequestException) PersistenceRollbackExceptionMapper(io.crnk.jpa.internal.PersistenceRollbackExceptionMapper) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 3 with ExceptionMapperRegistry

use of io.crnk.core.engine.internal.exception.ExceptionMapperRegistry in project crnk-framework by crnk-project.

the class ClientStubBase method handleError.

protected RuntimeException handleError(HttpAdapterResponse response) throws IOException {
    ErrorResponse errorResponse = null;
    String body = response.body();
    String contentType = response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE);
    if (body.length() > 0 && contentType != null && contentType.toLowerCase().contains(HttpHeaders.JSONAPI_CONTENT_TYPE)) {
        ObjectMapper objectMapper = client.getObjectMapper();
        Document document = objectMapper.readValue(body, Document.class);
        if (document.getErrors() != null && !document.getErrors().isEmpty()) {
            errorResponse = new ErrorResponse(document.getErrors(), response.code());
        }
    }
    if (errorResponse == null) {
        errorResponse = new ErrorResponse(null, response.code());
    }
    ExceptionMapperRegistry exceptionMapperRegistry = client.getExceptionMapperRegistry();
    Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(errorResponse);
    if (mapper.isPresent()) {
        Throwable throwable = mapper.get().fromErrorResponse(errorResponse);
        if (throwable instanceof RuntimeException) {
            return (RuntimeException) throwable;
        } else {
            return new ClientException(response.code(), response.message(), throwable);
        }
    } else {
        return new ClientException(response.code(), response.message());
    }
}
Also used : ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) ClientException(io.crnk.client.ClientException) Document(io.crnk.core.engine.document.Document) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Example 4 with ExceptionMapperRegistry

use of io.crnk.core.engine.internal.exception.ExceptionMapperRegistry in project crnk-framework by crnk-project.

the class RegistryEntry method toResource.

private Object toResource(JsonApiResponse response) {
    if (response.getErrors() != null && response.getErrors().iterator().hasNext()) {
        List<ErrorData> errorList = new ArrayList<>();
        response.getErrors().forEach(it -> errorList.add(it));
        Optional<Integer> errorCode = errorList.stream().filter(it -> it.getStatus() != null).map(it -> Integer.parseInt(it.getStatus())).collect(Collectors.maxBy(Integer::compare));
        ErrorResponse errorResponse = new ErrorResponse(errorList, errorCode.get());
        ExceptionMapperRegistry exceptionMapperRegistry = moduleRegistry.getExceptionMapperRegistry();
        ExceptionMapper<Throwable> exceptionMapper = exceptionMapperRegistry.findMapperFor(errorResponse).get();
        return exceptionMapper.fromErrorResponse(errorResponse);
    }
    return response.getEntity();
}
Also used : ResourceRepositoryInformation(io.crnk.core.engine.information.repository.ResourceRepositoryInformation) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) ResourceList(io.crnk.core.resource.list.ResourceList) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) HashMap(java.util.HashMap) ResourceField(io.crnk.core.engine.information.resource.ResourceField) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) ArrayList(java.util.ArrayList) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceFieldNotFoundException(io.crnk.core.exception.ResourceFieldNotFoundException) Map(java.util.Map) AnnotatedResourceEntry(io.crnk.legacy.registry.AnnotatedResourceEntry) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) QuerySpec(io.crnk.core.queryspec.QuerySpec) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Collection(java.util.Collection) ModuleRegistry(io.crnk.core.module.ModuleRegistry) Collectors(java.util.stream.Collectors) AnnotatedRelationshipEntryBuilder(io.crnk.legacy.registry.AnnotatedRelationshipEntryBuilder) ErrorData(io.crnk.core.engine.document.ErrorData) Serializable(java.io.Serializable) List(java.util.List) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) Optional(java.util.Optional) PreconditionUtil(io.crnk.core.engine.internal.utils.PreconditionUtil) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) RelationshipRepositoryNotFoundException(io.crnk.core.exception.RelationshipRepositoryNotFoundException) ArrayList(java.util.ArrayList) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Example 5 with ExceptionMapperRegistry

use of io.crnk.core.engine.internal.exception.ExceptionMapperRegistry in project crnk-framework by crnk-project.

the class JpaExceptionMapperTests method testTransactionRollbackException.

@Test
public void testTransactionRollbackException() {
    javax.transaction.RollbackException exception = new javax.transaction.RollbackException() {

        public Throwable getCause() {
            return new BadRequestException("test");
        }
    };
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    TransactionRollbackExceptionMapper mapper = (TransactionRollbackExceptionMapper) exceptionMapperRegistry.findMapperFor(exception.getClass()).get();
    ErrorResponse response = mapper.toErrorResponse(exception);
    ErrorData errorData = response.getErrors().iterator().next();
    Assert.assertEquals(Integer.toString(HttpStatus.BAD_REQUEST_400), errorData.getStatus());
    Assert.assertEquals("test", errorData.getDetail());
}
Also used : TransactionRollbackExceptionMapper(io.crnk.jpa.internal.TransactionRollbackExceptionMapper) BadRequestException(io.crnk.core.exception.BadRequestException) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Aggregations

ErrorResponse (io.crnk.core.engine.error.ErrorResponse)7 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)7 ErrorData (io.crnk.core.engine.document.ErrorData)6 Test (org.junit.Test)4 BadRequestException (io.crnk.core.exception.BadRequestException)3 Document (io.crnk.core.engine.document.Document)2 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)2 ArrayList (java.util.ArrayList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ClientException (io.crnk.client.ClientException)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)1 ResourceRepositoryInformation (io.crnk.core.engine.information.repository.ResourceRepositoryInformation)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)1 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)1 PreconditionUtil (io.crnk.core.engine.internal.utils.PreconditionUtil)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)1