Search in sources :

Example 6 with ExceptionMapper

use of io.crnk.core.engine.error.ExceptionMapper in project crnk-framework by crnk-project.

the class ExceptionMapperRegistry method findMapperFor.

@SuppressWarnings({ "rawtypes", "unchecked" })
public <E extends Throwable> Optional<ExceptionMapper<E>> findMapperFor(ErrorResponse errorResponse) {
    int currentDepth = -1;
    ExceptionMapper closestExceptionMapper = null;
    for (ExceptionMapperType mapperType : exceptionMappers) {
        JsonApiExceptionMapper mapperObj = mapperType.getExceptionMapper();
        if (mapperObj instanceof ExceptionMapper) {
            ExceptionMapper mapper = (ExceptionMapper) mapperObj;
            boolean accepted = mapper.accepts(errorResponse);
            if (accepted) {
                // the exception with the most super types is chosen
                int tempDepth = countSuperTypes(mapperType.getExceptionClass());
                if (tempDepth > currentDepth) {
                    currentDepth = tempDepth;
                    closestExceptionMapper = mapper;
                }
            }
        }
    }
    return (Optional) Optional.ofNullable(closestExceptionMapper);
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper)

Aggregations

ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)6 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)5 Optional (io.crnk.core.utils.Optional)5 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)4 ErrorData (io.crnk.core.engine.document.ErrorData)3 Test (org.junit.Test)3 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ClientException (io.crnk.client.ClientException)1 Document (io.crnk.core.engine.document.Document)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 RelationshipRepositoryNotFoundException (io.crnk.core.exception.RelationshipRepositoryNotFoundException)1 ResourceFieldNotFoundException (io.crnk.core.exception.ResourceFieldNotFoundException)1 ModuleRegistry (io.crnk.core.module.ModuleRegistry)1