Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

fix: fix gRPC code conversion #1555

Merged
merged 3 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,13 @@ public void onSuccess(ResponseT r) {
public void onFailure(Throwable throwable) {
if (throwable instanceof HttpResponseException) {
HttpResponseException e = (HttpResponseException) throwable;
StatusCode.Code statusCode =
HttpJsonStatusCode.httpStatusToStatusCode(e.getStatusCode(), e.getMessage());
boolean canRetry = retryableCodes.contains(statusCode);
StatusCode statusCode = HttpJsonStatusCode.of(e.getStatusCode(), e.getMessage());
boolean canRetry = retryableCodes.contains(statusCode.getCode());
String message = e.getStatusMessage();
ApiException newException =
message == null
? ApiExceptionFactory.createException(
throwable, HttpJsonStatusCode.of(statusCode), canRetry)
: ApiExceptionFactory.createException(
message, throwable, HttpJsonStatusCode.of(statusCode), canRetry);
? ApiExceptionFactory.createException(throwable, statusCode, canRetry)
: ApiExceptionFactory.createException(message, throwable, statusCode, canRetry);
super.setException(newException);
} else if (throwable instanceof CancellationException && cancelled) {
// this just circled around, so ignore.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.api.core.BetaApi;
import com.google.api.core.InternalExtensionOnly;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.common.base.Strings;
import java.util.Objects;

Expand All @@ -46,22 +47,22 @@ public class HttpJsonStatusCode implements StatusCode {
static final String UNKNOWN = "UNKNOWN";

private final int httpStatus;
private final StatusCode.Code statusCode;
private final Code statusCode;

/** Creates a new instance with the given status code. */
public static HttpJsonStatusCode of(int httpStatus, String errorMessage) {
return new HttpJsonStatusCode(httpStatus, httpStatusToStatusCode(httpStatus, errorMessage));
}

public static HttpJsonStatusCode of(StatusCode.Code statusCode) {
public static HttpJsonStatusCode of(Code statusCode) {
return new HttpJsonStatusCode(statusCode.getHttpStatusCode(), statusCode);
}

public static HttpJsonStatusCode of(com.google.rpc.Code rpcCode) {
return new HttpJsonStatusCode(rpcCode.getNumber(), rpcCodeToStatusCode(rpcCode));
return HttpJsonStatusCode.of(rpcCodeToStatusCode(rpcCode));
}

static StatusCode.Code rpcCodeToStatusCode(com.google.rpc.Code rpcCode) {
static Code rpcCodeToStatusCode(com.google.rpc.Code rpcCode) {
switch (rpcCode) {
case OK:
return Code.OK;
Expand Down Expand Up @@ -102,7 +103,7 @@ static StatusCode.Code rpcCodeToStatusCode(com.google.rpc.Code rpcCode) {
}
}

static StatusCode.Code httpStatusToStatusCode(int httpStatus, String errorMessage) {
static Code httpStatusToStatusCode(int httpStatus, String errorMessage) {
String causeMessage = Strings.nullToEmpty(errorMessage).toUpperCase();
switch (httpStatus) {
case 200:
Expand Down Expand Up @@ -155,7 +156,7 @@ static StatusCode.Code httpStatusToStatusCode(int httpStatus, String errorMessag
}

@Override
public StatusCode.Code getCode() {
public Code getCode() {
return statusCode;
}

Expand All @@ -165,8 +166,8 @@ public Integer getTransportCode() {
return httpStatus;
}

private HttpJsonStatusCode(int code, StatusCode.Code statusCode) {
this.httpStatus = code;
private HttpJsonStatusCode(int httpStatus, Code statusCode) {
this.httpStatus = httpStatus;
this.statusCode = statusCode;
}

Expand Down