Skip to content

fix(webdriver): handle non-W3C HTTP error responses in MobileErrorHandler - #1366

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

hoanle0126 wants to merge 3 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

Raise a generic WebDriverException in MobileErrorHandler.check_response when an HTTP error response (status >= 400, such as 401/502/503 proxy or gateway responses) cannot be handled as a standard W3C error, while continuing to delegate standard W3C errors to Selenium and ignoring legacy JSONWP status payloads.

Added unit tests in test/unit/webdriver/errorhandler_test.py covering non-W3C HTTP error responses and proxy/gateway failures.

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.

@KazuCocoa

Copy link
Copy Markdown
Member

Thanks for the clarification.

I think it makes sense to narrow the scope of this PR to handling non-W3C HTTP error responses (e.g., 401/502/503 from proxies or gateways), since those can realistically occur even when using a W3C-compliant Appium server.

Could you remove the handling for flat JSON error objects and non-standard alert data payloads? We don't need additional compatibility handling for cases that haven't been observed in practice.

I'd also prefer to keep the HTTP error handling minimal: raise a generic WebDriverException for HTTP error responses that cannot be handled as standard W3C errors, and continue delegating standard W3C error handling to Selenium as introduced in #1367.

Please keep the tests focused on these HTTP error cases as well.

@hoanle0126 hoanle0126 changed the title fix(webdriver): handle HTTP error responses and alert text in MobileErrorHandler fix(webdriver): handle non-W3C HTTP error responses in MobileErrorHandler Nov 18, 2026
@hoanle0126

Copy link
Copy Markdown
Contributor Author

@KazuCocoa Updated in 67b0579: removed the flat JSON error and non-dict alert data handling as well as the 405 UnknownMethodException special case, so MobileErrorHandler.check_response now delegates standard W3C errors directly to super().check_response() as in #1367 and raises a generic WebDriverException only for non-W3C HTTP >= 400 responses. Also trimmed the unit tests and PR description to match.

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