vendor/github.com/go-rod/rod/error.go
193 lines · 4554 bytes
1package rod
2
3import (
4 "context"
5 "fmt"
6
7 "github.com/go-rod/rod/lib/proto"
8 "github.com/go-rod/rod/lib/utils"
9)
10
11// TryError error.
12type TryError struct {
13 Value interface{}
14 Stack string
15}
16
17func (e *TryError) Error() string {
18 return fmt.Sprintf("error value: %#v\n%s", e.Value, e.Stack)
19}
20
21// Is interface.
22func (e *TryError) Is(err error) bool { _, ok := err.(*TryError); return ok }
23
24// Unwrap stdlib interface.
25func (e *TryError) Unwrap() error {
26 if err, ok := e.Value.(error); ok {
27 return err
28 }
29 return fmt.Errorf("%v", e.Value)
30}
31
32// ExpectElementError error.
33type ExpectElementError struct {
34 *proto.RuntimeRemoteObject
35}
36
37func (e *ExpectElementError) Error() string {
38 return fmt.Sprintf("expect js to return an element, but got: %s", utils.MustToJSON(e))
39}
40
41// Is interface.
42func (e *ExpectElementError) Is(err error) bool { _, ok := err.(*ExpectElementError); return ok }
43
44// ExpectElementsError error.
45type ExpectElementsError struct {
46 *proto.RuntimeRemoteObject
47}
48
49func (e *ExpectElementsError) Error() string {
50 return fmt.Sprintf("expect js to return an array of elements, but got: %s", utils.MustToJSON(e))
51}
52
53// Is interface.
54func (e *ExpectElementsError) Is(err error) bool { _, ok := err.(*ExpectElementsError); return ok }
55
56// ElementNotFoundError error.
57type ElementNotFoundError struct{}
58
59func (e *ElementNotFoundError) Error() string {
60 return "cannot find element"
61}
62
63// NotFoundSleeper returns ErrElementNotFound on the first call.
64func NotFoundSleeper() utils.Sleeper {
65 return func(context.Context) error {
66 return &ElementNotFoundError{}
67 }
68}
69
70// ObjectNotFoundError error.
71type ObjectNotFoundError struct {
72 *proto.RuntimeRemoteObject
73}
74
75func (e *ObjectNotFoundError) Error() string {
76 return fmt.Sprintf("cannot find object: %s", utils.MustToJSON(e))
77}
78
79// Is interface.
80func (e *ObjectNotFoundError) Is(err error) bool { _, ok := err.(*ObjectNotFoundError); return ok }
81
82// EvalError error.
83type EvalError struct {
84 *proto.RuntimeExceptionDetails
85}
86
87func (e *EvalError) Error() string {
88 exp := e.Exception
89 return fmt.Sprintf("eval js error: %s %s", exp.Description, exp.Value)
90}
91
92// Is interface.
93func (e *EvalError) Is(err error) bool { _, ok := err.(*EvalError); return ok }
94
95// NavigationError error.
96type NavigationError struct {
97 Reason string
98}
99
100func (e *NavigationError) Error() string {
101 return "navigation failed: " + e.Reason
102}
103
104// Is interface.
105func (e *NavigationError) Is(err error) bool { _, ok := err.(*NavigationError); return ok }
106
107// PageCloseCanceledError error.
108type PageCloseCanceledError struct{}
109
110func (e *PageCloseCanceledError) Error() string {
111 return "page close canceled"
112}
113
114// NotInteractableError error. Check the doc of Element.Interactable for details.
115type NotInteractableError struct{}
116
117func (e *NotInteractableError) Error() string {
118 return "element is not cursor interactable"
119}
120
121// InvisibleShapeError error.
122type InvisibleShapeError struct {
123 *Element
124}
125
126// Error ...
127func (e *InvisibleShapeError) Error() string {
128 return fmt.Sprintf("element has no visible shape or outside the viewport: %s", e.String())
129}
130
131// Is interface.
132func (e *InvisibleShapeError) Is(err error) bool { _, ok := err.(*InvisibleShapeError); return ok }
133
134// Unwrap ...
135func (e *InvisibleShapeError) Unwrap() error {
136 return &NotInteractableError{}
137}
138
139// CoveredError error.
140type CoveredError struct {
141 *Element
142}
143
144// Error ...
145func (e *CoveredError) Error() string {
146 return fmt.Sprintf("element covered by: %s", e.String())
147}
148
149// Unwrap ...
150func (e *CoveredError) Unwrap() error {
151 return &NotInteractableError{}
152}
153
154// Is interface.
155func (e *CoveredError) Is(err error) bool { _, ok := err.(*CoveredError); return ok }
156
157// NoPointerEventsError error.
158type NoPointerEventsError struct {
159 *Element
160}
161
162// Error ...
163func (e *NoPointerEventsError) Error() string {
164 return fmt.Sprintf("element's pointer-events is none: %s", e.String())
165}
166
167// Unwrap ...
168func (e *NoPointerEventsError) Unwrap() error {
169 return &NotInteractableError{}
170}
171
172// Is interface.
173func (e *NoPointerEventsError) Is(err error) bool { _, ok := err.(*NoPointerEventsError); return ok }
174
175// PageNotFoundError error.
176type PageNotFoundError struct{}
177
178func (e *PageNotFoundError) Error() string {
179 return "cannot find page"
180}
181
182// NoShadowRootError error.
183type NoShadowRootError struct {
184 *Element
185}
186
187// Error ...
188func (e *NoShadowRootError) Error() string {
189 return fmt.Sprintf("element has no shadow root: %s", e.String())
190}
191
192// Is interface.
193func (e *NoShadowRootError) Is(err error) bool { _, ok := err.(*NoShadowRootError); return ok }