Skip to content
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

[cdp][java] Continue requests without modification for know errors in NetworkInterceptor #13836

Merged

Conversation

pujagani
Copy link
Contributor

@pujagani pujagani commented Apr 18, 2024

User description

Related to #13774

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

Selenium was returning an HTTP 200 response if CDP responded with an error parameter in the paused request. The changes fix this by adding a check and continuing the request without modification.

Motivation and Context

Fixes #13774

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Type

Bug fix, Enhancement


Description

  • Added error response handling in network request interception across multiple versions (v85, v121, v122, v123) to continue without modification when errors are detected.
  • Enhanced the idealized network handling to improve the response to paused requests with errors.
  • Updated tests to check the new behavior ensuring that requests with known errors are handled correctly.

Changes walkthrough

Relevant files
Enhancement
Network.java
Enhance error handling in paused network requests               

java/src/org/openqa/selenium/devtools/idealized/Network.java

  • Added a check for error responses in paused requests and continue
    without modification if an error is present.
  • Enhanced the handling of paused requests by directly continuing them
    when an error response is detected.
  • +8/-0     
    v121Network.java
    Implement error response handling in v121                               

    java/src/org/openqa/selenium/devtools/v121/v121Network.java

  • Implemented method to check for error responses in paused requests.
  • Adjusted logic to only consider response status code for creating
    messages.
  • +6/-2     
    v122Network.java
    Add error handling in network requests for v122                   

    java/src/org/openqa/selenium/devtools/v122/v122Network.java

  • Added method to detect error responses in network requests.
  • Modified message creation to ignore error reasons unless a status code
    is present.
  • +6/-2     
    v123Network.java
    Introduce error response handling in v123                               

    java/src/org/openqa/selenium/devtools/v123/v123Network.java

  • Introduced error response detection for paused network requests.
  • Altered message creation logic to focus on status codes.
  • +6/-2     
    V85Network.java
    Simplify and enhance error handling in V85 network interception

    java/src/org/openqa/selenium/devtools/v85/V85Network.java

  • Added error response detection in network interception.
  • Simplified message creation by focusing on status codes.
  • +6/-2     
    Tests
    NetworkInterceptorTest.java
    Update tests for new error handling in network requests   

    java/test/org/openqa/selenium/devtools/NetworkInterceptorTest.java

  • Added a test to ensure proper handling when a known error occurs
    during a network request.
  • Modified existing tests to adapt to the new error handling mechanism.
  • +14/-1   

    PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link
    Contributor

    PR Description updated to latest commit (ed30887)

    Copy link
    Contributor

    PR Review

    ⏱️ Estimated effort to review [1-5]

    3, because the PR involves changes across multiple files and versions, including abstract methods and their implementations in derived classes. The logic is not overly complex but requires understanding the context of network requests handling in Selenium.

    🧪 Relevant tests

    Yes

    🔍 Possible issues

    Possible Bug: The new method hasErrorResponse checks only for the presence of getResponseErrorReason. If there are scenarios where an error is indicated without this field being set, those errors might not be handled correctly.

    🔒 Security concerns

    No


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link
    Contributor

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Enhancement
    Add a debug log statement before continuing a request without modification for better traceability.

    Consider adding a log statement before sending the continueWithoutModification command.
    This will help in debugging and understanding the flow of requests that are continued
    without modification.

    java/src/org/openqa/selenium/devtools/idealized/Network.java [204-207]

     if (hasErrorResponse(pausedRequest)) {
    +  logger.debug("Continuing without modification for request: " + pausedRequest);
       devTools.send(continueWithoutModification(pausedRequest));
       return;
     }
     
    Enhance the error response check to include status codes indicating client or server errors.

    The method hasErrorResponse is introduced to check for the presence of a response error
    reason. To maintain consistency and possibly enhance error handling, consider also
    checking for other error indicators such as specific status codes or error messages.

    java/src/org/openqa/selenium/devtools/v85/V85Network.java [174-175]

     protected boolean hasErrorResponse(RequestPaused pausedReq) {
    -  return pausedReq.getResponseErrorReason().isPresent();
    +  return pausedReq.getResponseErrorReason().isPresent() || pausedReq.getResponseStatusCode().map(code -> code >= 400).orElse(false);
     }
     
    Modify the test filter to simulate an error condition, enhancing the test's effectiveness in checking error handling.

    The test shouldProceedAsNormalIfRequestResultInAnKnownError uses a generic filter that
    does nothing (next -> next). To make this test more meaningful, consider simulating an
    error condition within the filter to actually test the error handling capabilities of the
    NetworkInterceptor.

    java/test/org/openqa/selenium/devtools/NetworkInterceptorTest.java [258-259]

    -Filter filter = next -> next;
    +Filter filter = next -> req -> {
    +  throw new WebDriverException("Simulated network error");
    +  return next.apply(req);
    +};
     
    Possible issue
    Ensure that both response status codes and error reasons are checked before creating SE messages.

    It seems that the condition for creating SE messages has been narrowed down to only check
    if the response status code is present, omitting the previous check for response error
    reason. If this change was unintentional, consider reverting to the original condition to
    ensure that error reasons are also handled.

    java/src/org/openqa/selenium/devtools/v121/v121Network.java [117]

    -if (pausedReq.getResponseStatusCode().isPresent()) {
    +if (pausedReq.getResponseStatusCode().isPresent() || pausedReq.getResponseErrorReason().isPresent()) {
     
    Robustness
    Add a check to ensure the DevTools connection is active before sending commands.

    The method continueWithoutModification is called without checking if the devTools instance
    is connected or active. Consider adding a check to ensure that devTools is ready to send
    commands to avoid potential runtime errors.

    java/src/org/openqa/selenium/devtools/idealized/Network.java [205]

    -devTools.send(continueWithoutModification(pausedRequest));
    +if (devTools.isConnected()) {
    +  devTools.send(continueWithoutModification(pausedRequest));
    +} else {
    +  logger.warn("DevTools is not connected. Cannot continue without modification.");
    +}
     

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    @@ -200,6 +200,12 @@ public void prepareToInterceptTraffic() {
    pausedRequest -> {
    try {
    String id = getRequestId(pausedRequest);

    if (hasErrorResponse(pausedRequest)) {
    devTools.send(continueWithoutModification(pausedRequest));
    Copy link
    Contributor

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Do we need to clean up the future in pendingResponses for this interception?

    Copy link
    Contributor Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Yes, you are right. I have updated the PR for the same.

    joebandenburg pushed a commit to joebandenburg/selenium that referenced this pull request Apr 19, 2024
    … NetworkInterceptor
    
    This is an alternative solution to SeleniumHQ#13836.
    
    Related to SeleniumHQ#13774
    joebandenburg added a commit to joebandenburg/selenium that referenced this pull request Apr 19, 2024
    … NetworkInterceptor
    
    This is an alternative solution to SeleniumHQ#13836.
    
    Related to SeleniumHQ#13774
    joebandenburg added a commit to joebandenburg/selenium that referenced this pull request Apr 19, 2024
    … NetworkInterceptor
    
    This is an alternative solution to SeleniumHQ#13836.
    
    Related to SeleniumHQ#13774
    @pujagani pujagani force-pushed the handle-knownerror-networkinterceptor branch from cbed6a7 to 8b8418b Compare April 22, 2024 04:05
    @pujagani pujagani force-pushed the handle-knownerror-networkinterceptor branch from 8b8418b to 788763d Compare April 23, 2024 08:49
    @pujagani pujagani merged commit 907b4f4 into SeleniumHQ:trunk Apr 23, 2024
    37 of 40 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    [🐛 Bug]: NetworkInterceptor overwrites failed requests with 200 response
    2 participants