-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
util.go
61 lines (55 loc) · 1.41 KB
/
util.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 protocol
import (
"errors"
"fmt"
"runtime"
"strings"
)
// chainErr chains together a variadic amount of errors into a single error and returns it. If all errors
// passed are nil, the error returned will also be nil.
func chainErr(err ...error) error {
var msg string
hasEOF := true
for _, e := range err {
if e == nil {
continue
}
if strings.Contains(msg, "EOF") {
if hasEOF {
// No need to log multiple EOFs.
continue
}
hasEOF = true
}
msg += wrap(e).Error() + "\n"
}
if msg == "" {
return nil
}
return errors.New(strings.TrimRight(msg, "\n"))
}
// callFrame obtains a call frame and formats it so that it includes the file, function and line.
func callFrame() string {
targetFrameIndex := 3
programCounters := make([]uintptr, targetFrameIndex+2)
n := runtime.Callers(0, programCounters)
frame := runtime.Frame{Function: "unknown"}
if n > 0 {
frames := runtime.CallersFrames(programCounters[:n])
for more, frameIndex := true, 0; more && frameIndex <= targetFrameIndex; frameIndex++ {
var frameCandidate runtime.Frame
frameCandidate, more = frames.Next()
if frameIndex == targetFrameIndex {
frame = frameCandidate
}
}
}
return fmt.Sprintf("%v/%v", frame.Function, frame.Line)
}
// wrap wraps a call frame around an error and returns the new error.
func wrap(e error) error {
if e == nil {
return nil
}
return fmt.Errorf("%v: %v", callFrame(), e)
}