Search in sources :

Example 6 with ServiceError

use of com.linkedin.restli.server.errors.ServiceError 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);
}
Also used : ParametersServiceError(com.linkedin.restli.server.errors.ParametersServiceError) ServiceErrorDef(com.linkedin.restli.server.annotations.ServiceErrorDef) ParametersServiceError(com.linkedin.restli.server.errors.ParametersServiceError) ServiceError(com.linkedin.restli.server.errors.ServiceError) ServiceErrors(com.linkedin.restli.server.annotations.ServiceErrors) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) ParamError(com.linkedin.restli.server.annotations.ParamError)

Aggregations

ServiceError (com.linkedin.restli.server.errors.ServiceError)6 ParametersServiceError (com.linkedin.restli.server.errors.ParametersServiceError)4 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)2 ServiceErrorDef (com.linkedin.restli.server.annotations.ServiceErrorDef)2 ServiceErrors (com.linkedin.restli.server.annotations.ServiceErrors)2 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)2 HashSet (java.util.HashSet)2 DataMap (com.linkedin.data.DataMap)1 StringArray (com.linkedin.data.template.StringArray)1 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)1 RequestContext (com.linkedin.r2.message.RequestContext)1 ErrorDetails (com.linkedin.restli.common.ErrorDetails)1 HttpStatus (com.linkedin.restli.common.HttpStatus)1 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)1 ResourceMethod (com.linkedin.restli.common.ResourceMethod)1 MutablePathKeys (com.linkedin.restli.internal.server.MutablePathKeys)1 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)1 Parameter (com.linkedin.restli.internal.server.model.Parameter)1 ServiceErrorSchema (com.linkedin.restli.restspec.ServiceErrorSchema)1 ServiceErrorSchemaArray (com.linkedin.restli.restspec.ServiceErrorSchemaArray)1