-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy patherrors.go
233 lines (194 loc) · 5.25 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
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
// Copyright 2020 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 errors
import (
"fmt"
"os/exec"
crdberrors "github.com/cockroachdb/errors"
)
// Error is an interface for error types used by the main.wrap() function
// to output correctly classified log messages and exit codes.
type Error interface {
error
// The exit code for the error when exiting roachprod.
ExitCode() int
}
// Exit codes for the errors
const (
cmdExitCode = 20
cockroachExitCode = 30
sshExitCode = 10
unclassifiedExitCode = 1
)
// Cmd wraps errors that result from a non-cockroach command run against
// the cluster.
//
// For errors coming from a cockroach command, use Cockroach.
type Cmd struct {
Err error
}
func (e Cmd) Error() string {
return fmt.Sprintf("COMMAND_PROBLEM: %s", e.Err.Error())
}
// ExitCode gives the process exit code to return for non-cockroach command
// errors.
func (e Cmd) ExitCode() int {
return cmdExitCode
}
// Format passes formatting responsibilities to cockroachdb/errors
func (e Cmd) Format(s fmt.State, verb rune) {
crdberrors.FormatError(e, s, verb)
}
// Unwrap the wrapped the non-cockroach command error.
func (e Cmd) Unwrap() error {
return e.Err
}
// Cockroach wraps errors that result from a cockroach command run against the cluster.
//
// For non-cockroach commands, use Cmd.
type Cockroach struct {
Err error
}
func (e Cockroach) Error() string {
return fmt.Sprintf("DEAD_ROACH_PROBLEM: %s", e.Err.Error())
}
// ExitCode gives the process exit code to return for cockroach errors.
func (e Cockroach) ExitCode() int {
return cockroachExitCode
}
// Format passes formatting responsibilities to cockroachdb/errors
func (e Cockroach) Format(s fmt.State, verb rune) {
crdberrors.FormatError(e, s, verb)
}
// Unwrap the wrapped cockroach error.
func (e Cockroach) Unwrap() error {
return e.Err
}
// SSH wraps ssh-specific errors from connections to remote hosts.
type SSH struct {
Err error
}
func (e SSH) Error() string {
return fmt.Sprintf("SSH_PROBLEM: %s", e.Err.Error())
}
// ExitCode gives the process exit code to return for SSH errors.
func (e SSH) ExitCode() int {
return sshExitCode
}
// Format passes formatting responsibilities to cockroachdb/errors
func (e SSH) Format(s fmt.State, verb rune) {
crdberrors.FormatError(e, s, verb)
}
// Unwrap the wrapped SSH error.
func (e SSH) Unwrap() error {
return e.Err
}
// Unclassified wraps roachprod and unclassified errors.
type Unclassified struct {
Err error
}
func (e Unclassified) Error() string {
return fmt.Sprintf("UNCLASSIFIED_PROBLEM: %s", e.Err.Error())
}
// ExitCode gives the process exit code to return for unclassified errors.
func (e Unclassified) ExitCode() int {
return unclassifiedExitCode
}
// Format passes formatting responsibilities to cockroachdb/errors
func (e Unclassified) Format(s fmt.State, verb rune) {
crdberrors.FormatError(e, s, verb)
}
// Unwrap the wrapped unclassified error.
func (e Unclassified) Unwrap() error {
return e.Err
}
// ClassifyCmdError classifies an error received while executing a
// non-cockroach command remotely over an ssh connection to the right Error
// type.
func ClassifyCmdError(err error) Error {
if err == nil {
return nil
}
if exitErr, ok := asExitError(err); ok {
if exitErr.ExitCode() == 255 {
return SSH{err}
}
return Cmd{err}
}
return Unclassified{err}
}
// ClassifyCockroachError classifies an error received while executing a
// cockroach command remotely over an ssh connection to the right Error type.
func ClassifyCockroachError(err error) Error {
if err == nil {
return nil
}
if exitErr, ok := asExitError(err); ok {
if exitErr.ExitCode() == 255 {
return SSH{err}
}
return Cockroach{err}
}
return Unclassified{err}
}
// Extract the an ExitError from err's error tree or (nil, false) if none exists.
func asExitError(err error) (*exec.ExitError, bool) {
if exitErr, ok := crdberrors.If(err, func(err error) (interface{}, bool) {
if err, ok := err.(*exec.ExitError); ok {
return err, true
}
return nil, false
}); ok {
return exitErr.(*exec.ExitError), true
}
return nil, false
}
// AsError extracts the Error from err's error tree or (nil, false) if none exists.
func AsError(err error) (Error, bool) {
if rpErr, ok := crdberrors.If(err, func(err error) (interface{}, bool) {
if rpErr, ok := err.(Error); ok {
return rpErr, true
}
return nil, false
}); ok {
return rpErr.(Error), true
}
return nil, false
}
// SelectPriorityError selects an error from the list in this priority order:
//
// - the Error with the highest exit code
// - one of the `error`s
// - nil
func SelectPriorityError(errors []error) error {
var result Error
for _, err := range errors {
if err == nil {
continue
}
rpErr, _ := AsError(err)
if result == nil {
result = rpErr
continue
}
if rpErr.ExitCode() > result.ExitCode() {
result = rpErr
}
}
if result != nil {
return result
}
for _, err := range errors {
if err != nil {
return err
}
}
return nil
}