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