diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java index d07843583bee..c42bbd96db66 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java @@ -55,16 +55,11 @@ public BigQueryException(int code, String message, BigQueryError error) { public BigQueryException(IOException exception) { super(exception, true); - BigQueryError bigqueryError = null; - if (exception instanceof GoogleJsonResponseException) { - GoogleJsonError error = ((GoogleJsonResponseException) exception).getDetails(); - if (error != null && error.getErrors() != null && !error.getErrors().isEmpty()) { - GoogleJsonError.ErrorInfo errorInfo = error.getErrors().get(0); - bigqueryError = new BigQueryError(errorInfo.getReason(), errorInfo.getLocation(), - errorInfo.getMessage(), (String) error.get("debugInfo")); - } + BigQueryError error = null; + if (reason() != null) { + error = new BigQueryError(reason(), location(), getMessage(), debugInfo()); } - this.error = bigqueryError; + this.error = error; } /** diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java index 0222a0d2258c..579340f1256e 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java @@ -86,20 +86,32 @@ public int hashCode() { private final boolean retryable; private final String reason; private final boolean idempotent; + private final String location; + private final String debugInfo; public BaseServiceException(IOException exception, boolean idempotent) { super(message(exception), exception); + int code = UNKNOWN_CODE; + String reason = null; + String location = null; + String debugInfo = null; if (exception instanceof GoogleJsonResponseException) { - Error error = error(((GoogleJsonResponseException) exception).getDetails()); - this.code = error.code; - this.reason = error.reason; - this.retryable = error.isRetryable(retryableErrors()); - } else { - this.code = UNKNOWN_CODE; - this.reason = null; - this.retryable = idempotent && isRetryable(exception); + GoogleJsonError jsonError = ((GoogleJsonResponseException) exception).getDetails(); + Error error = error(jsonError); + code = error.code; + reason = error.reason; + if (reason != null) { + GoogleJsonError.ErrorInfo errorInfo = jsonError.getErrors().get(0); + location = errorInfo.getLocation(); + debugInfo = (String) errorInfo.get("debugInfo"); + } } + this.code = code; + this.retryable = idempotent && isRetryable(exception); + this.reason = reason; this.idempotent = idempotent; + this.location = location; + this.debugInfo = debugInfo; } public BaseServiceException(GoogleJsonError error, boolean idempotent) { @@ -108,6 +120,8 @@ public BaseServiceException(GoogleJsonError error, boolean idempotent) { this.reason = reason(error); this.idempotent = idempotent; this.retryable = idempotent && isRetryable(error); + this.location = null; + this.debugInfo = null; } public BaseServiceException(int code, String message, String reason, boolean idempotent) { @@ -121,6 +135,8 @@ public BaseServiceException(int code, String message, String reason, boolean ide this.reason = reason; this.idempotent = idempotent; this.retryable = idempotent && new Error(code, reason).isRetryable(retryableErrors()); + this.location = null; + this.debugInfo = null; } protected Set retryableErrors() { @@ -166,6 +182,18 @@ public boolean idempotent() { return idempotent; } + /** + * Returns the service location where the error causing the exception occurred. Returns + * {@code null} if not set. + */ + public String location() { + return location; + } + + protected String debugInfo() { + return debugInfo; + } + protected static String reason(GoogleJsonError error) { if (error.getErrors() != null && !error.getErrors().isEmpty()) { return error.getErrors().get(0).getReason();