Skip to content

Commit bfde808

Browse files
authored
Minor code cleanups (#586)
* Use strings.Cut() for cleaner code * Remove unnecessary formatting
1 parent a18e1e2 commit bfde808

File tree

10 files changed

+27
-27
lines changed

10 files changed

+27
-27
lines changed

examples/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func main() {
111111
}
112112
// TODO: This is example only, perform proper Oauth/OIDC verification!
113113
if token != "yolo" {
114-
return nil, status.Errorf(codes.Unauthenticated, "invalid auth token")
114+
return nil, status.Error(codes.Unauthenticated, "invalid auth token")
115115
}
116116
// NOTE: You can also pass the token in the context for further interceptors or gRPC service code.
117117
return ctx, nil

interceptors/auth/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func buildDummyAuthFunction(expectedScheme string, expectedToken string) func(ct
3939
return nil, err
4040
}
4141
if token != expectedToken {
42-
return nil, status.Errorf(codes.PermissionDenied, "buildDummyAuthFunction bad token")
42+
return nil, status.Error(codes.PermissionDenied, "buildDummyAuthFunction bad token")
4343
}
4444
return context.WithValue(ctx, authedMarker, "marker_exists"), nil
4545
}

interceptors/auth/metadata.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ var (
2424
func AuthFromMD(ctx context.Context, expectedScheme string) (string, error) {
2525
val := metadata.ExtractIncoming(ctx).Get(headerAuthorize)
2626
if val == "" {
27-
return "", status.Errorf(codes.Unauthenticated, "Request unauthenticated with "+expectedScheme)
27+
return "", status.Error(codes.Unauthenticated, "Request unauthenticated with "+expectedScheme)
2828
}
29-
splits := strings.SplitN(val, " ", 2)
30-
if len(splits) < 2 {
31-
return "", status.Errorf(codes.Unauthenticated, "Bad authorization string")
29+
scheme, token, found := strings.Cut(val, " ")
30+
if !found {
31+
return "", status.Error(codes.Unauthenticated, "Bad authorization string")
3232
}
33-
if !strings.EqualFold(splits[0], expectedScheme) {
34-
return "", status.Errorf(codes.Unauthenticated, "Request unauthenticated with "+expectedScheme)
33+
if !strings.EqualFold(scheme, expectedScheme) {
34+
return "", status.Error(codes.Unauthenticated, "Request unauthenticated with "+expectedScheme)
3535
}
36-
return splits[1], nil
36+
return token, nil
3737
}

interceptors/client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ func (s *ClientInterceptorTestSuite) TestUnaryReporting() {
218218
require.Error(s.T(), err)
219219
s.mock.Equal(s.T(), []*mockReport{{
220220
CallMeta: CallMeta{Typ: Unary, Service: testpb.TestServiceFullName, Method: "PingError"},
221-
postCalls: []error{status.Errorf(codes.FailedPrecondition, "Userspace error.")},
222-
postMsgReceives: []error{status.Errorf(codes.FailedPrecondition, "Userspace error.")},
221+
postCalls: []error{status.Error(codes.FailedPrecondition, "Userspace error")},
222+
postMsgReceives: []error{status.Error(codes.FailedPrecondition, "Userspace error")},
223223
postMsgSends: []error{nil},
224224
}})
225225
}
@@ -288,8 +288,8 @@ func (s *ClientInterceptorTestSuite) TestListReporting() {
288288

289289
s.mock.Equal(s.T(), []*mockReport{{
290290
CallMeta: CallMeta{Typ: ServerStream, Service: testpb.TestServiceFullName, Method: "PingList"},
291-
postCalls: []error{status.Errorf(codes.FailedPrecondition, "foobar"), status.Errorf(codes.FailedPrecondition, "foobar")},
292-
postMsgReceives: []error{status.Errorf(codes.FailedPrecondition, "foobar"), status.Errorf(codes.FailedPrecondition, "foobar")},
291+
postCalls: []error{status.Error(codes.FailedPrecondition, "foobar"), status.Error(codes.FailedPrecondition, "foobar")},
292+
postMsgReceives: []error{status.Error(codes.FailedPrecondition, "foobar"), status.Error(codes.FailedPrecondition, "foobar")},
293293
postMsgSends: []error{nil},
294294
}})
295295
}

interceptors/logging/interceptors_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func (s *loggingClientServerSuite) TestPingError_WithCustomLevels() {
349349
AssertFieldNotEmpty(t, "grpc.start_time").
350350
AssertFieldNotEmpty(t, "grpc.request.deadline").
351351
AssertField(t, "grpc.code", tcase.code.String()).
352-
AssertField(t, "grpc.error", fmt.Sprintf("rpc error: code = %s desc = Userspace error.", tcase.code.String())).
352+
AssertField(t, "grpc.error", fmt.Sprintf("rpc error: code = %s desc = Userspace error", tcase.code.String())).
353353
AssertFieldNotEmpty(t, "grpc.time_ms").AssertNoMoreTags(t)
354354

355355
clientFinishCallLogLine := lines[0]
@@ -360,7 +360,7 @@ func (s *loggingClientServerSuite) TestPingError_WithCustomLevels() {
360360
AssertFieldNotEmpty(t, "grpc.start_time").
361361
AssertFieldNotEmpty(t, "grpc.request.deadline").
362362
AssertField(t, "grpc.code", tcase.code.String()).
363-
AssertField(t, "grpc.error", fmt.Sprintf("rpc error: code = %s desc = Userspace error.", tcase.code.String())).
363+
AssertField(t, "grpc.error", fmt.Sprintf("rpc error: code = %s desc = Userspace error", tcase.code.String())).
364364
AssertFieldNotEmpty(t, "grpc.time_ms").AssertNoMoreTags(t)
365365
})
366366
}

interceptors/retry/retry.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func StreamClientInterceptor(optFuncs ...CallOption) grpc.StreamClientIntercepto
8686
return streamer(parentCtx, desc, cc, method, grpcOpts...)
8787
}
8888
if desc.ClientStreams {
89-
return nil, status.Errorf(codes.Unimplemented, "grpc_retry: cannot retry on ClientStreams, set grpc_retry.Disable()")
89+
return nil, status.Error(codes.Unimplemented, "grpc_retry: cannot retry on ClientStreams, set grpc_retry.Disable()")
9090
}
9191

9292
var lastErr error
@@ -304,11 +304,11 @@ func perCallContext(parentCtx context.Context, callOpts *options, attempt uint)
304304
func contextErrToGrpcErr(err error) error {
305305
switch err {
306306
case context.DeadlineExceeded:
307-
return status.Errorf(codes.DeadlineExceeded, err.Error())
307+
return status.Error(codes.DeadlineExceeded, err.Error())
308308
case context.Canceled:
309-
return status.Errorf(codes.Canceled, err.Error())
309+
return status.Error(codes.Canceled, err.Error())
310310
default:
311-
return status.Errorf(codes.Unknown, err.Error())
311+
return status.Error(codes.Unknown, err.Error())
312312
}
313313
}
314314

interceptors/retry/retry_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s *failingService) maybeFailRequest() error {
6363
return nil
6464
}
6565
time.Sleep(reqSleep)
66-
return status.Errorf(reqError, "maybeFailRequest: failing it")
66+
return status.Error(reqError, "maybeFailRequest: failing it")
6767
}
6868

6969
func (s *failingService) Ping(ctx context.Context, ping *testpb.PingRequest) (*testpb.PingResponse, error) {

interceptors/server_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ func (s *ServerInterceptorTestSuite) TestUnaryReporting() {
102102
require.Error(s.T(), err)
103103
s.mock.Equal(s.T(), []*mockReport{{
104104
CallMeta: CallMeta{Typ: Unary, Service: testpb.TestServiceFullName, Method: "PingError"},
105-
postCalls: []error{status.Errorf(codes.FailedPrecondition, "Userspace error.")},
105+
postCalls: []error{status.Error(codes.FailedPrecondition, "Userspace error")},
106106
postMsgReceives: []error{nil},
107-
postMsgSends: []error{status.Errorf(codes.FailedPrecondition, "Userspace error.")},
107+
postMsgSends: []error{status.Error(codes.FailedPrecondition, "Userspace error")},
108108
}})
109109
}
110110

@@ -134,7 +134,7 @@ func (s *ServerInterceptorTestSuite) TestStreamingReports() {
134134

135135
s.mock.requireOneReportWithRetry(s.ctx, s.T(), &mockReport{
136136
CallMeta: CallMeta{Typ: ServerStream, Service: testpb.TestServiceFullName, Method: "PingList"},
137-
postCalls: []error{status.Errorf(codes.FailedPrecondition, "foobar")},
137+
postCalls: []error{status.Error(codes.FailedPrecondition, "foobar")},
138138
postMsgReceives: []error{nil},
139139
})
140140
}

testing/testpb/pingservice.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ func (s *TestPingService) Ping(_ context.Context, ping *PingRequest) (*PingRespo
4040

4141
func (s *TestPingService) PingError(_ context.Context, ping *PingErrorRequest) (*PingErrorResponse, error) {
4242
code := codes.Code(ping.ErrorCodeReturned)
43-
return nil, status.Errorf(code, "Userspace error.")
43+
return nil, status.Error(code, "Userspace error")
4444
}
4545

4646
func (s *TestPingService) PingList(ping *PingListRequest, stream TestService_PingListServer) error {
4747
if ping.ErrorCodeReturned != 0 {
48-
return status.Errorf(codes.Code(ping.ErrorCodeReturned), "foobar")
48+
return status.Error(codes.Code(ping.ErrorCodeReturned), "foobar")
4949
}
5050

5151
// Send user trailers and headers.

wrappers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ func (f *fakeServerStream) Context() context.Context {
4040

4141
func (f *fakeServerStream) SendMsg(m any) error {
4242
if f.sentMessage != nil {
43-
return status.Errorf(codes.AlreadyExists, "fakeServerStream only takes one message, sorry")
43+
return status.Error(codes.AlreadyExists, "fakeServerStream only takes one message, sorry")
4444
}
4545
f.sentMessage = m
4646
return nil
4747
}
4848

4949
func (f *fakeServerStream) RecvMsg(m any) error {
5050
if f.recvMessage == nil {
51-
return status.Errorf(codes.NotFound, "fakeServerStream has no message, sorry")
51+
return status.Error(codes.NotFound, "fakeServerStream has no message, sorry")
5252
}
5353
return nil
5454
}

0 commit comments

Comments
 (0)