Skip to content

Commit

Permalink
Merge pull request #570 from mziccard/add-info-to-exception
Browse files Browse the repository at this point in the history
Add location() and debugInfo() to BaseServiceException
  • Loading branch information
aozarov committed Jan 22, 2016
2 parents 1a18033 + 55ee5aa commit 3f7626d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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<Error> retryableErrors() {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3f7626d

Please sign in to comment.