-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle.go
More file actions
104 lines (92 loc) · 2.62 KB
/
Copy pathhandle.go
File metadata and controls
104 lines (92 loc) · 2.62 KB
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
package errorutils
import (
"errors"
"io"
"os"
"github.com/sirupsen/logrus"
)
// HandleFailure handles an error by logging it and then calling the handler function
// If the handler function returns an error, it is logged as well
func HandleFailure(err error, handleFn Handler, o ...Option) (err2 *Details) {
dtls := New(err, o...)
if !isNilDetail(dtls) {
logrus.Error(dtls)
err2 = handleFn.Handle()
LogFailures(New(err2, o...))
} else if dtls.HasAltprint() {
logrus.Info(dtls.altPrint)
}
return err2
}
// Failure has been detected, log it
func LogFailures(err error, o ...Option) {
dtls := New(err, o...)
if !isNilDetail(dtls) {
logrus.Error(dtls)
} else if dtls.HasAltprint() {
logrus.Info(dtls.altPrint)
}
}
// Failure has been detected, log it
// formatted string should contain one '%s' for the error message
func LogFailuresf(err error, format string, o ...Option) {
dtls := New(err, o...)
if !isNilDetail(dtls) {
logrus.Errorf(format, dtls)
} else if dtls.HasAltprint() {
logrus.Info(dtls.altPrint)
}
}
func WarnOnFail(err error, o ...Option) {
dtls := New(err, o...)
if !isNilDetail(dtls) {
logrus.Warn(dtls)
} else if dtls.HasAltprint() {
logrus.Info(dtls.altPrint)
}
}
// formatted string should contain one '%s' for the error message
func WarnOnFailf(err error, format string, o ...Option) {
dtls := New(err, o...)
if !isNilDetail(dtls) {
logrus.Warnf(format, dtls)
} else if dtls.HasAltprint() {
logrus.Info(dtls.altPrint)
}
}
// irrecoverable programming error
func ExitOnFail(err error, o ...Option) {
dtls := New(err, o...)
if !isNilDetail(dtls) {
std := logrus.StandardLogger()
std.Log(logrus.FatalLevel, dtls)
os.Exit(dtls.ExitCode())
} else if dtls.HasAltprint() {
logrus.Info(dtls.altPrint)
}
}
// depriecated
// legacy panic, replace with ExitOnFail or use panic() instead does not support altPrint and can't handle detail nils
func PanicOnFail(err error, o ...Option) {
if err != nil {
optErr := New(err, o...)
std := logrus.StandardLogger()
std.Log(logrus.PanicLevel, optErr)
os.Exit(optErr.ExitCode())
}
}
// SafeClose closes a file and appends any errors to the error that a function is supposed to return
// https://wstrm.dev/posts/errors-join-heart-defer/
func SafeClose(file io.Closer, origErr *error) {
//avoid changing it to the dynamically alocated slice of errors errors.Join returns if possible
if cerr := file.Close(); cerr != nil {
*origErr = errors.Join(*origErr, cerr)
}
}
// NotifyClose visibilizes errors on defer for functions that do not return an error
func NotifyClose(file io.Closer) {
err := file.Close()
if err != nil {
LogFailures(err)
}
}