errors.py (5036B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import traceback 6 7 8 class MarionetteException(Exception): 9 """Raised when a generic non-recoverable exception has occured.""" 10 11 status = "webdriver error" 12 13 def __init__(self, message=None, cause=None, stacktrace=None): 14 """Construct new MarionetteException instance. 15 16 :param message: An optional exception message. 17 18 :param cause: An optional tuple of three values giving 19 information about the root exception cause. Expected 20 tuple values are (type, value, traceback). 21 22 :param stacktrace: Optional string containing a stacktrace 23 (typically from a failed JavaScript execution) that will 24 be displayed in the exception's string representation. 25 26 """ 27 self.cause = cause 28 self.stacktrace = stacktrace 29 self._message = str(message) 30 31 def __str__(self): 32 # pylint: disable=W1645 33 msg = self.message 34 tb = None 35 36 if self.cause: 37 if type(self.cause) is tuple: 38 msg += f", caused by {self.cause[0]!r}" 39 tb = self.cause[2] 40 else: 41 msg += f", caused by {self.cause}" 42 43 if self.stacktrace: 44 st = "".join([f"\t{x}\n" for x in self.stacktrace.splitlines()]) 45 msg += f"\nstacktrace:\n{st}" 46 47 if tb: 48 msg += ": " + "".join(traceback.format_tb(tb)) 49 50 return str(msg) 51 52 @property 53 def message(self): 54 return self._message 55 56 57 class DetachedShadowRootException(MarionetteException): 58 status = "detached shadow root" 59 60 61 class ElementNotSelectableException(MarionetteException): 62 status = "element not selectable" 63 64 65 class ElementClickInterceptedException(MarionetteException): 66 status = "element click intercepted" 67 68 69 class InsecureCertificateException(MarionetteException): 70 status = "insecure certificate" 71 72 73 class InvalidArgumentException(MarionetteException): 74 status = "invalid argument" 75 76 77 class InvalidSessionIdException(MarionetteException): 78 status = "invalid session id" 79 80 81 class TimeoutException(MarionetteException): 82 status = "timeout" 83 84 85 class JavascriptException(MarionetteException): 86 status = "javascript error" 87 88 89 class NoSuchElementException(MarionetteException): 90 status = "no such element" 91 92 93 class NoSuchShadowRootException(MarionetteException): 94 status = "no such shadow root" 95 96 97 class NoSuchWindowException(MarionetteException): 98 status = "no such window" 99 100 101 class StaleElementException(MarionetteException): 102 status = "stale element reference" 103 104 105 class ScriptTimeoutException(MarionetteException): 106 status = "script timeout" 107 108 109 class ElementNotVisibleException(MarionetteException): 110 """Deprecated. Will be removed with the release of Firefox 54.""" 111 112 status = "element not visible" 113 114 def __init__( 115 self, 116 message="Element is not currently visible and may not be manipulated", 117 stacktrace=None, 118 cause=None, 119 ): 120 super().__init__(message, cause=cause, stacktrace=stacktrace) 121 122 123 class ElementNotAccessibleException(MarionetteException): 124 status = "element not accessible" 125 126 127 class ElementNotInteractableException(MarionetteException): 128 status = "element not interactable" 129 130 131 class NoSuchFrameException(MarionetteException): 132 status = "no such frame" 133 134 135 class InvalidElementStateException(MarionetteException): 136 status = "invalid element state" 137 138 139 class NoAlertPresentException(MarionetteException): 140 status = "no such alert" 141 142 143 class InvalidCookieDomainException(MarionetteException): 144 status = "invalid cookie domain" 145 146 147 class UnableToSetCookieException(MarionetteException): 148 status = "unable to set cookie" 149 150 151 class InvalidElementCoordinates(MarionetteException): 152 status = "invalid element coordinates" 153 154 155 class InvalidSelectorException(MarionetteException): 156 status = "invalid selector" 157 158 159 class MoveTargetOutOfBoundsException(MarionetteException): 160 status = "move target out of bounds" 161 162 163 class SessionNotCreatedException(MarionetteException): 164 status = "session not created" 165 166 167 class UnexpectedAlertOpen(MarionetteException): 168 status = "unexpected alert open" 169 170 171 class UnknownCommandException(MarionetteException): 172 status = "unknown command" 173 174 175 class UnknownException(MarionetteException): 176 status = "unknown error" 177 178 179 class UnsupportedOperationException(MarionetteException): 180 status = "unsupported operation" 181 182 183 class UnresponsiveInstanceException(Exception): 184 pass 185 186 187 es_ = [ 188 e 189 for e in locals().values() 190 if type(e) is type and issubclass(e, MarionetteException) 191 ] 192 by_string = {e.status: e for e in es_} 193 194 195 def lookup(identifier): 196 """Finds error exception class by associated Selenium JSON wire 197 protocol number code, or W3C WebDriver protocol string. 198 199 """ 200 return by_string.get(identifier, MarionetteException)