Skip to content

Commit 0cf36c4

Browse files
committed
fix: add (*statz.statusError).Format() for stacktrace
1 parent f81cbf8 commit 0cf36c4

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

errors/fmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ loop:
176176
e.Format(s, verb)
177177
break loop
178178
default:
179-
fmt.Fprint(s, e)
179+
fmt.Fprintf(s, fmt.FormatString(s, verb), e)
180180
break loop
181181
}
182182
if err == nil {

grpc/status/status.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ var (
6464
DefaultLogger Logger = func(_ context.Context, level Level, stat *status.Status, err error) {
6565
log.Printf("level=%s code=%s message=%q details=%s error=%q stacktrace=%q", level, stat.Code(), stat.Message(), stat.Details(), fmt.Sprintf("%v", err), fmt.Sprintf("%+v", err))
6666
}
67-
errorf = errorz.NewErrorf(errorz.WithCallerSkip(1))
67+
_ interface{ GRPCStatus() *status.Status } = (*statusError)(nil)
68+
errorf = errorz.NewErrorf(errorz.WithCallerSkip(1))
6869
)
6970

7071
func New(ctx context.Context, level Level, code codes.Code, msg string, err error, opts ...ErrorOption) error {
@@ -78,7 +79,16 @@ func New(ctx context.Context, level Level, code codes.Code, msg string, err erro
7879
opt.apply(e)
7980
}
8081
e.logger(ctx, e.level, e.s, err)
81-
return errorf("%s: %s", msg, err)
82+
return e
83+
}
84+
85+
func (e *statusError) Format(s fmt.State, verb rune) {
86+
if formatter, ok := e.error.(fmt.Formatter); ok { //nolint:errorlint
87+
formatter.Format(s, verb)
88+
return
89+
}
90+
91+
_, _ = fmt.Fprintf(s, fmt.FormatString(s, verb), e.error)
8292
}
8393

8494
func (e *statusError) GRPCStatus() *status.Status {

grpc/status/status_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package statz_test
1+
package statz
22

33
import (
44
"context"
5+
"errors"
56
"io"
67
"testing"
78

89
"google.golang.org/genproto/googleapis/rpc/errdetails"
910
"google.golang.org/grpc/codes"
11+
"google.golang.org/grpc/status"
1012

1113
errorz "github.com/kunitsucom/util.go/errors"
12-
statz "github.com/kunitsucom/util.go/grpc/status"
1314
)
1415

1516
func TestNew(t *testing.T) {
@@ -20,9 +21,21 @@ func TestNew(t *testing.T) {
2021

2122
ctx := context.Background()
2223
before := errorz.Errorf("err: %w", io.ErrUnexpectedEOF)
23-
err := statz.New(ctx, statz.ErrorLevel, codes.Internal, "my error message", before, statz.WithDetails(&errdetails.DebugInfo{StackEntries: []string{"stack1", "stack2"}}), statz.WithDetails(nil), statz.WithLogger(statz.DefaultLogger))
24+
err := New(ctx, ErrorLevel, codes.Internal, "my error message", before, WithDetails(&errdetails.DebugInfo{StackEntries: []string{"stack1", "stack2"}}), WithDetails(nil), WithLogger(DefaultLogger))
2425
t.Logf("🪲: [%%v]:\n%v", err)
2526
t.Logf("🪲: [%%+v]:\n%+v", err)
26-
_ = statz.New(ctx, statz.ErrorLevel, codes.Internal, "my error message", before, statz.WithLogger(statz.DiscardLogger))
27+
28+
var e *statusError
29+
if !errors.As(err, &e) {
30+
t.Errorf("❌: something wrong")
31+
}
32+
e.error = io.ErrUnexpectedEOF
33+
t.Logf("🪲: [%%v]:\n%v", e)
34+
35+
s, ok := New(ctx, ErrorLevel, codes.Internal, "my error message", io.ErrUnexpectedEOF, WithLogger(DiscardLogger)).(interface{ GRPCStatus() *status.Status }) //nolint:errorlint
36+
if !ok {
37+
t.Errorf("❌: New: not implement interface{ GRPCStatus() *status.Status }")
38+
}
39+
t.Logf("🪲: [%%v]:\n%v", s.GRPCStatus())
2740
})
2841
}

0 commit comments

Comments
 (0)