forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_codes.go
212 lines (194 loc) · 7.11 KB
/
error_codes.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2019 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package httpext
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/url"
"os"
"runtime"
"syscall"
"github.com/loadimpact/k6/lib/netext"
"github.com/pkg/errors"
"golang.org/x/net/http2"
)
// TODO: maybe rename the type errorCode, so we can have errCode variables? and
// also the constants would probably be better of if `ErrorCode` was a prefix,
// not a suffix - they would be much easier for auto-autocompletion at least...
type errCode uint32
const (
// non specific
defaultErrorCode errCode = 1000
defaultNetNonTCPErrorCode errCode = 1010
// DNS errors
defaultDNSErrorCode errCode = 1100
dnsNoSuchHostErrorCode errCode = 1101
blackListedIPErrorCode errCode = 1110
blockedHostnameErrorCode errCode = 1111
// tcp errors
defaultTCPErrorCode errCode = 1200
tcpBrokenPipeErrorCode errCode = 1201
netUnknownErrnoErrorCode errCode = 1202
tcpDialErrorCode errCode = 1210
tcpDialTimeoutErrorCode errCode = 1211
tcpDialRefusedErrorCode errCode = 1212
tcpDialUnknownErrnoCode errCode = 1213
tcpResetByPeerErrorCode errCode = 1220
// TLS errors
defaultTLSErrorCode errCode = 1300
x509UnknownAuthorityErrorCode errCode = 1310
x509HostnameErrorCode errCode = 1311
// HTTP2 errors
// defaultHTTP2ErrorCode errCode = 1600 // commented because of golint
// HTTP2 GoAway errors
unknownHTTP2GoAwayErrorCode errCode = 1610
// errors till 1611 + 13 are other HTTP2 GoAway errors with a specific errCode
// HTTP2 Stream errors
unknownHTTP2StreamErrorCode errCode = 1630
// errors till 1631 + 13 are other HTTP2 Stream errors with a specific errCode
// HTTP2 Connection errors
unknownHTTP2ConnectionErrorCode errCode = 1650
// errors till 1651 + 13 are other HTTP2 Connection errors with a specific errCode
// Custom k6 content errors, i.e. when the magic fails
//defaultContentError errCode = 1700 // reserved for future use
responseDecompressionErrorCode errCode = 1701
)
const (
tcpResetByPeerErrorCodeMsg = "write: connection reset by peer"
tcpDialTimeoutErrorCodeMsg = "dial: i/o timeout"
tcpDialRefusedErrorCodeMsg = "dial: connection refused"
tcpBrokenPipeErrorCodeMsg = "write: broken pipe"
netUnknownErrnoErrorCodeMsg = "%s: unknown errno `%d` on %s with message `%s`"
dnsNoSuchHostErrorCodeMsg = "lookup: no such host"
blackListedIPErrorCodeMsg = "ip is blacklisted"
blockedHostnameErrorMsg = "hostname is blocked"
http2GoAwayErrorCodeMsg = "http2: received GoAway with http2 ErrCode %s"
http2StreamErrorCodeMsg = "http2: stream error with http2 ErrCode %s"
http2ConnectionErrorCodeMsg = "http2: connection error with http2 ErrCode %s"
x509HostnameErrorCodeMsg = "x509: certificate doesn't match hostname"
x509UnknownAuthority = "x509: unknown authority"
)
func http2ErrCodeOffset(code http2.ErrCode) errCode {
if code > http2.ErrCodeHTTP11Required {
return 0
}
return 1 + errCode(code)
}
// errorCodeForError returns the errorCode and a specific error message for given error.
func errorCodeForError(err error) (errCode, string) {
switch e := errors.Cause(err).(type) {
case K6Error:
return e.Code, e.Message
case *net.DNSError:
switch e.Err {
case "no such host": // defined as private in the go stdlib
return dnsNoSuchHostErrorCode, dnsNoSuchHostErrorCodeMsg
default:
return defaultDNSErrorCode, err.Error()
}
case netext.BlackListedIPError:
return blackListedIPErrorCode, blackListedIPErrorCodeMsg
case netext.BlockedHostError:
return blockedHostnameErrorCode, blockedHostnameErrorMsg
case *http2.GoAwayError:
return unknownHTTP2GoAwayErrorCode + http2ErrCodeOffset(e.ErrCode),
fmt.Sprintf(http2GoAwayErrorCodeMsg, e.ErrCode)
case *http2.StreamError:
return unknownHTTP2StreamErrorCode + http2ErrCodeOffset(e.Code),
fmt.Sprintf(http2StreamErrorCodeMsg, e.Code)
case *http2.ConnectionError:
return unknownHTTP2ConnectionErrorCode + http2ErrCodeOffset(http2.ErrCode(*e)),
fmt.Sprintf(http2ConnectionErrorCodeMsg, http2.ErrCode(*e))
case *net.OpError:
if e.Net != "tcp" && e.Net != "tcp6" {
// TODO: figure out how this happens
return defaultNetNonTCPErrorCode, err.Error()
}
if e.Op == "write" {
if sErr, ok := e.Err.(*os.SyscallError); ok {
switch sErr.Err {
case syscall.ECONNRESET:
return tcpResetByPeerErrorCode, tcpResetByPeerErrorCodeMsg
case syscall.EPIPE:
return tcpBrokenPipeErrorCode, tcpBrokenPipeErrorCodeMsg
}
}
}
if e.Op == "dial" {
if e.Timeout() {
return tcpDialTimeoutErrorCode, tcpDialTimeoutErrorCodeMsg
}
if iErr, ok := e.Err.(*os.SyscallError); ok {
if errno, ok := iErr.Err.(syscall.Errno); ok {
if errno == syscall.ECONNREFUSED ||
// 10061 is some connection refused like thing on windows
// TODO: fix by moving to x/sys instead of syscall after
// https://github.com/golang/go/issues/31360 gets resolved
(errno == 10061 && runtime.GOOS == "windows") {
return tcpDialRefusedErrorCode, tcpDialRefusedErrorCodeMsg
}
return tcpDialUnknownErrnoCode,
fmt.Sprintf("dial: unknown errno %d error with msg `%s`", errno, iErr.Err)
}
}
return tcpDialErrorCode, err.Error()
}
switch inErr := e.Err.(type) {
case syscall.Errno:
return netUnknownErrnoErrorCode,
fmt.Sprintf(netUnknownErrnoErrorCodeMsg,
e.Op, (int)(inErr), runtime.GOOS, inErr.Error())
default:
return defaultTCPErrorCode, err.Error()
}
case *x509.UnknownAuthorityError:
return x509UnknownAuthorityErrorCode, x509UnknownAuthority
case *x509.HostnameError:
return x509HostnameErrorCode, x509HostnameErrorCodeMsg
case *tls.RecordHeaderError:
return defaultTLSErrorCode, err.Error()
case *url.Error:
return errorCodeForError(e.Err)
default:
return defaultErrorCode, err.Error()
}
}
// K6Error is a helper struct that enhances Go errors with custom k6-specific
// error-codes and more user-readable error messages.
type K6Error struct {
Code errCode
Message string
OriginalError error
}
// NewK6Error is the constructor for K6Error
func NewK6Error(code errCode, msg string, originalErr error) K6Error {
return K6Error{code, msg, originalErr}
}
// Error implements the `error` interface, so K6Errors are normal Go errors.
func (k6Err K6Error) Error() string {
return k6Err.Message
}
// Unwrap implements the `xerrors.Wrapper` interface, so K6Errors are a bit
// future-proof Go 2 errors.
func (k6Err K6Error) Unwrap() error {
return k6Err.OriginalError
}