Search in sources :

Example 1 with InvalidResourceException

use of io.crnk.core.exception.InvalidResourceException in project crnk-framework by crnk-project.

the class ResourceInformation method initAny.

private void initAny() {
    final Method jsonAnyGetter = ClassUtils.findMethodWith(resourceClass, JsonAnyGetter.class);
    final Method jsonAnySetter = ClassUtils.findMethodWith(resourceClass, JsonAnySetter.class);
    if (absentAnySetter(jsonAnyGetter, jsonAnySetter)) {
        throw new InvalidResourceException(String.format("A resource %s has to have both methods annotated with @JsonAnySetter and @JsonAnyGetter", resourceClass.getCanonicalName()));
    }
    if (jsonAnyGetter != null) {
        anyFieldAccessor = new AnyResourceFieldAccessor() {

            @Override
            public Object getValue(Object resource, String name) {
                try {
                    return jsonAnyGetter.invoke(resource, name);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new ResourceException(String.format("Exception while reading %s.%s due to %s", resource, name, e.getMessage()), e);
                }
            }

            @Override
            public void setValue(Object resource, String name, Object fieldValue) {
                try {
                    jsonAnySetter.invoke(resource, name, fieldValue);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new ResourceException(String.format("Exception while writting %s.%s=%s due to %s", resource, name, fieldValue, e.getMessage()), e);
                }
            }
        };
    }
}
Also used : InvalidResourceException(io.crnk.core.exception.InvalidResourceException) InvalidResourceException(io.crnk.core.exception.InvalidResourceException) ResourceException(io.crnk.core.exception.ResourceException) Method(java.lang.reflect.Method)

Example 2 with InvalidResourceException

use of io.crnk.core.exception.InvalidResourceException in project crnk-framework by crnk-project.

the class DefaultExceptionMapperLookup method getExceptionMappers.

@Override
public Set<JsonApiExceptionMapper> getExceptionMappers() {
    Reflections reflections;
    if (resourceSearchPackages != null) {
        reflections = new Reflections(resourceSearchPackages);
    } else {
        reflections = new Reflections();
    }
    Set<Class<?>> exceptionMapperClasses = reflections.getTypesAnnotatedWith(ExceptionMapperProvider.class);
    Set<JsonApiExceptionMapper> exceptionMappers = new HashSet<>();
    for (Class<?> exceptionMapperClazz : exceptionMapperClasses) {
        if (!JsonApiExceptionMapper.class.isAssignableFrom(exceptionMapperClazz)) {
            throw new InvalidResourceException(exceptionMapperClazz.getCanonicalName() + " is not an implementation of JsonApiExceptionMapper");
        }
        try {
            exceptionMappers.add((JsonApiExceptionMapper<? extends Throwable>) exceptionMapperClazz.newInstance());
        } catch (Exception e) {
            throw new InvalidResourceException(exceptionMapperClazz.getCanonicalName() + " can not be initialized", e);
        }
    }
    return exceptionMappers;
}
Also used : InvalidResourceException(io.crnk.core.exception.InvalidResourceException) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) InvalidResourceException(io.crnk.core.exception.InvalidResourceException) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Aggregations

InvalidResourceException (io.crnk.core.exception.InvalidResourceException)2 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)1 ResourceException (io.crnk.core.exception.ResourceException)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 Reflections (org.reflections.Reflections)1