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);
}
}
};
}
}
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;
}
Aggregations