Skip to content

[py]: return message as part of exception in execute method #15751

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

Merged
merged 3 commits into from
May 20, 2025

Conversation

navin772
Copy link
Member

@navin772 navin772 commented May 19, 2025

User description

🔗 Related Issues

💥 What does this PR do?

Returns a meaningful error and message when an exception is raised in the WebSocketConnection execute method.

Earlier only 'error': 'unknown error' exception was raised which doesn't provides any good error message.

Now, its much better:

Exception: unknown error: No such file or directory: /private/var/tmp/_bazel_navinchandra/a5b294e127f7c14e5d2a4b14aeba0f68/....

🔧 Implementation Notes

💡 Additional Considerations

🔄 Types of changes

  • Bug fix (backwards compatible)

PR Type

Bug fix


Description

  • Improve exception handling in WebSocketConnection execute

  • Include error message in raised exceptions for clarity

  • Enhance debugging by surfacing server-side error details


Changes walkthrough 📝

Relevant files
Bug fix
websocket_connection.py
Enhance exception message handling in WebSocketConnection

py/selenium/webdriver/remote/websocket_connection.py

  • Enhanced exception to include error message if present
  • Now raises detailed error combining 'error' and 'message' fields
  • Improves clarity of exceptions from WebSocket responses
  • +6/-1     

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @selenium-ci selenium-ci added the C-py Python Bindings label May 19, 2025
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ❌

    5678 - Partially compliant

    Compliant requirements:

    • Provide more meaningful error messages for connection issues

    Non-compliant requirements:

    • Fix the "Error: ConnectFailure (Connection refused)" issue when instantiating ChromeDriver multiple times

    Requires further human verification:

    • Support Ubuntu 16.04.4 with Chrome 65.0.3325.181 and ChromeDriver 2.35

    1234 - Not compliant

    Non-compliant requirements:

    • Fix issue with Firefox 42.0 not triggering JavaScript in link's href on click() in Selenium 2.48

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

    Exception Handling

    The PR improves error reporting but uses generic Exception class. Consider using a more specific exception type like WebDriverException for better error handling by client code.

        raise Exception(error_msg)
    else:
        raise Exception(error)

    Copy link
    Contributor

    qodo-merge-pro bot commented May 19, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Learned
    best practice
    Use specific exception types
    Suggestion Impact:The commit implemented the suggestion by replacing generic Exception with WebDriverException in three places: two in the error handling code for WebSocket responses (lines 14-18) and one additional place in the _deserialize_result method (line 27)

    code diff:

    +
    +from selenium.common import WebDriverException
     
     logger = logging.getLogger(__name__)
     
    @@ -68,9 +70,9 @@
                 error = response["error"]
                 if "message" in response:
                     error_msg = f"{error}: {response['message']}"
    -                raise Exception(error_msg)
    +                raise WebDriverException(error_msg)
                 else:
    -                raise Exception(error)
    +                raise WebDriverException(error)
             else:
                 result = response["result"]
                 return self._deserialize_result(result, command)
    @@ -102,7 +104,7 @@
         def _deserialize_result(self, result, command):
             try:
                 _ = command.send(result)
    -            raise Exception("The command's generator function did not exit when expected!")
    +            raise WebDriverException("The command's generator function did not exit when expected!")

    Use a more specific exception type instead of the generic Exception class.
    Consider using a custom exception or a more specific built-in exception that
    better represents the error condition from the WebSocket response.

    py/selenium/webdriver/remote/websocket_connection.py [67-73]

     if "error" in response:
         error = response["error"]
         if "message" in response:
             error_msg = f"{error}: {response['message']}"
    -        raise Exception(error_msg)
    +        raise WebDriverException(error_msg)
         else:
    -        raise Exception(error)
    +        raise WebDriverException(error)

    [Suggestion processed]

    Suggestion importance[1-10]: 6

    __

    Why:
    Relevant best practice - Fix inconsistent error messages and validation logic. Ensure error messages are clear, accurate, and provide specific information about the error condition.

    Low
    General
    Remove unnecessary variable

    The current implementation creates a variable error_msg only to use it once
    immediately. Simplify the code by raising the exception directly with the
    formatted error message to improve readability and reduce unnecessary variables.

    py/selenium/webdriver/remote/websocket_connection.py [67-73]

     if "error" in response:
         error = response["error"]
         if "message" in response:
    -        error_msg = f"{error}: {response['message']}"
    -        raise Exception(error_msg)
    +        raise Exception(f"{error}: {response['message']}")
         else:
             raise Exception(error)
    • Apply / Chat
    Suggestion importance[1-10]: 5

    __

    Why: The suggestion improves code readability by eliminating an unnecessary variable, but it does not affect functionality or correctness, making it a minor style enhancement.

    Low
    • Update

    @navin772 navin772 requested review from p0deje and cgoldberg May 19, 2025 13:11
    Copy link
    Contributor

    @cgoldberg cgoldberg left a comment

    Choose a reason for hiding this comment

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

    This looks fine, but it should probably raise WebDriverException instead of a generic Exception.

    Same thing in the _deserialize_result method in that file... if you want to fix that while you're in there.

    Copy link
    Member

    @p0deje p0deje left a comment

    Choose a reason for hiding this comment

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

    Agree with @cgoldberg, let's change to WebDriverException.

    Copy link
    Contributor

    @cgoldberg cgoldberg left a comment

    Choose a reason for hiding this comment

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

    👍

    @p0deje p0deje merged commit 65ffd18 into SeleniumHQ:trunk May 20, 2025
    15 of 16 checks passed
    @navin772 navin772 deleted the ws-exception branch May 20, 2025 13:27
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    4 participants