Skip to content

Commit d3094b3

Browse files
committed
fix(#794): Wrapping codes.OK should not cause panic
1 parent 6ec6dd3 commit d3094b3

File tree

4 files changed

+242
-169
lines changed

4 files changed

+242
-169
lines changed

testing/testpb/interceptor_suite.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ func (err *wrappedErrFields) Unwrap() error {
161161
}
162162

163163
func (err *wrappedErrFields) Error() string {
164+
if err.wrappedErr == nil {
165+
return ""
166+
}
167+
164168
// Ideally we print wrapped fields as well
165169
return err.wrappedErr.Error()
166170
}

testing/testpb/pingservice_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package testpb
55

66
import (
77
"context"
8+
"errors"
89
"fmt"
910
"net"
1011
"testing"
1112

13+
"github.com/stretchr/testify/assert"
1214
"github.com/stretchr/testify/require"
1315
"google.golang.org/grpc"
1416
"google.golang.org/grpc/codes"
@@ -89,3 +91,41 @@ func TestPingServiceOnWire(t *testing.T) {
8991
default:
9092
}
9193
}
94+
95+
func TestTestServicePing_PingError(t *testing.T) {
96+
testCases := map[string]struct {
97+
request *PingErrorRequest
98+
err error
99+
unwrapped error
100+
msg string
101+
}{
102+
"NotFound": {
103+
request: &PingErrorRequest{ErrorCodeReturned: uint32(codes.NotFound), Value: "not found"},
104+
err: &wrappedErrFields{wrappedErr: status.Error(codes.NotFound, "Userspace error"), fields: []any{"error-field", "plop"}},
105+
unwrapped: status.Error(codes.NotFound, "Userspace error"),
106+
msg: "rpc error: code = NotFound desc = Userspace error",
107+
},
108+
"OK": {
109+
request: &PingErrorRequest{ErrorCodeReturned: uint32(codes.OK), Value: "ok"},
110+
err: &wrappedErrFields{wrappedErr: nil, fields: []any{"error-field", "plop"}},
111+
unwrapped: nil,
112+
msg: "",
113+
},
114+
}
115+
116+
for name, testCase := range testCases {
117+
t.Run(name, func(t *testing.T) {
118+
svc := &TestPingService{}
119+
120+
_, err := svc.PingError(context.Background(), testCase.request)
121+
require.Equal(t, testCase.err, err)
122+
123+
var we *wrappedErrFields
124+
ok := errors.As(err, &we)
125+
require.True(t, ok)
126+
127+
assert.Equal(t, testCase.unwrapped, we.Unwrap())
128+
assert.Equal(t, testCase.msg, we.Error())
129+
})
130+
}
131+
}

testing/testpb/test.pb.go

Lines changed: 52 additions & 108 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)