|
| 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.spanner.common; |
| 18 | + |
| 19 | +import com.google.cloud.spanner.ErrorCode; |
| 20 | +import com.google.cloud.spanner.SpannerException; |
| 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 | +import java.util.HashMap; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +/** |
| 35 | + * A custom ErrorDetailsProvider for Spanner. |
| 36 | + */ |
| 37 | +public class SpannerErrorDetailsProvider extends GCPErrorDetailsProvider { |
| 38 | + private static final String ERROR_MESSAGE_FORMAT = "Error occurred in the phase: '%s'. Error message: %s"; |
| 39 | + |
| 40 | + static Map<ErrorCode, ErrorUtils.ActionErrorPair> actionErrorMap = new HashMap<>(); |
| 41 | + |
| 42 | + static { |
| 43 | + actionErrorMap.put(ErrorCode.CANCELLED, ErrorUtils.getActionErrorByStatusCode(499)); |
| 44 | + actionErrorMap.put(ErrorCode.UNKNOWN, ErrorUtils.getActionErrorByStatusCode(500)); |
| 45 | + actionErrorMap.put(ErrorCode.INVALID_ARGUMENT, ErrorUtils.getActionErrorByStatusCode(400)); |
| 46 | + actionErrorMap.put(ErrorCode.DEADLINE_EXCEEDED, ErrorUtils.getActionErrorByStatusCode(504)); |
| 47 | + actionErrorMap.put(ErrorCode.NOT_FOUND, ErrorUtils.getActionErrorByStatusCode(404)); |
| 48 | + actionErrorMap.put(ErrorCode.ALREADY_EXISTS, ErrorUtils.getActionErrorByStatusCode(409)); |
| 49 | + actionErrorMap.put(ErrorCode.PERMISSION_DENIED, ErrorUtils.getActionErrorByStatusCode(403)); |
| 50 | + actionErrorMap.put(ErrorCode.UNAUTHENTICATED, ErrorUtils.getActionErrorByStatusCode(401)); |
| 51 | + actionErrorMap.put(ErrorCode.RESOURCE_EXHAUSTED, ErrorUtils.getActionErrorByStatusCode(429)); |
| 52 | + actionErrorMap.put(ErrorCode.FAILED_PRECONDITION, ErrorUtils.getActionErrorByStatusCode(400)); |
| 53 | + actionErrorMap.put(ErrorCode.ABORTED, ErrorUtils.getActionErrorByStatusCode(409)); |
| 54 | + actionErrorMap.put(ErrorCode.OUT_OF_RANGE, ErrorUtils.getActionErrorByStatusCode(400)); |
| 55 | + actionErrorMap.put(ErrorCode.UNIMPLEMENTED, ErrorUtils.getActionErrorByStatusCode(501)); |
| 56 | + actionErrorMap.put(ErrorCode.INTERNAL, ErrorUtils.getActionErrorByStatusCode(500)); |
| 57 | + actionErrorMap.put(ErrorCode.UNAVAILABLE, ErrorUtils.getActionErrorByStatusCode(503)); |
| 58 | + actionErrorMap.put(ErrorCode.DATA_LOSS, ErrorUtils.getActionErrorByStatusCode(500)); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected String getExternalDocumentationLink() { |
| 63 | + return GCPUtils.SPANNER_SUPPORTED_DOC_URL; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public ProgramFailureException getExceptionDetails(Exception e, ErrorContext errorContext) { |
| 68 | + ProgramFailureException ex = super.getExceptionDetails(e, errorContext); |
| 69 | + if (ex != null) { |
| 70 | + return ex; |
| 71 | + } |
| 72 | + List<Throwable> causalChain = Throwables.getCausalChain(e); |
| 73 | + for (Throwable t : causalChain) { |
| 74 | + if (t instanceof SpannerException) { |
| 75 | + return getProgramFailureExceptionFromSpannerException((SpannerException) t); |
| 76 | + } |
| 77 | + } |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + private ProgramFailureException getProgramFailureExceptionFromSpannerException(SpannerException se) { |
| 82 | + String errorCodeName = se.getErrorCode().name(); |
| 83 | + ErrorUtils.ActionErrorPair actionErrorPair = null; |
| 84 | + String errorReason = se.getReason(); |
| 85 | + String errorMessage = se.getMessage(); |
| 86 | + if (actionErrorMap.containsKey(se.getErrorCode())) { |
| 87 | + actionErrorPair = actionErrorMap.get(se.getErrorCode()); |
| 88 | + errorReason = String.format("%s %s. %s", errorCodeName, errorMessage, actionErrorPair.getCorrectiveAction()); |
| 89 | + } |
| 90 | + if (!errorReason.endsWith(".")) { |
| 91 | + errorReason = errorReason + "."; |
| 92 | + } |
| 93 | + errorReason = String.format("%s For more details, see %s.", errorReason, GCPUtils.SPANNER_SUPPORTED_DOC_URL); |
| 94 | + |
| 95 | + String errorMessageWithCode = String.format("[ErrorCode='%s'] %s", errorCodeName, errorMessage); |
| 96 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), |
| 97 | + errorReason, String.format("%s: %s", se.getClass().getName(), errorMessageWithCode), |
| 98 | + actionErrorPair != null ? actionErrorPair.getErrorType() : ErrorType.UNKNOWN, true, ErrorCodeType.HTTP, |
| 99 | + errorCodeName, GCPUtils.SPANNER_SUPPORTED_DOC_URL, se); |
| 100 | + } |
| 101 | +} |
0 commit comments