|
| 1 | +/* |
| 2 | + * Copyright © 2025 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.cdap.plugin.gcp.bigtable.common; |
| 18 | + |
| 19 | +import com.google.bigtable.repackaged.io.grpc.Status; |
| 20 | +import com.google.bigtable.repackaged.io.grpc.StatusRuntimeException; |
| 21 | +import com.google.common.base.Throwables; |
| 22 | +import io.cdap.cdap.api.exception.ErrorCategory; |
| 23 | +import io.cdap.cdap.api.exception.ErrorCodeType; |
| 24 | +import io.cdap.cdap.api.exception.ErrorType; |
| 25 | +import io.cdap.cdap.api.exception.ErrorUtils; |
| 26 | +import io.cdap.cdap.api.exception.ProgramFailureException; |
| 27 | +import io.cdap.cdap.etl.api.exception.ErrorContext; |
| 28 | +import io.cdap.plugin.gcp.common.GCPErrorDetailsProvider; |
| 29 | +import io.cdap.plugin.gcp.common.GCPUtils; |
| 30 | + |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +/** |
| 36 | + * A custom ErrorDetailsProvider for BigTable plugins. |
| 37 | + */ |
| 38 | +public class BigtableErrorDetailsProvider extends GCPErrorDetailsProvider { |
| 39 | + private static final String ERROR_MESSAGE_FORMAT = "Error occurred in the phase: '%s'. Error message: %s"; |
| 40 | + |
| 41 | + static Map<Status.Code, Integer> actionErrorMap = new HashMap<>(); |
| 42 | + |
| 43 | + static { |
| 44 | + actionErrorMap.put(Status.Code.CANCELLED, 499); |
| 45 | + actionErrorMap.put(Status.Code.UNKNOWN, 500); |
| 46 | + actionErrorMap.put(Status.Code.INVALID_ARGUMENT, 400); |
| 47 | + actionErrorMap.put(Status.Code.DEADLINE_EXCEEDED, 504); |
| 48 | + actionErrorMap.put(Status.Code.NOT_FOUND, 404); |
| 49 | + actionErrorMap.put(Status.Code.ALREADY_EXISTS, 409); |
| 50 | + actionErrorMap.put(Status.Code.PERMISSION_DENIED, 403); |
| 51 | + actionErrorMap.put(Status.Code.UNAUTHENTICATED, 401); |
| 52 | + actionErrorMap.put(Status.Code.RESOURCE_EXHAUSTED, 429); |
| 53 | + actionErrorMap.put(Status.Code.FAILED_PRECONDITION, 400); |
| 54 | + actionErrorMap.put(Status.Code.ABORTED, 409); |
| 55 | + actionErrorMap.put(Status.Code.OUT_OF_RANGE, 400); |
| 56 | + actionErrorMap.put(Status.Code.UNIMPLEMENTED, 501); |
| 57 | + actionErrorMap.put(Status.Code.INTERNAL, 500); |
| 58 | + actionErrorMap.put(Status.Code.UNAVAILABLE, 503); |
| 59 | + actionErrorMap.put(Status.Code.DATA_LOSS, 500); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected String getExternalDocumentationLink() { |
| 64 | + return GCPUtils.BIG_TABLE_SUPPORTED_DOC_URL; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public ProgramFailureException getExceptionDetails(Exception e, ErrorContext errorContext) { |
| 69 | + ProgramFailureException ex = super.getExceptionDetails(e, errorContext); |
| 70 | + if (ex != null) { |
| 71 | + return ex; |
| 72 | + } |
| 73 | + List<Throwable> causalChain = Throwables.getCausalChain(e); |
| 74 | + for (Throwable t : causalChain) { |
| 75 | + if (t instanceof StatusRuntimeException) { |
| 76 | + return getProgramFailureExceptionFromBigTableException((StatusRuntimeException) t); |
| 77 | + } |
| 78 | + } |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + private ProgramFailureException getProgramFailureExceptionFromBigTableException(StatusRuntimeException se) { |
| 83 | + int httpStatusCode = actionErrorMap.get(se.getStatus().getCode()); |
| 84 | + ErrorUtils.ActionErrorPair actionErrorPair = null; |
| 85 | + String errorReason = se.getMessage(); |
| 86 | + String errorMessage = se.getMessage(); |
| 87 | + if (actionErrorMap.containsKey(se.getStatus().getCode())) { |
| 88 | + actionErrorPair = ErrorUtils.getActionErrorByStatusCode(httpStatusCode); |
| 89 | + errorReason = String.format("%s %s. %s", httpStatusCode, errorMessage, actionErrorPair.getCorrectiveAction()); |
| 90 | + } |
| 91 | + if (!errorReason.endsWith(".")) { |
| 92 | + errorReason = errorReason + "."; |
| 93 | + } |
| 94 | + errorReason = String.format("%s For more details, see %s.", errorReason, GCPUtils.BIG_TABLE_SUPPORTED_DOC_URL); |
| 95 | + |
| 96 | + String errorMessageWithCode = String.format("[ErrorCode='%s'] %s", httpStatusCode, errorMessage); |
| 97 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), |
| 98 | + errorReason, String.format("%s: %s", se.getClass().getName(), errorMessageWithCode), |
| 99 | + actionErrorPair != null ? actionErrorPair.getErrorType() : ErrorType.UNKNOWN, true, ErrorCodeType.HTTP, |
| 100 | + String.valueOf(httpStatusCode), GCPUtils.SPANNER_SUPPORTED_DOC_URL, se); |
| 101 | + } |
| 102 | +} |
0 commit comments