From f859be1b7fd75b9acb5ac85974b813e1e25a41ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AE=E0=AE=A9=E0=AF=8B=E0=AE=9C=E0=AF=8D=E0=AE=95?= =?UTF-8?q?=E0=AF=81=E0=AE=AE=E0=AE=BE=E0=AE=B0=E0=AF=8D=20=E0=AE=AA?= =?UTF-8?q?=E0=AE=B4=E0=AE=A9=E0=AE=BF=E0=AE=9A=E0=AF=8D=E0=AE=9A=E0=AE=BE?= =?UTF-8?q?=E0=AE=AE=E0=AE=BF?= Date: Sun, 29 Dec 2024 20:39:42 +0530 Subject: [PATCH 1/2] Fix exception traceback to omit superfluous 'Backtrace' information Fixes #9977 Filter out superfluous 'StackTrace' information from Selenium 4 exception tracebacks. * Add a check for an environment variable to conditionally include the stacktrace. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/SeleniumHQ/selenium/issues/9977?shareId=XXXX-XXXX-XXXX-XXXX). --- py/selenium/common/exceptions.py | 3 ++- py/selenium/webdriver/remote/errorhandler.py | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/py/selenium/common/exceptions.py b/py/selenium/common/exceptions.py index d1d7efdd1f0bd..d709a0fdac5cb 100644 --- a/py/selenium/common/exceptions.py +++ b/py/selenium/common/exceptions.py @@ -18,6 +18,7 @@ from typing import Optional from typing import Sequence +import os SUPPORT_MSG = "For documentation on this error, please visit:" ERROR_URL = "https://www.selenium.dev/documentation/webdriver/troubleshooting/errors" @@ -38,7 +39,7 @@ def __str__(self) -> str: exception_msg = f"Message: {self.msg}\n" if self.screen: exception_msg += "Screenshot: available via screen\n" - if self.stacktrace: + if self.stacktrace and os.getenv("INCLUDE_STACKTRACE"): stacktrace = "\n".join(self.stacktrace) exception_msg += f"Stacktrace:\n{stacktrace}" return exception_msg diff --git a/py/selenium/webdriver/remote/errorhandler.py b/py/selenium/webdriver/remote/errorhandler.py index d2df3b1b8e241..596f590c9217c 100644 --- a/py/selenium/webdriver/remote/errorhandler.py +++ b/py/selenium/webdriver/remote/errorhandler.py @@ -18,6 +18,7 @@ from typing import Any from typing import Dict from typing import Type +import os from selenium.common.exceptions import DetachedShadowRootException from selenium.common.exceptions import ElementClickInterceptedException @@ -141,6 +142,12 @@ class ErrorCode: class ErrorHandler: """Handles errors returned by the WebDriver server.""" + def filter_stacktrace(self, stacktrace): + """Filters out 'Backtrace' information from the stacktrace.""" + if stacktrace: + return [line for line in stacktrace if 'Backtrace' not in line] + return stacktrace + def check_response(self, response: Dict[str, Any]) -> None: """Checks that a JSON response from the WebDriver does not have an error. @@ -222,6 +229,8 @@ def check_response(self, response: Dict[str, Any]) -> None: stacktrace.append(msg) except TypeError: pass + if not os.getenv("INCLUDE_STACKTRACE"): + stacktrace = self.filter_stacktrace(stacktrace) if exception_class == UnexpectedAlertPresentException: alert_text = None if "data" in value: From 789368a8f142a5f924f0d51bc8f150c77c780c05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AE=E0=AE=A9=E0=AF=8B=E0=AE=9C=E0=AF=8D=E0=AE=95?= =?UTF-8?q?=E0=AF=81=E0=AE=AE=E0=AE=BE=E0=AE=B0=E0=AF=8D=20=E0=AE=AA?= =?UTF-8?q?=E0=AE=B4=E0=AE=A9=E0=AE=BF=E0=AE=9A=E0=AF=8D=E0=AE=9A=E0=AE=BE?= =?UTF-8?q?=E0=AE=AE=E0=AE=BF?= Date: Sun, 29 Dec 2024 20:43:51 +0530 Subject: [PATCH 2/2] Discard changes to py/selenium/webdriver/remote/errorhandler.py --- py/selenium/webdriver/remote/errorhandler.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/py/selenium/webdriver/remote/errorhandler.py b/py/selenium/webdriver/remote/errorhandler.py index 596f590c9217c..d2df3b1b8e241 100644 --- a/py/selenium/webdriver/remote/errorhandler.py +++ b/py/selenium/webdriver/remote/errorhandler.py @@ -18,7 +18,6 @@ from typing import Any from typing import Dict from typing import Type -import os from selenium.common.exceptions import DetachedShadowRootException from selenium.common.exceptions import ElementClickInterceptedException @@ -142,12 +141,6 @@ class ErrorCode: class ErrorHandler: """Handles errors returned by the WebDriver server.""" - def filter_stacktrace(self, stacktrace): - """Filters out 'Backtrace' information from the stacktrace.""" - if stacktrace: - return [line for line in stacktrace if 'Backtrace' not in line] - return stacktrace - def check_response(self, response: Dict[str, Any]) -> None: """Checks that a JSON response from the WebDriver does not have an error. @@ -229,8 +222,6 @@ def check_response(self, response: Dict[str, Any]) -> None: stacktrace.append(msg) except TypeError: pass - if not os.getenv("INCLUDE_STACKTRACE"): - stacktrace = self.filter_stacktrace(stacktrace) if exception_class == UnexpectedAlertPresentException: alert_text = None if "data" in value: