|
| 1 | +/* |
| 2 | + * Copyright © 2024 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.snowflake.common; |
| 18 | + |
| 19 | +import com.google.common.base.Throwables; |
| 20 | +import io.cdap.cdap.api.data.format.UnexpectedFormatException; |
| 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 io.cdap.plugin.snowflake.common.exception.ConnectionTimeoutException; |
| 29 | +import io.cdap.plugin.snowflake.common.exception.SchemaParseException; |
| 30 | + |
| 31 | +import java.net.URISyntaxException; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * Error details provided for the Snowflake |
| 37 | + **/ |
| 38 | +public class SnowflakeErrorDetailsProvider implements ErrorDetailsProvider { |
| 39 | + |
| 40 | + @Override |
| 41 | + public ProgramFailureException getExceptionDetails(Exception e, ErrorContext errorContext) { |
| 42 | + List<Throwable> causalChain = Throwables.getCausalChain(e); |
| 43 | + for (Throwable t : causalChain) { |
| 44 | + if (t instanceof ProgramFailureException) { |
| 45 | + // if causal chain already has program failure exception, return null to avoid double wrap. |
| 46 | + return null; |
| 47 | + } |
| 48 | + if (t instanceof IllegalArgumentException) { |
| 49 | + return getProgramFailureException((IllegalArgumentException) t, errorContext); |
| 50 | + } |
| 51 | + if (t instanceof IllegalStateException) { |
| 52 | + return getProgramFailureException((IllegalStateException) t, errorContext); |
| 53 | + } |
| 54 | + if (t instanceof URISyntaxException) { |
| 55 | + return getProgramFailureException((URISyntaxException) t, errorContext); |
| 56 | + } |
| 57 | + if (t instanceof SchemaParseException) { |
| 58 | + return getProgramFailureException((SchemaParseException) t, errorContext); |
| 59 | + } |
| 60 | + if (t instanceof UnexpectedFormatException) { |
| 61 | + return getProgramFailureException((UnexpectedFormatException) t, errorContext); |
| 62 | + } |
| 63 | + if (t instanceof ConnectionTimeoutException) { |
| 64 | + return getProgramFailureException((ConnectionTimeoutException) t, errorContext); |
| 65 | + } |
| 66 | + } |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Get a ProgramFailureException with the given error |
| 72 | + * information from {@link IllegalArgumentException}. |
| 73 | + * |
| 74 | + * @param e The IllegalArgumentException to get the error information from. |
| 75 | + * @return A ProgramFailureException with the given error information. |
| 76 | + */ |
| 77 | + private ProgramFailureException getProgramFailureException(IllegalArgumentException e, ErrorContext errorContext) { |
| 78 | + String errorMessage = e.getMessage(); |
| 79 | + String errorMessageFormat = "Error occurred in the phase: '%s'. Error message: %s"; |
| 80 | + |
| 81 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), |
| 82 | + errorMessage, |
| 83 | + String.format(errorMessageFormat, errorContext.getPhase(), errorMessage), ErrorType.USER, false, e); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Get a ProgramFailureException with the given error |
| 88 | + * information from {@link IllegalStateException}. |
| 89 | + * |
| 90 | + * @param e The IllegalStateException to get the error information from. |
| 91 | + * @return A ProgramFailureException with the given error information. |
| 92 | + */ |
| 93 | + private ProgramFailureException getProgramFailureException(IllegalStateException e, ErrorContext errorContext) { |
| 94 | + String errorMessage = e.getMessage(); |
| 95 | + String errorMessageFormat = "Error occurred in the phase: '%s'. Error message: %s"; |
| 96 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), |
| 97 | + errorMessage, |
| 98 | + String.format(errorMessageFormat, errorContext.getPhase(), errorMessage), ErrorType.SYSTEM, false, e); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get a ProgramFailureException with the given error |
| 103 | + * information from {@link URISyntaxException}. |
| 104 | + * |
| 105 | + * @param e The URISyntaxException to get the error information from. |
| 106 | + * @return A ProgramFailureException with the given error information. |
| 107 | + */ |
| 108 | + private ProgramFailureException getProgramFailureException(URISyntaxException e, |
| 109 | + ErrorContext errorContext) { |
| 110 | + String errorMessage = e.getMessage(); |
| 111 | + String errorMessageFormat = "Error occurred in the phase: '%s'. Error message: %s"; |
| 112 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), |
| 113 | + errorMessage, |
| 114 | + String.format(errorMessageFormat, errorContext.getPhase(), errorMessage), ErrorType.USER, false, e); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Get a ProgramFailureException with the given error |
| 119 | + * information from {@link SchemaParseException}. |
| 120 | + * |
| 121 | + * @param e The SchemaParseException to get the error information from. |
| 122 | + * @return A ProgramFailureException with the given error information. |
| 123 | + */ |
| 124 | + private ProgramFailureException getProgramFailureException(SchemaParseException e, ErrorContext errorContext) { |
| 125 | + String errorMessage = e.getMessage(); |
| 126 | + String errorMessageFormat = "Error occurred in the phase: '%s'. Error message: %s"; |
| 127 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), |
| 128 | + errorMessage, |
| 129 | + String.format(errorMessageFormat, errorContext.getPhase(), errorMessage), ErrorType.USER, false, e); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Get a ProgramFailureException with the given error |
| 134 | + * information from {@link UnexpectedFormatException}. |
| 135 | + * |
| 136 | + * @param e The UnexpectedFormatException to get the error information from. |
| 137 | + * @return A ProgramFailureException with the given error information. |
| 138 | + */ |
| 139 | + private ProgramFailureException getProgramFailureException(UnexpectedFormatException e, ErrorContext errorContext) { |
| 140 | + String errorMessage = e.getMessage(); |
| 141 | + String errorMessageFormat = "Error occurred in the phase: '%s'. Error message: %s"; |
| 142 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), |
| 143 | + errorMessage, |
| 144 | + String.format(errorMessageFormat, errorContext.getPhase(), errorMessage), ErrorType.USER, false, e); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Get a ProgramFailureException with the given error |
| 149 | + * information from {@link ConnectionTimeoutException}. |
| 150 | + * |
| 151 | + * @param e The ConnectionTimeoutException to get the error information from. |
| 152 | + * @return A ProgramFailureException with the given error information. |
| 153 | + */ |
| 154 | + private ProgramFailureException getProgramFailureException(ConnectionTimeoutException e, ErrorContext errorContext) { |
| 155 | + String errorMessage = e.getMessage(); |
| 156 | + String errorMessageFormat = "Error occurred in the phase: '%s'. Error message: %s"; |
| 157 | + return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), |
| 158 | + errorMessage, |
| 159 | + String.format(errorMessageFormat, errorContext.getPhase(), errorMessage), ErrorType.SYSTEM, false, e); |
| 160 | + } |
| 161 | +} |
0 commit comments