Skip to content

fix: raise on HTTP error responses without a W3C error body - #7

Draft
MattL1989 wants to merge 1 commit into
masterfrom
fix/error-handler-http-status
Draft

MattL1989 wants to merge 1 commit into
masterfrom
fix/error-handler-http-status

Conversation

@MattL1989

Copy link
Copy Markdown
Owner

Problem

MobileErrorHandler.check_response only raised when the response body was a W3C error object, and returned silently in every other case, whatever the HTTP status. Selenium's RemoteConnection hands HTTP errors to the handler as {'status': <http status>, 'value': <body>}. So when a proxy, load balancer or device farm gateway in front of Appium answers with an error page, the error page itself came back to the caller as the command result:

Server response Result before
POST /element → 502 with an HTML page find_element() returns the HTML string, although it is annotated to return a WebElement
GET /source → 401 driver.page_source returns 'Authorization Required'
POST /element/{id}/click → 500 Internal Server Error (text/plain) click() returns normally

The reverse also happened. A successful 200 response whose result merely looked like an error raised WebDriverException: ok: for example, execute_script returning {'value': {'error': 'none', 'message': 'ok'}}.

Selenium's own ErrorHandler raises in the first three cases and passes the last one.

Fix

  • Return early for successful responses, those with no status or ErrorCode.SUCCESS, exactly as Selenium's ErrorHandler does.
  • For an error response whose body is not a W3C error object, raise WebDriverException with the body as the message: for example Message: <html><body>502 Bad Gateway</body></html>. I raise it directly rather than calling Selenium's ErrorHandler, because Selenium's handler crashes with AttributeError on some non-standard bodies, such as a JSON list.
  • W3C error responses are mapped to exception classes exactly as before. This includes NoSuchContextException and InvalidSwitchToTargetException.

Behaviour change: error responses that are not W3C error objects now raise instead of being returned as a value.

Tests

  • New test/unit/webdriver/errorhandler_test.py checks that the handler:
    • raises for HTML, plain-text, 401 and non-W3C JSON error bodies;
    • still maps a W3C no such element error to NoSuchElementException;
    • does not raise for successful responses, including one whose result contains value.error;
    • raises when find_element() hits a 502 page, checked end to end through httpretty.
  • 6 of the new tests fail without the fix.
  • ruff check, ruff format --check, mypy appium and the full unit test suite pass.

Generated by Claude Code

MobileErrorHandler only raised when the response body was a W3C error
object and returned silently otherwise, regardless of the HTTP status.
So an HTML or plain-text error page from a proxy or load balancer, or a
401, was handed back to the caller as the command result: find_element()
returned the HTML string of a 502 page, page_source returned
'Authorization Required' and click() silently "succeeded".

The handler also looked for an error object in successful responses, so
a 200 response whose script result contained value.error raised a
WebDriverException.

Return early for successful responses, as Selenium's ErrorHandler does,
and raise a WebDriverException with the response body when an error
response is not a W3C error object. W3C errors are mapped to exception
classes as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MattL1989
MattL1989 force-pushed the fix/error-handler-http-status branch from 41a453f to 9235c91 Compare November 1, 2026 18:50
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.

1 participant