| 81 | |
| 82 | |
| 83 | class MobileErrorHandler(errorhandler.ErrorHandler): |
| 84 | def check_response(self, response: Dict[str, Any]) -> None: |
| 85 | """ |
| 86 | https://www.w3.org/TR/webdriver/#errors |
| 87 | """ |
| 88 | payload = response.get('value', '') |
| 89 | if isinstance(payload, dict): |
| 90 | payload_dict = payload |
| 91 | else: |
| 92 | try: |
| 93 | payload_dict = json.loads(payload) |
| 94 | except (json.JSONDecodeError, TypeError): |
| 95 | return |
| 96 | if not isinstance(payload_dict, dict): |
| 97 | return |
| 98 | value = payload_dict.get('value') |
| 99 | if not isinstance(value, dict): |
| 100 | return |
| 101 | error = value.get('error') |
| 102 | if not error: |
| 103 | return |
| 104 | |
| 105 | message = value.get('message', error) |
| 106 | stacktrace = value.get('stacktrace', '') |
| 107 | # In theory, we should also be checking HTTP status codes. |
| 108 | # Java client, for example, prints a warning if the actual `error` |
| 109 | # value does not match to the response's HTTP status code. |
| 110 | exception_class: Type[sel_exceptions.WebDriverException] = ERROR_TO_EXC_MAPPING.get( |
| 111 | error, sel_exceptions.WebDriverException |
| 112 | ) |
| 113 | if exception_class is sel_exceptions.WebDriverException and message: |
| 114 | if message == 'No such context found.': |
| 115 | exception_class = appium_exceptions.NoSuchContextException |
| 116 | elif message == 'That command could not be executed in the current context.': |
| 117 | exception_class = appium_exceptions.InvalidSwitchToTargetException |
| 118 | |
| 119 | if exception_class is sel_exceptions.UnexpectedAlertPresentException: |
| 120 | raise sel_exceptions.UnexpectedAlertPresentException( |
| 121 | msg=message, |
| 122 | stacktrace=format_stacktrace(stacktrace), |
| 123 | alert_text=value.get('data'), |
| 124 | ) |
| 125 | raise exception_class(msg=message, stacktrace=format_stacktrace(stacktrace)) |