Skip to content

Commit 3f7626d

Browse files
committed
Merge pull request #570 from mziccard/add-info-to-exception
Add location() and debugInfo() to BaseServiceException
2 parents 1a18033 + 55ee5aa commit 3f7626d

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,11 @@ public BigQueryException(int code, String message, BigQueryError error) {
5555

5656
public BigQueryException(IOException exception) {
5757
super(exception, true);
58-
BigQueryError bigqueryError = null;
59-
if (exception instanceof GoogleJsonResponseException) {
60-
GoogleJsonError error = ((GoogleJsonResponseException) exception).getDetails();
61-
if (error != null && error.getErrors() != null && !error.getErrors().isEmpty()) {
62-
GoogleJsonError.ErrorInfo errorInfo = error.getErrors().get(0);
63-
bigqueryError = new BigQueryError(errorInfo.getReason(), errorInfo.getLocation(),
64-
errorInfo.getMessage(), (String) error.get("debugInfo"));
65-
}
58+
BigQueryError error = null;
59+
if (reason() != null) {
60+
error = new BigQueryError(reason(), location(), getMessage(), debugInfo());
6661
}
67-
this.error = bigqueryError;
62+
this.error = error;
6863
}
6964

7065
/**

gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,32 @@ public int hashCode() {
8686
private final boolean retryable;
8787
private final String reason;
8888
private final boolean idempotent;
89+
private final String location;
90+
private final String debugInfo;
8991

9092
public BaseServiceException(IOException exception, boolean idempotent) {
9193
super(message(exception), exception);
94+
int code = UNKNOWN_CODE;
95+
String reason = null;
96+
String location = null;
97+
String debugInfo = null;
9298
if (exception instanceof GoogleJsonResponseException) {
93-
Error error = error(((GoogleJsonResponseException) exception).getDetails());
94-
this.code = error.code;
95-
this.reason = error.reason;
96-
this.retryable = error.isRetryable(retryableErrors());
97-
} else {
98-
this.code = UNKNOWN_CODE;
99-
this.reason = null;
100-
this.retryable = idempotent && isRetryable(exception);
99+
GoogleJsonError jsonError = ((GoogleJsonResponseException) exception).getDetails();
100+
Error error = error(jsonError);
101+
code = error.code;
102+
reason = error.reason;
103+
if (reason != null) {
104+
GoogleJsonError.ErrorInfo errorInfo = jsonError.getErrors().get(0);
105+
location = errorInfo.getLocation();
106+
debugInfo = (String) errorInfo.get("debugInfo");
107+
}
101108
}
109+
this.code = code;
110+
this.retryable = idempotent && isRetryable(exception);
111+
this.reason = reason;
102112
this.idempotent = idempotent;
113+
this.location = location;
114+
this.debugInfo = debugInfo;
103115
}
104116

105117
public BaseServiceException(GoogleJsonError error, boolean idempotent) {
@@ -108,6 +120,8 @@ public BaseServiceException(GoogleJsonError error, boolean idempotent) {
108120
this.reason = reason(error);
109121
this.idempotent = idempotent;
110122
this.retryable = idempotent && isRetryable(error);
123+
this.location = null;
124+
this.debugInfo = null;
111125
}
112126

113127
public BaseServiceException(int code, String message, String reason, boolean idempotent) {
@@ -121,6 +135,8 @@ public BaseServiceException(int code, String message, String reason, boolean ide
121135
this.reason = reason;
122136
this.idempotent = idempotent;
123137
this.retryable = idempotent && new Error(code, reason).isRetryable(retryableErrors());
138+
this.location = null;
139+
this.debugInfo = null;
124140
}
125141

126142
protected Set<Error> retryableErrors() {
@@ -166,6 +182,18 @@ public boolean idempotent() {
166182
return idempotent;
167183
}
168184

185+
/**
186+
* Returns the service location where the error causing the exception occurred. Returns
187+
* {@code null} if not set.
188+
*/
189+
public String location() {
190+
return location;
191+
}
192+
193+
protected String debugInfo() {
194+
return debugInfo;
195+
}
196+
169197
protected static String reason(GoogleJsonError error) {
170198
if (error.getErrors() != null && !error.getErrors().isEmpty()) {
171199
return error.getErrors().get(0).getReason();

0 commit comments

Comments
 (0)