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

[dotnet] Remove indirection through Response on HTTP exception #14892

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Dec 12, 2024

User description

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

This removes an unnecessary use of Response, which would throw an exception in the subsequent UnpackAndThrowOnError anyway.

Motivation and Context

This code stores an Exception into Response.Value. This is the only usage of that type to store anything other than a JSON-deserialized response, which the custom converter handles to only contain bool, long, double, string, List<object>, or Dictionary<string, object>.

This change makes it so we can assume Response.Value is always one of those types.

While trying to test this, I noticed this code path is unreachable by normal means anyway. The HttpRequestException is caught earlier on and turned into a normal WebDriverException.

catch (HttpRequestException ex)
{
string unknownErrorMessage = "An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL {0}. The exception message was: {1}";
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, unknownErrorMessage, requestInfo.FullUri.AbsoluteUri, ex.Message), ex);
}

However, this code path may be hit if an advanced user supplies their own ICommandExecutor in the WebDriver constructor. Therefore, this catch block is still meaningful.

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.

PR Type

enhancement, bug_fix


Description

  • Removed unnecessary indirection through the Response object when handling HttpRequestException in WebDriver.cs.
  • Directly throws a WebDriverException with a clear error message, improving code clarity and error handling.
  • Ensures that Response.Value is not used to store exceptions, aligning with the expected data types.

Changes walkthrough 📝

Relevant files
Enhancement
WebDriver.cs
Simplify exception handling for HTTP requests in WebDriver

dotnet/src/webdriver/WebDriver.cs

  • Removed the creation of a Response object in the catch block for
    HttpRequestException.
  • Threw a WebDriverException directly with a descriptive message.
  • +1/-5     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 1 🔵⚪⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling
    The new implementation throws the exception immediately instead of wrapping it in a Response object. Verify this doesn't break error handling expectations for custom ICommandExecutor implementations.

    Copy link
    Contributor

    PR Code Suggestions ✨

    No code suggestions found for the PR.

    Status = WebDriverResult.UnhandledError,
    Value = e
    };
    throw new WebDriverException("The " + driverCommandToExecute + " command failed to send.", e);
    Copy link
    Member

    Choose a reason for hiding this comment

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

    I don't mind.

    Do you see potential miss-using? It is new exception type...

    Copy link
    Contributor Author

    Choose a reason for hiding this comment

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

    Luckily this is the same behavior which already exists. Currently the method UnpackAndThrowOnError throws the same exception. The only difference here is the more specific message and inner exception passed instead to e.ToString() appending to the message.

    For reasons I expanded in the PR description, this doesn’t change any execution, so there’s no potential issue here.

    Copy link
    Member

    Choose a reason for hiding this comment

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

    All right. Let's think how to improve the error. This error will be user faced in any cace of network issue - it is so popular.

    Copy link
    Member

    @nvborisenko nvborisenko Dec 19, 2024

    Choose a reason for hiding this comment

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

    Unhandled error while executing <command>. ?

    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.

    2 participants