-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy patherrors.go
164 lines (135 loc) · 3.38 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package ngrok
import (
"fmt"
"net/url"
"strings"
)
// Error is an error enriched with a specific ErrorCode.
// All ngrok error codes are documented at https://ngrok.com/docs/errors.
//
// An [Error] can be extracted from a generic error using [errors.As].
//
// Example:
//
// var nerr ngrok.Error
// if errors.As(err, &nerr) {
// fmt.Printf("%s: %s\n", nerr.ErrorCode(), nerr.Msg())
// }
type Error interface {
error
// Msg returns the error string without the error code.
Msg() string
// ErrorCode returns the ngrok error code, if one exists.
ErrorCode() string
}
// Errors arising from authentication failure.
type errAuthFailed struct {
// Whether the error was generated by the remote server, or in the sending
// of the authentication request.
Remote bool
// The underlying error.
Inner error
}
func (e errAuthFailed) Error() string {
var msg string
if e.Remote {
msg = "authentication failed"
} else {
msg = "failed to send authentication request"
}
return fmt.Sprintf("%s: %v", msg, e.Inner)
}
func (e errAuthFailed) Unwrap() error {
return e.Inner
}
func (e errAuthFailed) Is(target error) bool {
_, ok := target.(errAuthFailed)
return ok
}
// The error returned by [Tunnel]'s [net.Listener.Accept] method.
type errAcceptFailed struct {
// The underlying error.
Inner error
}
func (e errAcceptFailed) Error() string {
return fmt.Sprintf("failed to accept connection: %v", e.Inner)
}
func (e errAcceptFailed) Unwrap() error {
return e.Inner
}
func (e errAcceptFailed) Is(target error) bool {
_, ok := target.(errAcceptFailed)
return ok
}
// Errors arising from a failure to start a tunnel.
type errListen struct {
// The underlying error.
Inner error
}
func (e errListen) Error() string {
return fmt.Sprintf("failed to start tunnel: %v", e.Inner)
}
func (e errListen) Unwrap() error {
return e.Inner
}
func (e errListen) Is(target error) bool {
_, ok := target.(errListen)
return ok
}
// Errors arising from a failure to construct a [golang.org/x/net/proxy.Dialer] from a [url.URL].
type errProxyInit struct {
// The provided proxy URL.
URL *url.URL
// The underlying error.
Inner error
}
func (e errProxyInit) Error() string {
return fmt.Sprintf("failed to construct proxy dialer from \"%s\": %v", e.URL.String(), e.Inner)
}
func (e errProxyInit) Unwrap() error {
return e.Inner
}
func (e errProxyInit) Is(target error) bool {
_, ok := target.(errProxyInit)
return ok
}
// Error arising from a failure to dial the ngrok server.
type errSessionDial struct {
// The address to which a connection was attempted.
Addr string
// The underlying error.
Inner error
}
func (e errSessionDial) Error() string {
return fmt.Sprintf("failed to dial ngrok server with address \"%s\": %v", e.Addr, e.Inner)
}
func (e errSessionDial) Unwrap() error {
return e.Inner
}
func (e errSessionDial) Is(target error) bool {
_, ok := target.(errSessionDial)
return ok
}
// Generic ngrok error that requires no parsing
type ngrokError struct {
Message string
ErrCode string
}
const errUrl = "https://ngrok.com/docs/errors"
func (m ngrokError) Error() string {
out := m.Message
if m.ErrCode != "" {
out = fmt.Sprintf("%s\n\n%s/%s", out, errUrl, strings.ToLower(m.ErrCode))
}
return out
}
func (m ngrokError) Msg() string {
return m.Message
}
func (m ngrokError) ErrorCode() string {
return m.ErrCode
}
func (e ngrokError) Is(target error) bool {
_, ok := target.(ngrokError)
return ok
}