-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.go
109 lines (95 loc) · 2.62 KB
/
format.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
package errs
import (
"errors"
"fmt"
"io"
"runtime"
"strings"
"github.com/domonda/go-pretty"
)
// CallStackPrintable can be implemented to customize the printing
// of the implementation's data in an error call stack output.
type CallStackPrintable interface {
PrintForCallStack(io.Writer)
}
func formatError(err error) string {
var (
firstWithoutStack error
calls []string
)
for err != nil {
switch e := err.(type) {
case callStackParamsProvider:
calls = append(calls, formatCallStackParams(e))
case callStackProvider:
calls = append(calls, formatCallStack(e))
default:
if firstWithoutStack == nil {
firstWithoutStack = err
}
}
err = errors.Unwrap(err)
}
if firstWithoutStack == nil {
// Should never happen, just to make sure we don't panic
firstWithoutStack = errors.New("no wrapped error found")
}
var b strings.Builder
b.WriteString(firstWithoutStack.Error()) //#nosec
b.WriteByte('\n') //#nosec
for i := len(calls) - 1; i >= 0; i-- {
b.WriteString(calls[i]) //#nosec
b.WriteByte('\n') //#nosec
}
return b.String()
}
func formatCallStack(e callStackProvider) string {
stack := e.CallStack()
frame, _ := runtime.CallersFrames(stack).Next()
return fmt.Sprintf(
"%s\n %s:%d",
frame.Function,
strings.TrimPrefix(frame.File, TrimFilePathPrefix),
frame.Line,
)
}
func formatCallStackParams(e callStackParamsProvider) string {
stack, params := e.CallStackParams()
frame, _ := runtime.CallersFrames(stack).Next()
return fmt.Sprintf(
"%s\n %s:%d",
FormatFunctionCall(frame.Function, params...),
strings.TrimPrefix(frame.File, TrimFilePathPrefix),
frame.Line,
)
}
// FormatFunctionCall formats a function call in pseudo syntax
// using the PrintForCallStack method of params that implement
// the CallStackPrintable interface or github.com/domonda/go-pretty
// to format params that don't implement CallStackPrintable.
//
// FormatFunctionCall is a function variable that can be changed
// to globally configure the formatting of function calls.
var FormatFunctionCall = func(function string, params ...any) string {
var b strings.Builder
b.WriteString(function)
b.WriteByte('(')
for i, param := range params {
if i > 0 {
b.WriteString(", ")
}
if printable, ok := param.(CallStackPrintable); ok {
printable.PrintForCallStack(&b)
} else {
pretty.Fprint(&b, param)
}
}
b.WriteByte(')')
return b.String()
}
// LogFunctionCall using FormatFunctionCall if logger is not nil
func LogFunctionCall(logger Logger, function string, params ...any) {
if logger != nil {
logger.Printf(FormatFunctionCall(function, params...))
}
}