-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PLUGIN-1826] Error Management HTTP Source and Sink and fix sonar issues #180
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright © 2024 Cask Data, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package io.cdap.plugin.http.common; | ||
|
||
import com.google.common.base.Throwables; | ||
import io.cdap.cdap.api.exception.ErrorCategory; | ||
import io.cdap.cdap.api.exception.ErrorType; | ||
import io.cdap.cdap.api.exception.ErrorUtils; | ||
import io.cdap.cdap.api.exception.ProgramFailureException; | ||
import io.cdap.cdap.etl.api.exception.ErrorContext; | ||
import io.cdap.cdap.etl.api.exception.ErrorDetailsProvider; | ||
import io.cdap.cdap.etl.api.validation.InvalidConfigPropertyException; | ||
|
||
import java.util.List; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Error details provided for the HTTP | ||
**/ | ||
public class HttpErrorDetailsProvider implements ErrorDetailsProvider { | ||
@Override | ||
public ProgramFailureException getExceptionDetails(Exception e, ErrorContext errorContext) { | ||
List<Throwable> causalChain = Throwables.getCausalChain(e); | ||
for (Throwable t : causalChain) { | ||
if (t instanceof ProgramFailureException) { | ||
// if causal chain already has program failure exception, return null to avoid double wrap. | ||
return null; | ||
} | ||
if (t instanceof IllegalArgumentException) { | ||
return getProgramFailureException((IllegalArgumentException) t, errorContext); | ||
} | ||
if (t instanceof IllegalStateException) { | ||
return getProgramFailureException((IllegalStateException) t, errorContext); | ||
} | ||
if (t instanceof InvalidConfigPropertyException) { | ||
return getProgramFailureException((InvalidConfigPropertyException) t, errorContext); | ||
} | ||
if (t instanceof NoSuchElementException) { | ||
return getProgramFailureException((NoSuchElementException) t, errorContext); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Get a ProgramFailureException with the given error | ||
* information from {@link IllegalArgumentException}. | ||
* | ||
* @param e The IllegalArgumentException to get the error information from. | ||
* @return A ProgramFailureException with the given error information. | ||
*/ | ||
private ProgramFailureException getProgramFailureException(IllegalArgumentException e, ErrorContext errorContext) { | ||
String errorMessage = e.getMessage(); | ||
String errorMessageFormat = "Error occurred in the phase: '%s'. Error message: %s"; | ||
return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorMessage, | ||
String.format(errorMessageFormat, errorContext.getPhase(), errorMessage), ErrorType.USER, false, e); | ||
} | ||
|
||
/** | ||
* Get a ProgramFailureException with the given error | ||
* information from {@link IllegalStateException}. | ||
* | ||
* @param e The IllegalStateException to get the error information from. | ||
* @return A ProgramFailureException with the given error information. | ||
*/ | ||
private ProgramFailureException getProgramFailureException(IllegalStateException e, ErrorContext errorContext) { | ||
String errorMessage = e.getMessage(); | ||
String errorMessageFormat = "Error occurred in the phase: '%s'. Error message: %s"; | ||
return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorMessage, | ||
String.format(errorMessageFormat, errorContext.getPhase(), errorMessage), ErrorType.SYSTEM, false, e); | ||
} | ||
|
||
/** | ||
* Get a ProgramFailureException with the given error | ||
* information from {@link InvalidConfigPropertyException}. | ||
* | ||
* @param e The InvalidConfigPropertyException to get the error information from. | ||
* @return A ProgramFailureException with the given error information. | ||
*/ | ||
private ProgramFailureException getProgramFailureException(InvalidConfigPropertyException e, | ||
ErrorContext errorContext) { | ||
String errorMessage = e.getMessage(); | ||
String errorMessageFormat = "Error occurred in the phase: '%s'. Error message: %s"; | ||
return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorMessage, | ||
String.format(errorMessageFormat, errorContext.getPhase(), errorMessage), ErrorType.USER, false, e); | ||
} | ||
|
||
/** | ||
* Get a ProgramFailureException with the given error | ||
* information from {@link NoSuchElementException}. | ||
* | ||
* @param e The NoSuchElementException to get the error information from. | ||
* @return A ProgramFailureException with the given error information. | ||
*/ | ||
private ProgramFailureException getProgramFailureException(NoSuchElementException e, ErrorContext errorContext) { | ||
String errorMessage = e.getMessage(); | ||
String errorMessageFormat = "Error occurred in the phase: '%s'. Error message: %s"; | ||
return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorMessage, | ||
String.format(errorMessageFormat, errorContext.getPhase(), errorMessage), ErrorType.SYSTEM, false, e); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,10 @@ | |
*/ | ||
package io.cdap.plugin.http.common.pagination; | ||
|
||
import io.cdap.cdap.api.exception.ErrorCategory; | ||
import io.cdap.cdap.api.exception.ErrorCodeType; | ||
import io.cdap.cdap.api.exception.ErrorType; | ||
import io.cdap.cdap.api.exception.ErrorUtils; | ||
import io.cdap.plugin.http.common.RetryPolicy; | ||
import io.cdap.plugin.http.common.error.ErrorHandling; | ||
import io.cdap.plugin.http.common.error.HttpErrorHandler; | ||
|
@@ -26,6 +30,7 @@ | |
import io.cdap.plugin.http.common.pagination.state.PaginationIteratorState; | ||
import io.cdap.plugin.http.common.pagination.state.UrlPaginationIteratorState; | ||
import io.cdap.plugin.http.source.common.BaseHttpSourceConfig; | ||
import okhttp3.internal.http2.ErrorCode; | ||
import org.awaitility.Awaitility; | ||
import org.awaitility.core.ConditionTimeoutException; | ||
import org.awaitility.pollinterval.FixedPollInterval; | ||
|
@@ -36,9 +41,12 @@ | |
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
|
@@ -122,8 +130,13 @@ protected BasePage getNextPage() throws IOException { | |
case SUCCESS: | ||
break; | ||
case STOP: | ||
throw new IllegalStateException(String.format("Fetching from url '%s' returned status code '%d' and body '%s'", | ||
nextPageUrl, httpStatusCode, response.getBody())); | ||
String errorMessage = String.format("Fetching from url '%s' returned status code '%d' and body '%s'", | ||
nextPageUrl, httpStatusCode, response.getBody()); | ||
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorMessage, String.format("Error message: %s", errorMessage), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't do redundant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed from all files |
||
ErrorUtils.getActionErrorByStatusCode(httpStatusCode).getErrorType(), | ||
true, ErrorCodeType.HTTP, String.valueOf(httpStatusCode), | ||
"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status", new IllegalStateException(errorMessage)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should use official documentations link: https://datatracker.ietf.org/doc/html/rfc7231#section-6 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
case SKIP: | ||
case SEND: | ||
if (!this.supportsSkippingPages()) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
errorReason
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated