go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/grpc/grpcutil/errors_test.go (about) 1 // Copyright 2020 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package grpcutil 16 17 import ( 18 "errors" 19 "testing" 20 21 "google.golang.org/grpc/codes" 22 "google.golang.org/grpc/status" 23 24 lucierr "go.chromium.org/luci/common/errors" 25 "go.chromium.org/luci/common/retry/transient" 26 27 . "github.com/smartystreets/goconvey/convey" 28 ) 29 30 func TestCode(t *testing.T) { 31 t.Parallel() 32 33 Convey("Code returns the correct error code", t, func() { 34 Convey("For simple errors", func() { 35 errGRPCNotFound := status.Errorf(codes.NotFound, "not found") 36 So(Code(errGRPCNotFound), ShouldEqual, codes.NotFound) 37 }) 38 39 Convey("For errors missing tags", func() { 40 err := errors.New("foobar") 41 So(Code(err), ShouldEqual, codes.Unknown) 42 }) 43 44 Convey("For wrapped errors", func() { 45 errGRPCNotFound := status.Errorf(codes.NotFound, "not found") 46 errWrapped := lucierr.Annotate(errGRPCNotFound, "wrapped").Err() 47 So(Code(errWrapped), ShouldEqual, codes.NotFound) 48 }) 49 50 Convey("Multi-errors with multiple different codes return Unknown", func() { 51 errGRPCNotFound := status.Errorf(codes.NotFound, "not found") 52 errGRPCInvalidArgument := status.Errorf(codes.InvalidArgument, "invalid argument") 53 errMulti := lucierr.NewMultiError(errGRPCNotFound, errGRPCInvalidArgument) 54 So(Code(errMulti), ShouldEqual, codes.Unknown) 55 }) 56 57 Convey("Multi-errors with one error return that error's code", func() { 58 errGRPCInvalidArgument := status.Errorf(codes.InvalidArgument, "invalid argument") 59 errMulti := lucierr.NewMultiError(errGRPCInvalidArgument) 60 So(Code(errMulti), ShouldEqual, codes.InvalidArgument) 61 }) 62 63 Convey("Multi-errors with the same code return that code", func() { 64 errGRPCInvalidArgument1 := status.Errorf(codes.InvalidArgument, "invalid argument") 65 errGRPCInvalidArgument2 := status.Errorf(codes.InvalidArgument, "invalid argument") 66 errMulti := lucierr.NewMultiError(errGRPCInvalidArgument1, errGRPCInvalidArgument2) 67 So(Code(errMulti), ShouldEqual, codes.InvalidArgument) 68 }) 69 70 Convey("Nested multi-errors work correctly", func() { 71 errGRPCInvalidArgument := status.Errorf(codes.InvalidArgument, "invalid argument") 72 errMulti1 := lucierr.NewMultiError(errGRPCInvalidArgument) 73 errMulti2 := lucierr.NewMultiError(errMulti1) 74 So(Code(errMulti2), ShouldEqual, codes.InvalidArgument) 75 }) 76 }) 77 } 78 79 func TestWrapIfTransient(t *testing.T) { 80 t.Parallel() 81 82 Convey("Works", t, func() { 83 newErr := func(code codes.Code) error { return status.Errorf(code, "...") } 84 check := func(err error) bool { return transient.Tag.In(err) } 85 86 So(check(WrapIfTransient(newErr(codes.Internal))), ShouldBeTrue) 87 So(check(WrapIfTransient(newErr(codes.Unknown))), ShouldBeTrue) 88 So(check(WrapIfTransient(newErr(codes.Unavailable))), ShouldBeTrue) 89 90 So(check(WrapIfTransient(nil)), ShouldBeFalse) 91 So(check(WrapIfTransient(newErr(codes.FailedPrecondition))), ShouldBeFalse) 92 93 So(check(WrapIfTransientOr(newErr(codes.DeadlineExceeded))), ShouldBeFalse) 94 So(check(WrapIfTransientOr(newErr(codes.DeadlineExceeded), codes.DeadlineExceeded)), ShouldBeTrue) 95 }) 96 }