-
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?
[PLUGIN-1826] Error Management HTTP Source and Sink and fix sonar issues #180
Conversation
Amit-CloudSufi
commented
Dec 6, 2024
•
edited
Loading
edited
- Jira : https://cdap.atlassian.net/browse/PLUGIN-1826
This comment was marked as resolved.
This comment was marked as resolved.
4cad117
to
fac0c1a
Compare
e612cd0
to
d273336
Compare
0af6ccc
to
a41b2f6
Compare
…ckage to handle error provider, fix sonar issues and added Validation error for linear retry duration
4f1d5f5
to
2e321fa
Compare
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.
please fix unit tests
errorMessage, String.format("Error message: %s", errorMessage), | ||
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
updated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
don't do redundant Error message: %s
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.
removed from all files
@@ -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'", |
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
throw new IllegalArgumentException("Invalid URL: " + configURL, e); | ||
} catch (IOException e) { | ||
LOG.warn("Error making {} request to URL {}.", config.getMethod(), config.getUrl()); | ||
String errorMessage = "Unable to make request. "; |
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.
this is not an actional error reason
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
LOG.warn("Error making {} request to URL {}.", config.getMethod(), config.getUrl()); | ||
String errorMessage = "Unable to make request. "; | ||
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorMessage, String.format("Error message: %s", errorMessage), ErrorType.UNKNOWN, true, e); |
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.
similar comment here
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
if (!shouldRetry) { | ||
messageBuffer.clear(); | ||
retryCount = 0; | ||
String errorMessage = String.format("Unable to execute HTTP request to %s.", url); |
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
retryCount = 0; | ||
String errorMessage = String.format("Unable to execute HTTP request to %s.", url); | ||
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorMessage, String.format("Error message: %s", errorMessage), ErrorType.SYSTEM, true, e); |
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.
similar comment here
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
String errorMessage = String.format("Unable to execute HTTP request to %s.", url); | ||
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorMessage, String.format("Error message: %s", errorMessage), ErrorType.SYSTEM, true, e); | ||
|
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.
nit: remove empty line
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.
removed
} catch (Exception e) { | ||
String errorMessage = String.format("Unexpected error occurred while executing HTTP request to URL: %s", url); | ||
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorMessage, String.format("Error message: %s", errorMessage), ErrorType.UNKNOWN, true, e); |
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.
similar comment here
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
@@ -247,16 +263,16 @@ public CloseableHttpClient createHttpClient(String pageUriStr) throws IOExceptio | |||
URI uri = URI.create(pageUriStr); | |||
AuthScope authScope = new AuthScope(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme())); | |||
credentialsProvider.setCredentials(authScope, | |||
new UsernamePasswordCredentials(config.getUsername(), config.getPassword())); | |||
new UsernamePasswordCredentials(config.getUsername(), config.getPassword())); |
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.
revert unintended changes.
new UsernamePasswordCredentials( | ||
config.getProxyUsername(), config.getProxyPassword())); |
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.
revert unintended changes
} | ||
|
||
|
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.
nit: remove empty line
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.
removed
@@ -311,7 +324,7 @@ private Header getAuthorizationHeader(AccessToken accessToken) { | |||
private List<PlaceholderBean> getPlaceholderListFromURL() { | |||
List<PlaceholderBean> placeholderList = new ArrayList<>(); | |||
if (!(config.getMethod().equals(REQUEST_METHOD_PUT) || config.getMethod().equals(REQUEST_METHOD_PATCH) || | |||
config.getMethod().equals(REQUEST_METHOD_DELETE))) { | |||
config.getMethod().equals(REQUEST_METHOD_DELETE))) { |
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.
revert unintended changes
.await().with() | ||
.pollInterval(pollInterval) | ||
.pollDelay(config.getWaitTimeBetweenPages(), TimeUnit.MILLISECONDS) | ||
.timeout(config.getMaxRetryDuration(), TimeUnit.SECONDS) | ||
.until(this::executeHTTPServiceAndCheckStatusCode); |
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.
revert unintended changes
String errorMessage = "Error while executing http request for remaining input messages" + | ||
" after the batch execution."; | ||
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorMessage, String.format("Error message: %s", errorMessage), ErrorType.UNKNOWN, true, |
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.
similar comment here
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.
Also don't we have status code information here?
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
} catch (Exception e) { | ||
throw new RuntimeException("Error while executing http request for remaining input messages " + | ||
"after the batch execution. " + e); | ||
String errorMessage = "Error while executing http request for remaining input messages" + |
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.
can we add http request information here?
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.
added
errorMessage, String.format("Error message: %s", errorMessage), | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
similar comment here about documentation
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
String errorMessage = String.format("Fetching from url '%s' returned status code '%d' and body '%s'", | ||
config.getUrl(), httpStatusCode, httpResponseBody); | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
similar comment here
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
case SKIP: | ||
case SEND: | ||
LOG.warn(String.format("Fetching from url '%s' returned status code '%d' and body '%s'", | ||
config.getUrl(), httpStatusCode, httpResponseBody)); | ||
config.getUrl(), httpStatusCode, httpResponseBody)); |
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.
revert unintended changes
@@ -1,3 +1,4 @@ | |||
|
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.
nit: remove empty line
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.
removed
* @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) { |
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.
I see All the 4 methods being Overloaded , have only 1 param changing i.e. ERROR TYPE.
Only 1 method should suffice :
private ProgramFailureException getProgramFailureException(Exception e, ErrorContext errorContext, ErrorType errorType) {
String errorMessage = e.getMessage();
return ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
errorMessage,
String.format(ERROR_MESSAGE_FORMAT, errorContext.getPhase(), errorMessage), errorType, false, e);
}
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.
+1
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
@@ -28,6 +28,7 @@ | |||
import io.cdap.plugin.http.common.http.OAuthUtil; | |||
|
|||
import java.io.File; | |||
import java.util.Objects; |
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.
is this used anywhere?
@@ -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; |
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.
where is this used?
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; |
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.
where are these new imports used?
inputSchema = Schema.parseJson(hConf.get(INPUT_SCHEMA_KEY)); | ||
return new HTTPRecordWriter(config, inputSchema); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Unable to parse the input schema. Reason: " + e.getMessage(), e); |
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.
String.format("Failed to parse the input schema with message: %s", e.getMessage())
String errorMessage = String.format("Retry failed! Unable to read new page and execute request. " + | ||
"Fetching from '%s' returned http error status code '%s'. For more details, see %s", | ||
config.getUrl(), httpStatusCode, supportedDocUrl); | ||
String errorReason = "Retry failed. Unable to read new page."; |
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.
ErrorUtils.ActionErrorPair pair = ErrorUtils.getActionErrorByStatusCode(statusCode);
String errorReason = String.format("Unable to read new page: %s %s. %s", e.getStatusCode(), e.getStatusMessage(), pair.getCorrectiveAction());
This comment applies to whole PR.
sb.append("&"); | ||
StringBuilder sb = new StringBuilder(); | ||
if (input != null && input.getSchema() != null) { | ||
for (Schema.Field field : Objects.requireNonNull(input.getSchema().getFields())) { |
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.
same comment here
Preconditions.checkNotNull(schema.getFields()).stream() | ||
.map(Schema.Field::getName) | ||
.collect(Collectors.toList())); | ||
List<String> getNameList = Objects.nonNull(schema) ? Preconditions.checkNotNull(schema.getFields()).stream() |
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.
please don't do direct Objects.nonNull
instead move this code inside try & catch NPE and then throw ProgramFailureException
with details.
} catch (IOException e) { | ||
String errorMessage = String.format("Unable to validate OAuth and process the request. " + | ||
"Validation failed with reason %s.", e.getMessage()); | ||
String errorReason = "Unable to validate OAuth and process the request."; | ||
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), | ||
errorReason, errorMessage, ErrorType.UNKNOWN, true, e); |
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.
if this is called in configurePipeline
please use failureCollector
instead.
This comment applies to whole PR.
@@ -511,7 +512,7 @@ public Schema getSchema() { | |||
return Strings.isNullOrEmpty(schema) ? null : Schema.parseJson(schema); | |||
} catch (IOException e) { | |||
throw new InvalidConfigPropertyException("Unable to parse output schema: " + | |||
schema, e, PROPERTY_SCHEMA); | |||
schema, e, PROPERTY_SCHEMA); |
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.
remove unintended checkstyle changes.
This comment applies to whole PR.
@@ -545,7 +546,7 @@ public Map<String, String> getFullFieldsMapping() { | |||
Map<String, String> result = new HashMap<>(); | |||
|
|||
if (!Strings.isNullOrEmpty(schema)) { | |||
for (Schema.Field field : getSchema().getFields()) { | |||
for (Schema.Field field : Objects.requireNonNull(Objects.requireNonNull(getSchema()).getFields())) { |
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.
same comment regarding null checks.