|
22 | 22 | import com.google.api.gax.rpc.ApiException; |
23 | 23 | import com.google.common.base.Strings; |
24 | 24 | import com.google.common.base.Throwables; |
| 25 | +import com.google.common.collect.ImmutableMap; |
25 | 26 | import io.cdap.cdap.api.exception.ErrorCategory; |
26 | 27 | import io.cdap.cdap.api.exception.ErrorCodeType; |
27 | 28 | import io.cdap.cdap.api.exception.ErrorType; |
|
30 | 31 | import io.cdap.cdap.etl.api.exception.ErrorContext; |
31 | 32 |
|
32 | 33 | import java.io.IOException; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.HashMap; |
33 | 36 | import java.util.List; |
| 37 | +import java.util.Map; |
34 | 38 | import javax.annotation.Nullable; |
35 | 39 |
|
36 | 40 | /** |
37 | 41 | * Common functions for GCP error details provider related functionalities. |
38 | 42 | */ |
39 | 43 | public final class GCPErrorDetailsProviderUtil { |
40 | 44 |
|
| 45 | + // https://github.com/grpc/grpc/blob/master/doc/statuscodes.md |
| 46 | + public static final Map<Integer, Integer> GCP_GRPC_ERROR_CODE_HTTP_STATUS_CODE_MAP = Collections.unmodifiableMap( |
| 47 | + new HashMap<Integer, Integer>() {{ |
| 48 | + put(3, 400); // INVALID_ARGUMENT <--> HTTP 400 (Bad Request) |
| 49 | + put(4, 504); // DEADLINE_EXCEEDED <--> HTTP 504 (Gateway Timeout) |
| 50 | + put(5, 404); // NOT_FOUND <--> HTTP 404 (Not Found) |
| 51 | + put(6, 409); // ALREADY_EXISTS <--> HTTP 409 (Conflict) |
| 52 | + put(7, 403); // PERMISSION_DENIED <--> HTTP 403 (Forbidden) |
| 53 | + put(8, 429); // RESOURCE_EXHAUSTED <--> HTTP 429 (Too Many Requests) |
| 54 | + put(9, 400); // FAILED_PRECONDITION <--> HTTP 400 (Bad Request) |
| 55 | + put(10, 409); // ABORTED <--> HTTP 409 (Conflict) |
| 56 | + put(11, 400); // OUT_OF_RANGE <--> HTTP 400 (Bad Request) |
| 57 | + put(12, 501); // UNIMPLEMENTED <--> HTTP 501 (Not Implemented) |
| 58 | + put(13, 500); // INTERNAL <--> HTTP 500 (Internal Server Error) |
| 59 | + put(14, 503); // UNAVAILABLE <--> HTTP 503 (Service Unavailable) |
| 60 | + put(15, 500); // DATA_LOSS <--> HTTP 500 (Internal Server Error) |
| 61 | + put(16, 401); // UNAUTHENTICATED <--> HTTP 401 (Unauthorized) |
| 62 | + }}); |
| 63 | + |
41 | 64 | /** |
42 | 65 | * Get a ProgramFailureException with the given error |
43 | 66 | * information from {@link HttpResponseException}. |
@@ -130,4 +153,39 @@ private static String getErrorMessage(GoogleJsonResponseException exception) { |
130 | 153 | } |
131 | 154 | return exception.getMessage(); |
132 | 155 | } |
| 156 | + |
| 157 | + |
| 158 | + /** |
| 159 | + * Get the HTTP status code for a given gRPC error code. |
| 160 | + * |
| 161 | + * @param grpcStatusCode the int value of the gRPC error code |
| 162 | + */ |
| 163 | + public static ErrorUtils.ActionErrorPair getActionErrorByGrpcStatusCode(int grpcStatusCode) { |
| 164 | + if (!GCP_GRPC_ERROR_CODE_HTTP_STATUS_CODE_MAP.containsKey(grpcStatusCode)) { |
| 165 | + return null; |
| 166 | + } |
| 167 | + return ErrorUtils.getActionErrorByStatusCode(GCP_GRPC_ERROR_CODE_HTTP_STATUS_CODE_MAP.get(grpcStatusCode)); |
| 168 | + } |
| 169 | + |
| 170 | + public static ProgramFailureException getProgramFailureExceptionByGrpcStatusCode(int grpcErrorCodeValue, |
| 171 | + String grpcErrorReason, String grpcErrorMessage, String supportedDocUrl, Exception se) { |
| 172 | + int httpStatusCode = GCPErrorDetailsProviderUtil.GCP_GRPC_ERROR_CODE_HTTP_STATUS_CODE_MAP. |
| 173 | + getOrDefault(grpcErrorCodeValue, 500); |
| 174 | + ErrorUtils.ActionErrorPair actionErrorPair = GCPErrorDetailsProviderUtil.getActionErrorByGrpcStatusCode( |
| 175 | + grpcErrorCodeValue); |
| 176 | + String errorReason = grpcErrorReason; |
| 177 | + if (actionErrorPair != null) { |
| 178 | + errorReason = String.format("%s %s. %s", httpStatusCode, grpcErrorMessage, actionErrorPair.getCorrectiveAction()); |
| 179 | + } |
| 180 | + if (!errorReason.endsWith(".")) { |
| 181 | + errorReason = errorReason + "."; |
| 182 | + } |
| 183 | + errorReason = String.format("%s For more details, see %s.", errorReason, supportedDocUrl); |
| 184 | + |
| 185 | + String errorMessageWithCode = String.format("[ErrorCode='%s'] %s", httpStatusCode, grpcErrorMessage); |
| 186 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), errorReason, |
| 187 | + String.format("%s: %s", se.getClass().getName(), errorMessageWithCode), |
| 188 | + actionErrorPair != null ? actionErrorPair.getErrorType() : ErrorType.UNKNOWN, true, ErrorCodeType.HTTP, |
| 189 | + String.valueOf(httpStatusCode), supportedDocUrl, se); |
| 190 | + } |
133 | 191 | } |
0 commit comments