Skip to content

Commit

Permalink
funcArgsDetail
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jun 6, 2020
1 parent bf0add7 commit 685447d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
35 changes: 35 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"reflect"
"strconv"
)

Expand Down Expand Up @@ -69,9 +70,43 @@ func errorDetail(b []byte, p *Frame) []byte {
}

func funcArgsDetail(b []byte, args []interface{}) []byte {
nlast := len(args) - 1
for i, arg := range args {
b = appendValue(b, arg)
if i != nlast {
b = append(b, ',', ' ')
}
}
return b
}

func appendValue(b []byte, arg interface{}) []byte {
if arg == nil {
return append(b, "nil"...)
}
v := reflect.ValueOf(arg)
kind := v.Kind()
if kind >= reflect.Bool && kind <= reflect.Complex128 {
return append(b, fmt.Sprint(arg)...)
}
if kind == reflect.String {
val := arg.(string)
if len(val) > 16 {
val = val[:16] + "..."
}
return strconv.AppendQuote(b, val)
}
if kind == reflect.Array {
return append(b, "Array"...)
}
if kind == reflect.Struct {
return append(b, "Struct"...)
}
val := v.Pointer()
b = append(b, '0', 'x')
return strconv.AppendInt(b, int64(val), 16)
}

// Unwrap provides compatibility for Go 1.13 error chains.
func (p *Frame) Unwrap() error {
return p.Err
Expand Down
11 changes: 8 additions & 3 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const (
s1Exp = `not found
===> errors stack:
github.com/qiniu/x/errors.TestFrame()
github.com/qiniu/x/errors.TestFrame("abcdefghijklmnop...", nil, 123, Array, Struct)
/Users/xsw/qiniu/x/errors/errors_test.go:11 errNotFound := New("not found")
github.com/qiniu/x/errors.TestFrame()
/Users/xsw/qiniu/x/errors/errors_test.go:12 err1 := Frame(errNotFound, ...)
Expand All @@ -22,8 +22,13 @@ github.com/qiniu/x/errors.TestFrame()
func TestFrame(t *testing.T) {
file := "/Users/xsw/qiniu/x/errors/errors_test.go"
errNotFound := New("not found")
err1 := NewFrame(errNotFound, `errNotFound := New("not found")`, file, 11, "github.com/qiniu/x/errors", "TestFrame", t)
err2 := NewFrame(err1, `err1 := Frame(errNotFound, ...)`, file, 12, "github.com/qiniu/x/errors", "TestFrame", t)
arg1 := "abcdefghijklmnopqrstuvwxyz"
arg2 := interface{}(nil)
arg3 := 123
arg4 := [...]int{1}
arg5 := struct{}{}
err1 := NewFrame(errNotFound, `errNotFound := New("not found")`, file, 11, "github.com/qiniu/x/errors", "TestFrame", arg1, arg2, arg3, arg4, arg5)
err2 := NewFrame(err1, `err1 := Frame(errNotFound, ...)`, file, 12, "github.com/qiniu/x/errors", "TestFrame")
s1 := fmt.Sprint(err2)
fmt.Println(s1)
s2 := fmt.Sprintf("%s", err2)
Expand Down

0 comments on commit 685447d

Please sign in to comment.