summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/tools/webdriver/webdriver/bidi/error.py
blob: 42361ca90b91caf1b018b42b2c44ebd1985b4cf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# mypy: allow-untyped-defs

import collections

from typing import ClassVar, DefaultDict, Optional, Type


class BidiException(Exception):
    # The error_code class variable is used to map the JSON Error Code (see
    # https://w3c.github.io/webdriver/#errors) to a BidiException subclass.
    # TODO: Match on error and let it be a class variables only.
    error_code: ClassVar[str]

    def __init__(self, message: str, stacktrace: Optional[str] = None):
        super()

        self.message = message
        self.stacktrace = stacktrace

    def __repr__(self):
        """Return the object representation in string format."""
        return f"{self.__class__.__name__}({self.error}, {self.message}, {self.stacktrace})"

    def __str__(self):
        """Return the string representation of the object."""
        message = f"{self.error_code} ({self.message})"

        if self.stacktrace is not None:
            message += f"\n\nRemote-end stacktrace:\n\n{self.stacktrace}"

        return message


class InvalidArgumentException(BidiException):
    error_code = "invalid argument"


class InvalidSelectorException(BidiException):
    error_code = "invalid selector"


class InvalidSessionIDError(BidiException):
    error_code = "invalid session id"


class MoveTargetOutOfBoundsException(BidiException):
    error_code = "move target out of bounds"


class NoSuchAlertException(BidiException):
    error_code = "no such alert"


class NoSuchElementException(BidiException):
    error_code = "no such element"


class NoSuchFrameException(BidiException):
    error_code = "no such frame"


class NoSuchInterceptException(BidiException):
    error_code = "no such intercept"


class NoSuchHandleException(BidiException):
    error_code = "no such handle"


class NoSuchHistoryEntryException(BidiException):
    error_code = "no such history entry"


class NoSuchNodeException(BidiException):
    error_code = "no such node"


class NoSuchRequestException(BidiException):
    error_code = "no such request"


class NoSuchScriptException(BidiException):
    error_code = "no such script"


class NoSuchUserContextException(BidiException):
    error_code = "no such user context"


class UnableToCaptureScreenException(BidiException):
    error_code = "unable to capture screen"


class UnableToSetCookieException(BidiException):
    error_code = "unable to set cookie"


class UnderspecifiedStoragePartitionException(BidiException):
    error_code = "underspecified storage partition"


class UnknownCommandException(BidiException):
    error_code = "unknown command"


class UnknownErrorException(BidiException):
    error_code = "unknown error"


class UnsupportedOperationException(BidiException):
    error_code = "unsupported operation"


def from_error_details(error: str, message: str, stacktrace: Optional[str]) -> BidiException:
    """Create specific WebDriver BiDi exception class from error details.

    Defaults to ``UnknownErrorException`` if `error` is unknown.
    """
    cls = get(error)
    return cls(message, stacktrace)


def get(error_code: str) -> Type[BidiException]:
    """Get exception from `error_code`.

    It's falling back to ``UnknownErrorException`` if it is not found.
    """
    return _errors.get(error_code, UnknownErrorException)


_errors: DefaultDict[str, Type[BidiException]] = collections.defaultdict()
for item in list(locals().values()):
    if isinstance(item, type) and item != BidiException and issubclass(item, BidiException):
        _errors[item.error_code] = item