Skip to content

Commit 0e696c8

Browse files
committedJul 2, 2024·
added CallStackPrintable
1 parent 8fde935 commit 0e696c8

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed
 

‎format.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ package errs
33
import (
44
"errors"
55
"fmt"
6+
"io"
67
"runtime"
78
"strings"
89

910
"github.com/domonda/go-pretty"
1011
)
1112

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+
1219
func formatError(err error) string {
1320
var (
1421
firstWithoutStack error
@@ -70,17 +77,25 @@ func formatCallStackParams(e callStackParamsProvider) string {
7077
}
7178

7279
// 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 {
7687
var b strings.Builder
7788
b.WriteString(function)
7889
b.WriteByte('(')
7990
for i, param := range params {
8091
if i > 0 {
8192
b.WriteString(", ")
8293
}
83-
pretty.Fprint(&b, param)
94+
if printable, ok := param.(CallStackPrintable); ok {
95+
printable.PrintForCallStack(&b)
96+
} else {
97+
pretty.Fprint(&b, param)
98+
}
8499
}
85100
b.WriteByte(')')
86101
return b.String()

‎go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/domonda/go-errs
33
go 1.22
44

55
require (
6-
github.com/domonda/go-pretty v0.0.0-20230810130018-8920f571470a
7-
github.com/stretchr/testify v1.8.4
6+
github.com/domonda/go-pretty v0.0.0-20240110134850-17385799142f
7+
github.com/stretchr/testify v1.9.0
88
)
99

1010
require (

‎go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/domonda/go-pretty v0.0.0-20230810130018-8920f571470a h1:b3a6MwwMrHR9dw6585e3Ky51T50OKuD3fRuLyh8ziEw=
4-
github.com/domonda/go-pretty v0.0.0-20230810130018-8920f571470a/go.mod h1:3QkM8UJdyJMeKZiIo7hYzSkQBpRS3k0gOHw4ysyEIB4=
3+
github.com/domonda/go-pretty v0.0.0-20240110134850-17385799142f h1:5eA74m451PqlqCXyJzWXp95Quj4PZ6Lm/ndKBuiNhe4=
4+
github.com/domonda/go-pretty v0.0.0-20240110134850-17385799142f/go.mod h1:3QkM8UJdyJMeKZiIo7hYzSkQBpRS3k0gOHw4ysyEIB4=
55
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
66
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7-
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
8-
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
8+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
99
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1010
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1111
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)
Please sign in to comment.