Skip to content

Commit

Permalink
added CallStackPrintable
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Jul 2, 2024
1 parent 8fde935 commit 0e696c8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
23 changes: 19 additions & 4 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ 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
Expand Down Expand Up @@ -70,17 +77,25 @@ func formatCallStackParams(e callStackParamsProvider) string {
}

// FormatFunctionCall formats a function call in pseudo syntax
// using github.com/domonda/go-pretty to format the params.
// Used to format errors with function call stack information.
func FormatFunctionCall(function string, params ...any) string {
// 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(", ")
}
pretty.Fprint(&b, param)
if printable, ok := param.(CallStackPrintable); ok {
printable.PrintForCallStack(&b)
} else {
pretty.Fprint(&b, param)
}
}
b.WriteByte(')')
return b.String()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/domonda/go-errs
go 1.22

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

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

0 comments on commit 0e696c8

Please sign in to comment.