Skip to content
Permalink
Browse files
fix: fix gRPC code conversion (#1555)
  • Loading branch information
chanseokoh committed Nov 18, 2021
1 parent 02436d2 commit 09b99d591497b44c3c25b1a54abb0f1cb69d7376
@@ -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.
@@ -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;

@@ -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;
@@ -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:
@@ -155,7 +156,7 @@ static StatusCode.Code httpStatusToStatusCode(int httpStatus, String errorMessag
}

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

@@ -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;
}

0 comments on commit 09b99d5

Please sign in to comment.