Account for GoogleJsonResponseException#getDetails returning null

Apparently, this can happen

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=223163802
This commit is contained in:
guyben 2018-11-28 07:33:45 -08:00 committed by jianglai
parent c2ee453745
commit dbdc69916a
5 changed files with 22 additions and 13 deletions

View file

@ -676,7 +676,7 @@ public class BigqueryConnection implements AutoCloseable {
bigquery.datasets().get(getProjectId(), datasetName).execute();
return true;
} catch (GoogleJsonResponseException e) {
if (e.getDetails().getCode() == 404) {
if (e.getDetails() != null && e.getDetails().getCode() == 404) {
return false;
}
throw e;
@ -689,7 +689,7 @@ public class BigqueryConnection implements AutoCloseable {
bigquery.tables().get(getProjectId(), datasetName, tableName).execute();
return true;
} catch (GoogleJsonResponseException e) {
if (e.getDetails().getCode() == 404) {
if (e.getDetails() != null && e.getDetails().getCode() == 404) {
return false;
}
throw e;

View file

@ -30,15 +30,20 @@ public final class BigqueryJobFailureException extends RuntimeException {
/** Delegate {@link IOException} errors, checking for {@link GoogleJsonResponseException} */
public static BigqueryJobFailureException create(IOException cause) {
if (cause instanceof GoogleJsonResponseException) {
return create(((GoogleJsonResponseException) cause).getDetails());
return create((GoogleJsonResponseException) cause);
} else {
return new BigqueryJobFailureException(cause.getMessage(), cause, null, null);
}
}
/** Create an error for JSON server response errors. */
public static BigqueryJobFailureException create(GoogleJsonError error) {
return new BigqueryJobFailureException(error.getMessage(), null, null, error);
public static BigqueryJobFailureException create(GoogleJsonResponseException cause) {
GoogleJsonError err = cause.getDetails();
if (err != null) {
return new BigqueryJobFailureException(err.getMessage(), null, null, err);
} else {
return new BigqueryJobFailureException(cause.getMessage(), cause, null, null);
}
}
/** Create an error from a failed job. */

View file

@ -19,7 +19,7 @@ import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static google.registry.model.EppResourceUtils.loadByForeignKey;
import static google.registry.util.DomainNameUtils.getSecondLevelDomain;
import com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.services.dns.Dns;
import com.google.api.services.dns.model.Change;
@ -390,12 +390,12 @@ public class CloudDnsWriter extends BaseDnsWriter {
try {
dnsConnection.changes().create(projectId, zoneName, change).execute();
} catch (GoogleJsonResponseException e) {
List<ErrorInfo> errors = e.getDetails().getErrors();
GoogleJsonError err = e.getDetails();
// We did something really wrong here, just give up and re-throw
if (errors.size() > 1) {
if (err == null || err.getErrors().size() > 1) {
throw new RuntimeException(e);
}
String errorReason = errors.get(0).getReason();
String errorReason = err.getErrors().get(0).getReason();
if (RETRYABLE_EXCEPTION_REASONS.contains(errorReason)) {
throw new ZoneStateException(errorReason);

View file

@ -138,7 +138,7 @@ public class UpdateSnapshotViewAction implements Runnable {
.update(ref.getProjectId(), ref.getDatasetId(), ref.getTableId(), table)
.execute();
} catch (GoogleJsonResponseException e) {
if (e.getDetails().getCode() == 404) {
if (e.getDetails() != null && e.getDetails().getCode() == 404) {
bigquery.tables().insert(ref.getProjectId(), ref.getDatasetId(), table).execute();
} else {
logger.atWarning().withCause(e).log(

View file

@ -95,7 +95,9 @@ public class DirectoryGroupsConnection implements GroupsConnection {
// If the member is already in the group, ignore the error, get the existing member, and
// return it.
GoogleJsonError err = e.getDetails();
if (err.getCode() == SC_NOT_FOUND && err.getMessage().equals(GROUP_NOT_FOUND_MSG)) {
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);
@ -169,7 +171,8 @@ public class DirectoryGroupsConnection implements GroupsConnection {
return createdGroup;
} catch (GoogleJsonResponseException e) {
// Ignore the error thrown if the group already exists.
if (e.getDetails().getCode() == SC_CONFLICT
if (e.getDetails() != null
&& e.getDetails().getCode() == SC_CONFLICT
&& e.getDetails().getMessage().equals("Entity already exists.")) {
logger.atInfo().withCause(e).log(
"Could not create group %s because it already exists.", groupKey);
@ -204,7 +207,8 @@ public class DirectoryGroupsConnection implements GroupsConnection {
"%s is a member of the group %s. Got reply: %s", memberEmail, groupKey, getReply);
return true;
} catch (GoogleJsonResponseException e) {
if (ERROR_MESSAGES_MEMBER_NOT_FOUND.contains(e.getDetails().getMessage())) {
if (e.getDetails() != null
&& ERROR_MESSAGES_MEMBER_NOT_FOUND.contains(e.getDetails().getMessage())) {
// This means the "get" request failed because the email wasn't part of the group.
// This is expected behavior for any visitor that isn't a support group member.
logger.atInfo().log(