|
| 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.aws.s3.common; |
| 18 | + |
| 19 | +import com.amazonaws.services.s3.model.AmazonS3Exception; |
| 20 | +import com.google.common.base.Throwables; |
| 21 | +import io.cdap.cdap.api.exception.ErrorCategory; |
| 22 | +import io.cdap.cdap.api.exception.ErrorCodeType; |
| 23 | +import io.cdap.cdap.api.exception.ErrorType; |
| 24 | +import io.cdap.cdap.api.exception.ErrorUtils; |
| 25 | +import io.cdap.cdap.api.exception.ProgramFailureException; |
| 26 | +import io.cdap.cdap.etl.api.exception.ErrorContext; |
| 27 | +import io.cdap.cdap.etl.api.exception.ErrorDetailsProvider; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +/** |
| 31 | + * Error details provided for the Amazon S3 |
| 32 | + **/ |
| 33 | +public class AmazonErrorDetailsProvider implements ErrorDetailsProvider { |
| 34 | + |
| 35 | + static final String S3_EXTERNAL_DOC = "https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html"; |
| 36 | + |
| 37 | + @Override |
| 38 | + public ProgramFailureException getExceptionDetails(Exception e, ErrorContext errorContext) { |
| 39 | + List<Throwable> causalChain = Throwables.getCausalChain(e); |
| 40 | + for (Throwable t : causalChain) { |
| 41 | + if (t instanceof ProgramFailureException) { |
| 42 | + // if causal chain already has program failure exception, return null to avoid double wrap. |
| 43 | + return null; |
| 44 | + } |
| 45 | + if (t instanceof AmazonS3Exception) { |
| 46 | + AmazonS3Exception s3Exception = (AmazonS3Exception) t; |
| 47 | + String errorMessage = s3Exception.getErrorMessage(); |
| 48 | + String errorMessageWithDetails = s3Exception.getMessage(); |
| 49 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), |
| 50 | + errorMessage, errorMessageWithDetails, ErrorType.USER, true, ErrorCodeType.HTTP, s3Exception.getErrorCode(), |
| 51 | + S3_EXTERNAL_DOC, t); |
| 52 | + } |
| 53 | + } |
| 54 | + return null; |
| 55 | + } |
| 56 | +} |
0 commit comments