-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy patherror.go
402 lines (355 loc) · 13.5 KB
/
error.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2016 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package cli
import (
"context"
"crypto/x509"
"fmt"
"io"
"net"
"regexp"
"strconv"
"strings"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/util/grpcutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/netutil"
"github.com/cockroachdb/errors"
"github.com/lib/pq"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// cliOutputError prints out an error object on the given writer.
//
// It has a somewhat inconvenient set of requirements: it must make
// the error both palatable to a human user, which mandates some
// beautification, and still retain a few guarantees for automatic
// parsers (and a modicum of care for cross-compatibility across
// versions), including that of keeping the output relatively stable.
//
// As a result, future changes should be careful to properly balance
// changes made in favor of one audience with the needs and
// requirements of the other audience.
func cliOutputError(w io.Writer, err error, showSeverity, verbose bool) {
f := formattedError{err: err, showSeverity: showSeverity, verbose: verbose}
fmt.Fprintln(w, f.Error())
}
type formattedError struct {
err error
showSeverity, verbose bool
}
// Error implements the error interface.
func (f *formattedError) Error() string {
// If we're applying recursively, ignore what's there and display the original error.
// This happens when the shell reports an error for a second time.
var other *formattedError
if errors.As(f.err, &other) {
return other.Error()
}
var buf strings.Builder
// If the severity is missing, we're going to assume it's an error.
severity := "ERROR"
// Extract the fields.
var message, hint, detail, location string
var code pgcode.Code
if pqErr := (*pq.Error)(nil); errors.As(f.err, &pqErr) {
if pqErr.Severity != "" {
severity = pqErr.Severity
}
message = pqErr.Message
code = pgcode.MakeCode(string(pqErr.Code))
hint, detail = pqErr.Hint, pqErr.Detail
location = formatLocation(pqErr.File, pqErr.Line, pqErr.Routine)
} else {
message = f.err.Error()
code = pgerror.GetPGCode(f.err)
// Extract the standard hint and details.
hint = errors.FlattenHints(f.err)
detail = errors.FlattenDetails(f.err)
if file, line, fn, ok := errors.GetOneLineSource(f.err); ok {
location = formatLocation(file, strconv.FormatInt(int64(line), 10), fn)
}
}
// The order of the printing goes from most to less important.
if f.showSeverity && severity != "" {
fmt.Fprintf(&buf, "%s: ", severity)
}
fmt.Fprintln(&buf, message)
// Avoid printing the code for NOTICE, as the code is always 00000.
if severity != "NOTICE" && code.String() != "" {
// In contrast to `psql` we print the code even when printing
// non-verbosely, because we want to promote users reporting codes
// when interacting with support.
if code == pgcode.Uncategorized && !f.verbose {
// An exception is made for the "uncategorized" code, because we
// also don't want users to get the idea they can rely on XXUUU
// in their apps. That code is special, as we typically seek to
// replace it over time by something more specific.
//
// So in this case, if not printing verbosely, we don't display
// the code.
} else {
fmt.Fprintln(&buf, "SQLSTATE:", code)
}
}
if detail != "" {
fmt.Fprintln(&buf, "DETAIL:", detail)
}
if hint != "" {
fmt.Fprintln(&buf, "HINT:", hint)
}
if f.verbose && location != "" {
fmt.Fprintln(&buf, "LOCATION:", location)
}
// The code above is easier to read and write by stripping the
// extraneous newline at the end, than ensuring it's not there in
// the first place.
return strings.TrimRight(buf.String(), "\n")
}
// formatLocation spells out the error's location in a format
// similar to psql: routine then file:num. The routine part is
// skipped if empty.
func formatLocation(file, line, fn string) string {
var res strings.Builder
res.WriteString(fn)
if file != "" || line != "" {
if fn != "" {
res.WriteString(", ")
}
if file == "" {
res.WriteString("<unknown>")
} else {
res.WriteString(file)
}
if line != "" {
res.WriteByte(':')
res.WriteString(line)
}
}
return res.String()
}
// reConnRefused is a regular expression that can be applied
// to the details of a GRPC connection failure.
//
// On *nix, a connect error looks like:
// dial tcp <addr>: <syscall>: connection refused
// On Windows, it looks like:
// dial tcp <addr>: <syscall>: No connection could be made because the target machine actively refused it.
// So we look for the common bit.
var reGRPCConnRefused = regexp.MustCompile(`Error while dialing dial tcp .*: connection.* refused`)
// reGRPCNoTLS is a regular expression that can be applied to the
// details of a GRPC auth failure when the server is insecure.
var reGRPCNoTLS = regexp.MustCompile(`authentication handshake failed: tls: first record does not look like a TLS handshake`)
// reGRPCAuthFailure is a regular expression that can be applied to
// the details of a GRPC auth failure when the SSL handshake fails.
var reGRPCAuthFailure = regexp.MustCompile(`authentication handshake failed: x509`)
// reGRPCConnFailed is a regular expression that can be applied
// to the details of a GRPC connection failure when, perhaps,
// the server was expecting a TLS handshake but the client didn't
// provide one (i.e. the client was started with --insecure).
// Note however in that case it's not certain what the problem is,
// as the same error could be raised for other reasons.
var reGRPCConnFailed = regexp.MustCompile(`desc = (transport is closing|all SubConns are in TransientFailure)`)
// MaybeDecorateGRPCError catches grpc errors and provides a more helpful error
// message to the user.
func MaybeDecorateGRPCError(
wrapped func(*cobra.Command, []string) error,
) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
err = wrapped(cmd, args)
if err == nil {
return nil
}
defer func() {
// We want to flatten the error to reveal the hints, details etc.
// However we can't do it twice, so we need to detect first if
// some code already added the formattedError{} wrapper.
var f *formattedError
if !errors.As(err, &f) {
err = &formattedError{err: err, showSeverity: true}
}
}()
extraInsecureHint := func() string {
extra := ""
if baseCfg.Insecure {
extra = "\nIf the node is configured to require secure connections,\n" +
"remove --insecure and configure secure credentials instead.\n"
}
return extra
}
connFailed := func() error {
const format = "cannot dial server.\n" +
"Is the server running?\n" +
"If the server is running, check --host client-side and --advertise server-side.\n\n%v"
return errors.Errorf(format, err)
}
connSecurityHint := func() error {
const format = "SSL authentication error while connecting.\n%s\n%v"
return errors.Errorf(format, extraInsecureHint(), err)
}
connInsecureHint := func() error {
return errors.Errorf("cannot establish secure connection to insecure server.\n"+
"Maybe use --insecure?\n\n%v", err)
}
connRefused := func() error {
extra := extraInsecureHint()
return errors.Errorf("server closed the connection.\n"+
"Is this a CockroachDB node?\n%s\n%v", extra, err)
}
// Is this an "unable to connect" type of error?
if errors.Is(err, pq.ErrSSLNotSupported) {
// SQL command failed after establishing a TCP connection
// successfully, but discovering that it cannot use TLS while it
// expected the server supports TLS.
return connInsecureHint()
}
if wErr := (*security.Error)(nil); errors.As(err, &wErr) {
return errors.Errorf("cannot load certificates.\n"+
"Check your certificate settings, set --certs-dir, or use --insecure for insecure clusters.\n\n%v",
err)
}
if wErr := (*x509.UnknownAuthorityError)(nil); errors.As(err, &wErr) {
// A SQL connection was attempted with an incorrect CA.
return connSecurityHint()
}
if wErr := (*initialSQLConnectionError)(nil); errors.As(err, &wErr) {
// SQL handshake failed after establishing a TCP connection
// successfully, something else than CockroachDB responded, was
// confused and closed the door on us.
return connRefused()
}
if wErr := (*pq.Error)(nil); errors.As(err, &wErr) {
// SQL commands will fail with a pq error but only after
// establishing a TCP connection successfully. So if we got
// here, there was a TCP connection already.
// Did we fail due to security settings?
if pgcode.MakeCode(string(wErr.Code)) == pgcode.ProtocolViolation {
return connSecurityHint()
}
// Are we running a v20.2 binary against a v20.1 server?
if strings.Contains(wErr.Message, "column \"membership\" does not exist") {
// The v20.2 binary makes use of columns not present in v20.1,
// so this is a disallowed operation. Surface a better error
// code here.
return fmt.Errorf("cannot use a v20.2 cli against servers running v20.1")
}
// Otherwise, there was a regular SQL error. Just report
// that.
return err
}
if wErr := (*net.OpError)(nil); errors.As(err, &wErr) {
// A non-RPC client command was used (e.g. a SQL command) and an
// error occurred early while establishing the TCP connection.
// Is this a TLS error?
if msg := wErr.Err.Error(); strings.HasPrefix(msg, "tls: ") {
// Error during the SSL handshake: a provided client
// certificate was invalid, but the CA was OK. (If the CA was
// not OK, we'd get a x509 error, see case above.)
return connSecurityHint()
}
return connFailed()
}
if wErr := (*netutil.InitialHeartbeatFailedError)(nil); errors.As(err, &wErr) {
// A GRPC TCP connection was established but there was an early failure.
// Try to distinguish the cases.
msg := wErr.Error()
if reGRPCConnRefused.MatchString(msg) {
return connFailed()
}
if reGRPCNoTLS.MatchString(msg) {
return connInsecureHint()
}
if reGRPCAuthFailure.MatchString(msg) {
return connSecurityHint()
}
if reGRPCConnFailed.MatchString(msg) /* gRPC 1.21 */ ||
status.Code(errors.Cause(err)) == codes.Unavailable /* gRPC 1.27 */ {
return connRefused()
}
// Other cases may be timeouts or other situations, these
// will be handled below.
}
opTimeout := func() error {
return errors.Errorf("operation timed out.\n\n%v", err)
}
// Is it a plain context cancellation (i.e. timeout)?
if errors.IsAny(err,
context.DeadlineExceeded,
context.Canceled) {
return opTimeout()
}
// Is it a GRPC-observed context cancellation (i.e. timeout), a GRPC
// connection error, or a known indication of a too-old server?
if code := status.Code(errors.Cause(err)); code == codes.DeadlineExceeded {
return opTimeout()
} else if code == codes.Unimplemented &&
strings.Contains(err.Error(), "unknown method Decommission") ||
strings.Contains(err.Error(), "unknown service cockroach.server.serverpb.Init") {
return fmt.Errorf(
"incompatible client and server versions (likely server version: v1.0, required: >=v1.1)")
} else if grpcutil.IsClosedConnection(err) {
return errors.Errorf("connection lost.\n\n%v", err)
}
// Does the server require GSSAPI authentication?
if strings.Contains(err.Error(), "pq: unknown authentication response: 7") {
return fmt.Errorf(
"server requires GSSAPI authentication for this user.\n" +
"The CockroachDB CLI does not support GSSAPI authentication; use 'psql' instead")
}
// Are we trying to re-initialize an initialized cluster?
if strings.Contains(err.Error(), server.ErrClusterInitialized.Error()) {
// We really want to use errors.Is() here but this would require
// error serialization support in gRPC.
// This is not yet performed in CockroachDB even though the error
// library now has infrastructure to do so, see:
// https://github.com/cockroachdb/errors/pull/14
return server.ErrClusterInitialized
}
// Are we trying to recommission/decommision a node that does not exist?
if strings.Contains(err.Error(), kvserver.ErrMissingLivenessRecord.Error()) {
return fmt.Errorf("node does not exist")
}
// Nothing we can special case, just return what we have.
return err
}
}
// maybeShoutError calls log.Shout on errors, better surfacing problems to the user.
func maybeShoutError(
wrapped func(*cobra.Command, []string) error,
) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
err := wrapped(cmd, args)
return checkAndMaybeShout(err)
}
}
func checkAndMaybeShout(err error) error {
return checkAndMaybeShoutTo(err, log.Shoutf)
}
func checkAndMaybeShoutTo(
err error, logger func(context.Context, log.Severity, string, ...interface{}),
) error {
if err == nil {
return nil
}
severity := log.Severity_ERROR
cause := err
var ec *cliError
if errors.As(err, &ec) {
severity = ec.severity
cause = ec.cause
}
logger(context.Background(), severity, "%v", cause)
return err
}