Skip to content

Commit 6f8030a

Browse files
khasanovbijohanbrandhorst
authored andcommitted
Renew deprecated code
1 parent c39ca0b commit 6f8030a

21 files changed

+80
-83
lines changed

auth/auth_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"google.golang.org/grpc/codes"
2222
"google.golang.org/grpc/credentials/oauth"
2323
"google.golang.org/grpc/metadata"
24+
"google.golang.org/grpc/status"
2425
)
2526

2627
var (
@@ -40,7 +41,7 @@ func buildDummyAuthFunction(expectedScheme string, expectedToken string) func(ct
4041
return nil, err
4142
}
4243
if token != expectedToken {
43-
return nil, grpc.Errorf(codes.PermissionDenied, "buildDummyAuthFunction bad token")
44+
return nil, status.Errorf(codes.PermissionDenied, "buildDummyAuthFunction bad token")
4445
}
4546
return context.WithValue(ctx, authedMarker, "marker_exists"), nil
4647
}
@@ -92,13 +93,13 @@ type AuthTestSuite struct {
9293
func (s *AuthTestSuite) TestUnary_NoAuth() {
9394
_, err := s.Client.Ping(s.SimpleCtx(), goodPing)
9495
assert.Error(s.T(), err, "there must be an error")
95-
assert.Equal(s.T(), codes.Unauthenticated, grpc.Code(err), "must error with unauthenticated")
96+
assert.Equal(s.T(), codes.Unauthenticated, status.Code(err), "must error with unauthenticated")
9697
}
9798

9899
func (s *AuthTestSuite) TestUnary_BadAuth() {
99100
_, err := s.Client.Ping(ctxWithToken(s.SimpleCtx(), "bearer", "bad_token"), goodPing)
100101
assert.Error(s.T(), err, "there must be an error")
101-
assert.Equal(s.T(), codes.PermissionDenied, grpc.Code(err), "must error with permission denied")
102+
assert.Equal(s.T(), codes.PermissionDenied, status.Code(err), "must error with permission denied")
102103
}
103104

104105
func (s *AuthTestSuite) TestUnary_PassesAuth() {
@@ -118,15 +119,15 @@ func (s *AuthTestSuite) TestStream_NoAuth() {
118119
require.NoError(s.T(), err, "should not fail on establishing the stream")
119120
_, err = stream.Recv()
120121
assert.Error(s.T(), err, "there must be an error")
121-
assert.Equal(s.T(), codes.Unauthenticated, grpc.Code(err), "must error with unauthenticated")
122+
assert.Equal(s.T(), codes.Unauthenticated, status.Code(err), "must error with unauthenticated")
122123
}
123124

124125
func (s *AuthTestSuite) TestStream_BadAuth() {
125126
stream, err := s.Client.PingList(ctxWithToken(s.SimpleCtx(), "bearer", "bad_token"), goodPing)
126127
require.NoError(s.T(), err, "should not fail on establishing the stream")
127128
_, err = stream.Recv()
128129
assert.Error(s.T(), err, "there must be an error")
129-
assert.Equal(s.T(), codes.PermissionDenied, grpc.Code(err), "must error with permission denied")
130+
assert.Equal(s.T(), codes.PermissionDenied, status.Code(err), "must error with permission denied")
130131
}
131132

132133
func (s *AuthTestSuite) TestStream_PassesAuth() {

auth/examples_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import (
77
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
88
"google.golang.org/grpc"
99
"google.golang.org/grpc/codes"
10-
)
11-
12-
var (
13-
cc *grpc.ClientConn
10+
"google.golang.org/grpc/status"
1411
)
1512

1613
func parseToken(token string) (struct{}, error) {
@@ -30,7 +27,7 @@ func Example_serverConfig() {
3027
}
3128
tokenInfo, err := parseToken(token)
3229
if err != nil {
33-
return nil, grpc.Errorf(codes.Unauthenticated, "invalid auth token: %v", err)
30+
return nil, status.Errorf(codes.Unauthenticated, "invalid auth token: %v", err)
3431
}
3532
grpc_ctxtags.Extract(ctx).Set("auth.sub", userClaimFromToken(tokenInfo))
3633
newCtx := context.WithValue(ctx, "tokenInfo", tokenInfo)

auth/metadata.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"strings"
99

1010
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
11-
"google.golang.org/grpc"
1211
"google.golang.org/grpc/codes"
12+
"google.golang.org/grpc/status"
1313
)
1414

1515
var (
@@ -24,15 +24,15 @@ var (
2424
func AuthFromMD(ctx context.Context, expectedScheme string) (string, error) {
2525
val := metautils.ExtractIncoming(ctx).Get(headerAuthorize)
2626
if val == "" {
27-
return "", grpc.Errorf(codes.Unauthenticated, "Request unauthenticated with "+expectedScheme)
27+
return "", status.Errorf(codes.Unauthenticated, "Request unauthenticated with "+expectedScheme)
2828

2929
}
3030
splits := strings.SplitN(val, " ", 2)
3131
if len(splits) < 2 {
32-
return "", grpc.Errorf(codes.Unauthenticated, "Bad authorization string")
32+
return "", status.Errorf(codes.Unauthenticated, "Bad authorization string")
3333
}
3434
if !strings.EqualFold(splits[0], expectedScheme) {
35-
return "", grpc.Errorf(codes.Unauthenticated, "Request unauthenticated with "+expectedScheme)
35+
return "", status.Errorf(codes.Unauthenticated, "Request unauthenticated with "+expectedScheme)
3636
}
3737
return splits[1], nil
3838
}

auth/metadata_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99

1010
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
1111
"github.com/stretchr/testify/assert"
12-
"google.golang.org/grpc"
1312
"google.golang.org/grpc/codes"
1413
"google.golang.org/grpc/metadata"
14+
"google.golang.org/grpc/status"
1515
)
1616

1717
func TestAuthFromMD(t *testing.T) {
@@ -64,7 +64,7 @@ func TestAuthFromMD(t *testing.T) {
6464
ctx := metautils.NiceMD(run.md).ToIncoming(context.TODO())
6565
out, err := AuthFromMD(ctx, "bearer")
6666
if run.errCode != codes.OK {
67-
assert.Equal(t, run.errCode, grpc.Code(err), run.msg)
67+
assert.Equal(t, run.errCode, status.Code(err), run.msg)
6868
} else {
6969
assert.NoError(t, err, run.msg)
7070
}

chain_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestChainUnaryClient(t *testing.T) {
111111
requireContextValue(t, ctx, "parent", "second must know the parent context value")
112112
require.Equal(t, someServiceName, method, "second must know someService")
113113
require.Len(t, opts, 1, "second should see parent CallOptions")
114-
wrappedOpts := append(opts, grpc.FailFast(true))
114+
wrappedOpts := append(opts, grpc.WaitForReady(false))
115115
wrappedCtx := context.WithValue(ctx, "second", 1)
116116
return invoker(wrappedCtx, method, req, reply, cc, wrappedOpts...)
117117
}
@@ -145,7 +145,7 @@ func TestChainStreamClient(t *testing.T) {
145145
requireContextValue(t, ctx, "parent", "second must know the parent context value")
146146
require.Equal(t, someServiceName, method, "second must know someService")
147147
require.Len(t, opts, 1, "second should see parent CallOptions")
148-
wrappedOpts := append(opts, grpc.FailFast(true))
148+
wrappedOpts := append(opts, grpc.WaitForReady(false))
149149
wrappedCtx := context.WithValue(ctx, "second", 1)
150150
return streamer(wrappedCtx, desc, cc, method, wrappedOpts...)
151151
}

logging/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import (
88
"io"
99

1010
"github.com/golang/protobuf/proto"
11-
"google.golang.org/grpc"
1211
"google.golang.org/grpc/codes"
12+
"google.golang.org/grpc/status"
1313
)
1414

1515
// ErrorToCode function determines the error code of an error
1616
// This makes using custom errors with grpc middleware easier
1717
type ErrorToCode func(err error) codes.Code
1818

1919
func DefaultErrorToCode(err error) codes.Code {
20-
return grpc.Code(err)
20+
return status.Code(err)
2121
}
2222

2323
// Decider function defines rules for suppressing any interceptor logs

ratelimit/ratelimit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ratelimit
22

33
import (
44
"context"
5+
56
"google.golang.org/grpc"
67
"google.golang.org/grpc/codes"
78
"google.golang.org/grpc/status"

recovery/examples_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ package grpc_recovery_test
66
import (
77
"github.com/grpc-ecosystem/go-grpc-middleware"
88
"github.com/grpc-ecosystem/go-grpc-middleware/recovery"
9-
"google.golang.org/grpc/codes"
109
"google.golang.org/grpc"
10+
"google.golang.org/grpc/codes"
11+
"google.golang.org/grpc/status"
1112
)
1213

1314
var (
@@ -18,7 +19,7 @@ var (
1819
func Example_initialization() {
1920
// Define customfunc to handle panic
2021
customFunc = func(p interface{}) (err error) {
21-
return grpc.Errorf(codes.Unknown, "panic triggered: %v", p)
22+
return status.Errorf(codes.Unknown, "panic triggered: %v", p)
2223
}
2324
// Shared options for the logger, with a custom gRPC code to log level function.
2425
opts := []grpc_recovery.Option{

recovery/interceptors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"google.golang.org/grpc"
1010
"google.golang.org/grpc/codes"
11+
"google.golang.org/grpc/status"
1112
)
1213

1314
// RecoveryHandlerFunc is a function that recovers from the panic `p` by returning an `error`.
@@ -47,7 +48,7 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
4748

4849
func recoverFrom(ctx context.Context, p interface{}, r RecoveryHandlerFuncContext) error {
4950
if r == nil {
50-
return grpc.Errorf(codes.Internal, "%s", p)
51+
return status.Errorf(codes.Internal, "%s", p)
5152
}
5253
return r(ctx, p)
5354
}

recovery/interceptors_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/stretchr/testify/suite"
1717
"google.golang.org/grpc"
1818
"google.golang.org/grpc/codes"
19+
"google.golang.org/grpc/status"
1920
)
2021

2122
var (
@@ -68,8 +69,8 @@ func (s *RecoverySuite) TestUnary_SuccessfulRequest() {
6869
func (s *RecoverySuite) TestUnary_PanickingRequest() {
6970
_, err := s.Client.Ping(s.SimpleCtx(), panicPing)
7071
require.Error(s.T(), err, "there must be an error")
71-
assert.Equal(s.T(), codes.Internal, grpc.Code(err), "must error with internal")
72-
assert.Equal(s.T(), "very bad thing happened", grpc.ErrorDesc(err), "must error with message")
72+
assert.Equal(s.T(), codes.Internal, status.Code(err), "must error with internal")
73+
assert.Equal(s.T(), "very bad thing happened", status.Convert(err).Message(), "must error with message")
7374
}
7475

7576
func (s *RecoverySuite) TestStream_SuccessfulReceive() {
@@ -85,14 +86,14 @@ func (s *RecoverySuite) TestStream_PanickingReceive() {
8586
require.NoError(s.T(), err, "should not fail on establishing the stream")
8687
_, err = stream.Recv()
8788
require.Error(s.T(), err, "there must be an error")
88-
assert.Equal(s.T(), codes.Internal, grpc.Code(err), "must error with internal")
89-
assert.Equal(s.T(), "very bad thing happened", grpc.ErrorDesc(err), "must error with message")
89+
assert.Equal(s.T(), codes.Internal, status.Code(err), "must error with internal")
90+
assert.Equal(s.T(), "very bad thing happened", status.Convert(err).Message(), "must error with message")
9091
}
9192

9293
func TestRecoveryOverrideSuite(t *testing.T) {
9394
opts := []grpc_recovery.Option{
9495
grpc_recovery.WithRecoveryHandler(func(p interface{}) (err error) {
95-
return grpc.Errorf(codes.Unknown, "panic triggered: %v", p)
96+
return status.Errorf(codes.Unknown, "panic triggered: %v", p)
9697
}),
9798
}
9899
s := &RecoveryOverrideSuite{
@@ -121,8 +122,8 @@ func (s *RecoveryOverrideSuite) TestUnary_SuccessfulRequest() {
121122
func (s *RecoveryOverrideSuite) TestUnary_PanickingRequest() {
122123
_, err := s.Client.Ping(s.SimpleCtx(), panicPing)
123124
require.Error(s.T(), err, "there must be an error")
124-
assert.Equal(s.T(), codes.Unknown, grpc.Code(err), "must error with unknown")
125-
assert.Equal(s.T(), "panic triggered: very bad thing happened", grpc.ErrorDesc(err), "must error with message")
125+
assert.Equal(s.T(), codes.Unknown, status.Code(err), "must error with unknown")
126+
assert.Equal(s.T(), "panic triggered: very bad thing happened", status.Convert(err).Message(), "must error with message")
126127
}
127128

128129
func (s *RecoveryOverrideSuite) TestStream_SuccessfulReceive() {
@@ -138,6 +139,6 @@ func (s *RecoveryOverrideSuite) TestStream_PanickingReceive() {
138139
require.NoError(s.T(), err, "should not fail on establishing the stream")
139140
_, err = stream.Recv()
140141
require.Error(s.T(), err, "there must be an error")
141-
assert.Equal(s.T(), codes.Unknown, grpc.Code(err), "must error with unknown")
142-
assert.Equal(s.T(), "panic triggered: very bad thing happened", grpc.ErrorDesc(err), "must error with message")
142+
assert.Equal(s.T(), codes.Unknown, status.Code(err), "must error with unknown")
143+
assert.Equal(s.T(), "panic triggered: very bad thing happened", status.Convert(err).Message(), "must error with message")
143144
}

0 commit comments

Comments
 (0)