Java Code Examples for com.google.api.client.googleapis.json.GoogleJsonError#getCode()

The following examples show how to use com.google.api.client.googleapis.json.GoogleJsonError#getCode() . 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: AsyncRequest.java    From connector-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Wrapper on {@link JsonBatchCallback#onFailure} to record failure while executing batched
 * request.
 */
@Override
public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) {
  if (event != null) {
    event.failure();
    event = null;
  } else {
    operationStats.event(request.requestToExecute.getClass().getName()).failure();
  }
  logger.log(Level.WARNING, "Request failed with error {0}", error);
  if (request.retryPolicy.isRetryableStatusCode(error.getCode())) {
    if (request.getRetries() < request.retryPolicy.getMaxRetryLimit()) {
      request.setStatus(Status.RETRYING);
      request.incrementRetries();
      return;
    }
  }
  GoogleJsonResponseException exception =
      new GoogleJsonResponseException(
          new Builder(error.getCode(), error.getMessage(), responseHeaders), error);
  fail(exception);
}
 
Example 2
Source File: GoogleDriveFileSystem.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean delete(Path path, boolean recursive) throws IOException {
  Preconditions.checkArgument(recursive, "Non-recursive is not supported.");
  String fileId = toFileId(path);
  LOG.debug("Deleting file: " + fileId);
  try {
    client.files().delete(fileId).execute();
  } catch (GoogleJsonResponseException e) {
    GoogleJsonError error = e.getDetails();
    if (404 == error.getCode()) { //Non-existing file id
      return false;
    }
    throw e;
  }
  return true;
}
 
Example 3
Source File: BaseWorkflowSample.java    From googleads-shopping-samples with Apache License 2.0 6 votes vote down vote up
protected static void checkGoogleJsonResponseException(GoogleJsonResponseException e)
    throws GoogleJsonResponseException {
  GoogleJsonError err = e.getDetails();
  // err can be null if response is not JSON
  if (err != null) {
    // For errors in the 4xx range, print out the errors and continue normally.
    if (err.getCode() >= 400 && err.getCode() < 500) {
      System.out.printf("There are %d error(s)%n", err.getErrors().size());
      for (ErrorInfo info : err.getErrors()) {
        System.out.printf("- [%s] %s%n", info.getReason(), info.getMessage());
      }
    } else {
      throw e;
    }
  } else {
    throw e;
  }
}
 
Example 4
Source File: IndexingServiceTest.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
private static <T> ListenableFuture<T> getExceptionFuture(GoogleJsonError e) {
  SettableFuture<T> settable = SettableFuture.create();
  GoogleJsonResponseException exception =
      new GoogleJsonResponseException(
          new Builder(e.getCode(), e.getMessage(), new HttpHeaders()), e);
  settable.setException(exception);
  return settable;
}
 
Example 5
Source File: ApiErrorExtractor.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the given exception indicates that 'userProject' is missing in request.
 * Recursively checks getCause() if outer exception isn't an instance of the correct class.
 */
public boolean userProjectMissing(IOException e) {
  GoogleJsonError jsonError = getJsonError(e);
  return jsonError != null
      && jsonError.getCode() == HttpStatusCodes.STATUS_CODE_BAD_REQUEST
      && USER_PROJECT_MISSING_MESSAGE.equals(jsonError.getMessage());
}
 
Example 6
Source File: GoogleCloudStorageExceptions.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
public static GoogleJsonResponseException createJsonResponseException(
    GoogleJsonError e, HttpHeaders responseHeaders) {
  if (e != null) {
    return new GoogleJsonResponseException(
        new HttpResponseException.Builder(e.getCode(), e.getMessage(), responseHeaders), e);
  }
  return null;
}
 
Example 7
Source File: DirectoryGroupsConnection.java    From nomulus with Apache License 2.0 4 votes vote down vote up
@Override
public void addMemberToGroup(String groupKey, String email, Role role) throws IOException {
  // Documentation for this API call:
  // https://developers.google.com/admin-sdk/directory/v1/reference/members/insert
  Member member = new Member();
  member.setEmail(email);
  member.setRole(role.toString());
  try {
    directory.members().insert(groupKey, member).execute();
  } catch (GoogleJsonResponseException e) {
    // If the member is already in the group, ignore the error, get the existing member, and
    // return it.
    GoogleJsonError err = e.getDetails();
    if (err == null) {
      throw e;
    } else if (err.getCode() == SC_NOT_FOUND && err.getMessage().equals(GROUP_NOT_FOUND_MSG)) {
      logger.atInfo().withCause(e).log(
          "Creating group %s during addition of member %s because the group doesn't exist.",
          groupKey, email);
      createGroup(groupKey);
      addMemberToGroup(groupKey, email, role);
    } else if (err.getCode() == SC_NOT_FOUND && err.getMessage().equals(MEMBER_NOT_FOUND_MSG)) {
      throw new RuntimeException(String.format(
          "Adding member %s to group %s failed because the member wasn't found.",
          email,
          groupKey), e);
    } else if (err.getCode() == SC_CONFLICT
        && err.getMessage().equals(MEMBER_ALREADY_EXISTS_MSG)) {
      // This error case usually happens when an email address is already a member of the gorup,
      // but it is bouncing incoming emails. It won't show up in the members list API call, but
      // will throw a "Member already exists" error message if you attempt to add it again. The
      // correct thing to do is log an info message when this happens and then ignore it.
      logger.atInfo().withCause(e).log(
          "Could not add email %s to group %s because it is already a member "
              + "(likely because the email address is bouncing incoming messages).",
          email, groupKey);
    } else {
      throw e;
    }
  }
}