Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions testing/testpb/interceptor_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ func (err *wrappedErrFields) Unwrap() error {
}

func (err *wrappedErrFields) Error() string {
if err.wrappedErr == nil {
return ""
}

// Ideally we print wrapped fields as well
return err.wrappedErr.Error()
}
Expand Down
40 changes: 40 additions & 0 deletions testing/testpb/pingservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package testpb

import (
"context"
"errors"
"fmt"
"net"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -89,3 +91,41 @@ func TestPingServiceOnWire(t *testing.T) {
default:
}
}

func TestTestServicePing_PingError(t *testing.T) {
testCases := map[string]struct {
request *PingErrorRequest
err error
unwrapped error
msg string
}{
"NotFound": {
request: &PingErrorRequest{ErrorCodeReturned: uint32(codes.NotFound), Value: "not found"},
err: &wrappedErrFields{wrappedErr: status.Error(codes.NotFound, "Userspace error"), fields: []any{"error-field", "plop"}},
unwrapped: status.Error(codes.NotFound, "Userspace error"),
msg: "rpc error: code = NotFound desc = Userspace error",
},
"OK": {
request: &PingErrorRequest{ErrorCodeReturned: uint32(codes.OK), Value: "ok"},
err: &wrappedErrFields{wrappedErr: nil, fields: []any{"error-field", "plop"}},
unwrapped: nil,
msg: "",
},
}

for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
svc := &TestPingService{}

_, err := svc.PingError(context.Background(), testCase.request)
require.Equal(t, testCase.err, err)

var we *wrappedErrFields
ok := errors.As(err, &we)
require.True(t, ok)

assert.Equal(t, testCase.unwrapped, we.Unwrap())
assert.Equal(t, testCase.msg, we.Error())
})
}
}
Loading