@@ -3,12 +3,19 @@ package errs
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "io"
6
7
"runtime"
7
8
"strings"
8
9
9
10
"github.com/domonda/go-pretty"
10
11
)
11
12
13
+ // CallStackPrintable can be implemented to customize the printing
14
+ // of the implementation's data in an error call stack output.
15
+ type CallStackPrintable interface {
16
+ PrintForCallStack (io.Writer )
17
+ }
18
+
12
19
func formatError (err error ) string {
13
20
var (
14
21
firstWithoutStack error
@@ -70,17 +77,25 @@ func formatCallStackParams(e callStackParamsProvider) string {
70
77
}
71
78
72
79
// FormatFunctionCall formats a function call in pseudo syntax
73
- // using github.com/domonda/go-pretty to format the params.
74
- // Used to format errors with function call stack information.
75
- func FormatFunctionCall (function string , params ... any ) string {
80
+ // using the PrintForCallStack method of params that implement
81
+ // the CallStackPrintable interface or github.com/domonda/go-pretty
82
+ // to format params that don't implement CallStackPrintable.
83
+ //
84
+ // FormatFunctionCall is a function variable that can be changed
85
+ // to globally configure the formatting of function calls.
86
+ var FormatFunctionCall = func (function string , params ... any ) string {
76
87
var b strings.Builder
77
88
b .WriteString (function )
78
89
b .WriteByte ('(' )
79
90
for i , param := range params {
80
91
if i > 0 {
81
92
b .WriteString (", " )
82
93
}
83
- pretty .Fprint (& b , param )
94
+ if printable , ok := param .(CallStackPrintable ); ok {
95
+ printable .PrintForCallStack (& b )
96
+ } else {
97
+ pretty .Fprint (& b , param )
98
+ }
84
99
}
85
100
b .WriteByte (')' )
86
101
return b .String ()
0 commit comments