use of com.linkedin.restli.server.annotations.ServiceErrorDef in project rest.li by linkedin.
the class RestLiAnnotationReader method addServiceErrors.
/**
* Reads annotations on a given resource class in order to build service errors, which are then added to
* a given resource model.
*
* @param resourceModel resource model to add service errors to
* @param resourceClass class annotated with service errors
*/
private static void addServiceErrors(final ResourceModel resourceModel, final Class<?> resourceClass) {
final ServiceErrorDef serviceErrorDefAnnotation = resourceClass.getAnnotation(ServiceErrorDef.class);
final ServiceErrors serviceErrorsAnnotation = resourceClass.getAnnotation(ServiceErrors.class);
final List<ServiceError> serviceErrors = buildServiceErrors(serviceErrorDefAnnotation, serviceErrorsAnnotation, null, resourceClass, null);
if (serviceErrors == null) {
return;
}
resourceModel.setServiceErrors(serviceErrors);
}
use of com.linkedin.restli.server.annotations.ServiceErrorDef in project rest.li by linkedin.
the class RestLiAnnotationReader method addServiceErrors.
/**
* Reads annotations on a given method in order to build service errors, which are then added to
* a given resource method descriptor.
*
* @param resourceMethodDescriptor resource method descriptor to add service errors to
* @param method method annotated with service errors
*/
private static void addServiceErrors(final ResourceMethodDescriptor resourceMethodDescriptor, final Method method) {
final Class<?> resourceClass = method.getDeclaringClass();
final ServiceErrorDef serviceErrorDefAnnotation = resourceClass.getAnnotation(ServiceErrorDef.class);
final ServiceErrors serviceErrorsAnnotation = method.getAnnotation(ServiceErrors.class);
final ParamError[] paramErrorAnnotations = method.getAnnotationsByType(ParamError.class);
final List<ServiceError> serviceErrors = buildServiceErrors(serviceErrorDefAnnotation, serviceErrorsAnnotation, paramErrorAnnotations, resourceClass, method);
if (serviceErrors == null) {
return;
}
// Form a set of parameter names which exist on this method
final Set<String> acceptableParameterNames = resourceMethodDescriptor.getParameters().stream().map(Parameter::getName).collect(Collectors.toSet());
// Validate that all parameter names are valid
for (ServiceError serviceError : serviceErrors) {
if (serviceError instanceof ParametersServiceError) {
final String[] parameterNames = ((ParametersServiceError) serviceError).parameterNames();
if (parameterNames != null) {
for (String parameterName : parameterNames) {
if (!acceptableParameterNames.contains(parameterName)) {
throw new ResourceConfigException(String.format("Nonexistent parameter '%s' specified for method-level service error '%s' in %s (valid parameters: %s)", parameterName, serviceError.code(), buildExceptionLocationString(resourceClass, method), acceptableParameterNames.toString()));
}
}
}
}
}
resourceMethodDescriptor.setServiceErrors(serviceErrors);
}
Aggregations