Java Code Examples for com.google.rpc.Status#getDetailsList()

The following examples show how to use com.google.rpc.Status#getDetailsList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Actions.java    From bazel-buildfarm with Apache License 2.0 8 votes vote down vote up
public static boolean isRetriable(Status status) {
  if (status == null
      || status.getCode() != Code.FAILED_PRECONDITION.getNumber()
      || status.getDetailsCount() == 0) {
    return false;
  }
  for (Any details : status.getDetailsList()) {
    try {
      PreconditionFailure f = details.unpack(PreconditionFailure.class);
      if (f.getViolationsCount() == 0) {
        return false; // Generally shouldn't happen
      }
      for (Violation v : f.getViolationsList()) {
        if (!v.getType().equals(Errors.VIOLATION_TYPE_MISSING)) {
          return false;
        }
      }
    } catch (InvalidProtocolBufferException protoEx) {
      return false;
    }
  }
  return true; // if *all* > 0 violations have type MISSING
}
 
Example 2
Source File: GrpcClientErrorUtils.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
public static <D extends MessageOrBuilder> Optional<D> getDetail(StatusRuntimeException error, Class<D> detailType) {
    Status status = getStatus(error);
    for (Any any : status.getDetailsList()) {
        Descriptors.Descriptor descriptor = any.getDescriptorForType();
        Descriptors.FieldDescriptor typeUrlField = descriptor.findFieldByName("type_url");
        String typeUrl = (String) any.getField(typeUrlField);

        Class type;
        if (typeUrl.contains(DebugInfo.class.getSimpleName())) {
            type = DebugInfo.class;
        } else if (typeUrl.contains(BadRequest.class.getSimpleName())) {
            type = BadRequest.class;
        } else {
            return Optional.empty();
        }
        if (type == detailType) {
            Message unpack;
            try {
                unpack = any.unpack(type);
            } catch (InvalidProtocolBufferException e) {
                throw new IllegalArgumentException("Cannot unpack error details", e);
            }
            return Optional.of((D) unpack);
        }
    }
    return Optional.empty();
}
 
Example 3
Source File: JobsRpcUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the given {@link Status} to a {@link UserException}, if possible.
 *
 * @param statusProto status runtime exception
 * @return user exception if one is passed as part of the details
 */
public static Optional<UserException> fromStatus(Status statusProto) {
  for (Any details : statusProto.getDetailsList()) {
    if (details.is(DremioPBError.class)) {
      try {
        return Optional.of(UserRemoteException.create(details.unpack(DremioPBError.class)));
      } catch (InvalidProtocolBufferException e) {
        logger.warn("Received an invalid UserException, ignoring", e);
        return Optional.empty();
      }
    }
  }

  return Optional.empty();
}
 
Example 4
Source File: AbstractErrorUtils.java    From google-ads-java with Apache License 2.0 3 votes vote down vote up
/**
 * Gets a list of all partial failure error messages for a given response. Operations are indexed
 * from 0.
 *
 * <p>For example, given the following Failure:
 *
 * <pre>
 *   <code>
 *     errors {
 *       message: "Too low."
 *       location {
 *         field_path_elements {
 *           field_name: "operations"
 *           index {
 *             value: 1
 *           }
 *         }
 *         field_path_elements {
 *           field_name: "create"
 *         }
 *         field_path_elements {
 *           field_name: "campaign"
 *         }
 *       }
 *     }
 *     errors {
 *       message: "Too low."
 *       location {
 *         field_path_elements {
 *           field_name: "operations"
 *           index {
 *             value: 2
 *           }
 *         }
 *         field_path_elements {
 *           field_name: "create"
 *         }
 *         field_path_elements {
 *           field_name: "campaign"
 *         }
 *       }
 *     }
 *   </code>
 * </pre>
 *
 * A single {@link GoogleAdsErrorT} instance would be returned for operation index 1 and 2, and an
 * empty list otherwise.
 *
 * <p>This method supports <code>XXXService.mutate(request)</code> where the request contains a
 * list of operations named "operations". It also supports
 * <code>GoogleAdsService.mutateGoogleAds(request)</code>, where the request contains a list of
 * <code>MutateOperation</code>s named "mutate_operations".
 *
 * @param operationIndex the index of the operation, starting from 0.
 * @param partialFailureStatus a partialFailure status, with the detail list containing {@link
 *     GoogleAdsFailureT} instances.
 * @return a list containing the {@link GoogleAdsErrorT} instances for a given operation index.
 * @throws InvalidProtocolBufferException if not able to unpack the protocol buffer. This is most
 *     likely due to using the wrong version of ErrorUtils being used with the API response.
 */
public List<GoogleAdsErrorT> getGoogleAdsErrors(long operationIndex, Status partialFailureStatus)
    throws InvalidProtocolBufferException {
  List<GoogleAdsErrorT> result = new ArrayList();
  for (Any detail : partialFailureStatus.getDetailsList()) {
    GoogleAdsFailureT failure = getGoogleAdsFailure(detail);
    result.addAll(getGoogleAdsErrors(operationIndex, failure));
  }
  return result;
}