-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
errrors.go
61 lines (53 loc) · 1.61 KB
/
errrors.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
package utils
import (
"os"
"runtime/debug"
log "github.com/sirupsen/logrus"
)
const (
// ErrorCommandSpecific is reserved for command specific indications
ErrorCommandSpecific = 1
// ErrorConnectionFailure is returned on connection failure to API endpoint
ErrorConnectionFailure = 11
// ErrorAPIResponse is returned on unexpected API response, i.e. authorization failure
ErrorAPIResponse = 12
// ErrorResourceDoesNotExist is returned when the requested resource does not exist
ErrorResourceDoesNotExist = 13
// ErrorGeneric is returned for generic error
ErrorGeneric = 20
)
// CheckError logs a fatal message and exits with ErrorGeneric if err is not nil
func CheckError(err error) {
if err != nil {
debug.PrintStack()
Fatal(ErrorGeneric, err)
}
}
// CheckErrorWithCode is a convenience function to exit if an error is non-nil and exit if it was
func CheckErrorWithCode(err error, exitcode int) {
if err != nil {
Fatal(exitcode, err)
}
}
// FailOnErr panics if there is an error. It returns the first value so you can use it if you cast it:
// text := FailOrErr(Foo)).(string)
func FailOnErr(v interface{}, err error) interface{} {
CheckError(err)
return v
}
// Fatal is a wrapper for logrus.Fatal() to exit with custom code
func Fatal(exitcode int, args ...interface{}) {
exitfunc := func() {
os.Exit(exitcode)
}
log.RegisterExitHandler(exitfunc)
log.Fatal(args...)
}
// Fatalf is a wrapper for logrus.Fatalf() to exit with custom code
func Fatalf(exitcode int, format string, args ...interface{}) {
exitfunc := func() {
os.Exit(exitcode)
}
log.RegisterExitHandler(exitfunc)
log.Fatalf(format, args...)
}