-
Notifications
You must be signed in to change notification settings - Fork 21
/
errors.go
executable file
·144 lines (120 loc) · 3.65 KB
/
errors.go
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
135
136
137
138
139
140
141
142
143
144
package goselenium
import "fmt"
// ErrorResponse is what is returned from the Selenium API when an error
// occurs.
type ErrorResponse struct {
Message string
State string
}
// CommunicationError is the result of a communication failure between
// this library and the WebDriver API.
type CommunicationError struct {
url string
Response *ErrorResponse
method string
}
// Error returns a formatted communication error string.
func (c CommunicationError) Error() string {
return fmt.Sprintf("%s: api error, url: %s, err: %+v", c.method, c.url, c.Response)
}
// IsCommunicationError checks whether an error is a selenium communication
// error.
func IsCommunicationError(err error) bool {
_, ok := err.(CommunicationError)
return ok
}
func newCommunicationError(err error, method string, url string, resp []byte) CommunicationError {
var convertedResponse ErrorResponse
reqErr, ok := err.(*requestError)
if ok {
convertedResponse = ErrorResponse{
Message: reqErr.Value.Message,
State: reqErr.State,
}
}
return CommunicationError{
url: url,
Response: &convertedResponse,
method: method,
}
}
// UnmarshallingError is the result of an unmarshalling failure of a JSON
// string.
type UnmarshallingError struct {
err error
json string
method string
}
// Error returns a formatted unmarshalling error string.
func (u UnmarshallingError) Error() string {
return fmt.Sprintf("%s: unmarshalling error, json: %s, err: %s", u.method, u.json, u.err)
}
// IsUnmarshallingError checks whether an error is a selenium unmarshalling
// error.
func IsUnmarshallingError(err error) bool {
_, ok := err.(UnmarshallingError)
return ok
}
func newUnmarshallingError(err error, method string, json string) UnmarshallingError {
return UnmarshallingError{
err: err,
json: json,
method: method,
}
}
// MarshallingError is an error that is returned when a json.Marshal error occurs.
type MarshallingError struct {
err error
object interface{}
method string
}
// Error returns a formatted marshalling error string.
func (m MarshallingError) Error() string {
return fmt.Sprintf("%s: marshalling error for object %+v, err: %s", m.method, m.object, m.err.Error())
}
// IsMarshallingError checks whether an error is a marshalling error.
func IsMarshallingError(err error) bool {
_, ok := err.(MarshallingError)
return ok
}
func newMarshallingError(err error, method string, obj interface{}) MarshallingError {
return MarshallingError{
err: err,
object: obj,
method: method,
}
}
// SessionIDError is an error that is returned when the session id is
// invalid. This value will contain the method that the session error occurred
// in.
type SessionIDError string
// Error returns a formatted session error string.
func (s SessionIDError) Error() string {
return fmt.Sprintf("%s: session id is invalid (have you created a session yet?)", string(s))
}
// IsSessionIDError checks whether an error is due to a session ID not being
// set.
func IsSessionIDError(err error) bool {
_, ok := err.(SessionIDError)
return ok
}
func newSessionIDError(method string) SessionIDError {
return SessionIDError(method)
}
// InvalidURLError is an error that is returned whenever a URL is not correctly
// formatted.
type InvalidURLError string
// Error returns the formatted invalid error string.
func (i InvalidURLError) Error() string {
return fmt.Sprintf("invalid url: %s", string(i))
}
// IsInvalidURLError checks whether an error is due to the URL being incorrectly
// formatted.
func IsInvalidURLError(err error) bool {
_, ok := err.(InvalidURLError)
return ok
}
// InvalidURLError
func newInvalidURLError(url string) InvalidURLError {
return InvalidURLError(url)
}