Skip to content

fix(webdriver): handle HTTP error responses and alert text in MobileErrorHandler - #1366

Open
hoanle0126 wants to merge 2 commits into
appium:masterfrom
hoanle0126:fix/errorhandler-http-and-alert-handling
Open

hoanle0126 wants to merge 2 commits into
appium:masterfrom
hoanle0126:fix/errorhandler-http-and-alert-handling

Conversation

@hoanle0126

@hoanle0126 hoanle0126 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Change list

Build on the Selenium delegation in MobileErrorHandler.check_response to handle non-W3C HTTP failures, flat error payloads, and non-dict alert data without crashing or failing silently:

  • Raise WebDriverException (or UnknownMethodException for HTTP 405) when check_response receives an HTTP status >= 400 with non-JSON/HTML proxy bodies, JSON arrays/strings, empty value objects, or scalar string value payloads.
  • Delegate flat W3C error dicts returned under HTTP error status codes and normalize non-dict data fields (wrapping string data in a text dict and omitting other non-dict values) before calling super().check_response so UnexpectedAlertPresentException preserves string alert_text without raising AttributeError.
  • Preserve existing W3C status error delegation, Appium context exception mapping, and non-W3C JSONWP ignoring behavior.

Updated unit tests in test/unit/webdriver/errorhandler_test.py covering HTTP error status codes, flat error objects, string and non-dict alert payloads, context exceptions, and Selenium W3C error delegation.

Types of changes

  • Bugfix (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 not work as expected)
  • Unit tests added/updated

@hoanle0126
hoanle0126 force-pushed the fix/errorhandler-http-and-alert-handling branch from 0da2609 to 8933e90 Compare November 16, 2026 00:07
@KazuCocoa

Copy link
Copy Markdown
Member

I guess this PR has been changed since the original one. I added #1367 to simplify this appium client's behavior.

Have you seen any necessary situation with this fix for w3c supported Appium versions?

@hoanle0126

Copy link
Copy Markdown
Contributor Author

@KazuCocoa Yes, this PR was rebased onto #1367 (ce92e55). While #1367 cleanly handles standard nested W3C error payloads ({"value": {"error": ...}}), checking against master and selenium==4.52.0 (RemoteConnection._request / ErrorHandler.check_response) shows two situations that still fail silently or raise AttributeError:

  1. HTTP >= 400 non-W3C proxy/auth/gateway responses and flat error objects: RemoteConnection._request returns {'status': <http_code>, 'value': <str|dict>} for HTTP 401 (Authorization Required), 502/503 HTML proxy errors, 405 responses, empty 500 bodies, or flat JSON error payloads like {"error": "invalid session id", ...} without a nested value key. On master after refactor: use selenium code as possible #1367, MobileErrorHandler.check_response returns None silently in those cases (so driver calls like find_element() return None instead of raising WebDriverException).
  2. unexpected alert open when value['data'] is not a dict: Selenium's ErrorHandler.check_response runs value["data"].get("text") whenever 'data' is present, so a string payload ("data": "Legacy alert") or "data": null raises AttributeError instead of UnexpectedAlertPresentException. Commit 3027f56 normalizes string data to {"text": ...} and omits non-dict/non-str data values before calling super().check_response.

Re-verified ruff check ., ruff format --check ., mypy appium, and pytest test/unit (275 passed).

@KazuCocoa

Copy link
Copy Markdown
Member

Could you clarify whether you've actually encountered these issues in a real environment using a W3C-compliant Appium version?

I understand that these issues could theoretically occur, but I'd like to know whether they've been observed in practice.

@hoanle0126

Copy link
Copy Markdown
Contributor Author

@KazuCocoa To be transparent, I have not encountered either case against a live W3C-compliant Appium server in practice. I noticed them while reading MobileErrorHandler.check_response against Selenium 4.52's RemoteConnection._request and ErrorHandler.check_response after #1367.

In practice, only non-W3C HTTP >= 400 responses (such as a 401/502/503 HTML or text response from a reverse proxy, load balancer, or cloud grid gateway in front of Appium, where RemoteConnection._request returns {'status': <code>, 'value': <str>}) are realistic in front of a W3C Appium server. Non-dict data on unexpected alert open and flat JSON error objects are defensive against non-standard intermediaries.

If you'd prefer keeping MobileErrorHandler minimal after #1367 (either narrowing this PR to just HTTP >= 400 non-JSON proxy responses, or closing it until someone hits this in a real environment), let me know and I'm happy to update or close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants