github.com/renbou/grpcbridge@v0.0.2-0.20240416012907-bcbd8b12648a/internal/bridgetest/helpers.go (about) 1 package bridgetest 2 3 import ( 4 "fmt" 5 "strings" 6 7 "google.golang.org/grpc/codes" 8 "google.golang.org/grpc/status" 9 ) 10 11 // StatusCodeIs checks that the error has the specified gRPC status code. 12 func StatusCodeIs(err error, wantCode codes.Code) error { 13 if gotCode := status.Code(err); gotCode != wantCode { 14 return fmt.Errorf("got status code = %s, want %s", gotCode, wantCode) 15 } 16 17 return nil 18 } 19 20 // StatusIs checks that the error matches the specified gRPC status. 21 func StatusIs(err error, wantStatus *status.Status) error { 22 if codeErr := StatusCodeIs(err, wantStatus.Code()); codeErr != nil { 23 return codeErr 24 } 25 26 if gotStatus := status.Convert(err); !strings.Contains(gotStatus.Message(), wantStatus.Message()) { 27 return fmt.Errorf("got status message = %q, want %q", gotStatus.Message(), wantStatus.Message()) 28 } 29 30 return nil 31 }