Chromium Code Reviews| Index: grpc/grpcutil/errors.go |
| diff --git a/grpc/grpcutil/errors.go b/grpc/grpcutil/errors.go |
| index 760892e5f5a6c0232add46d4037538043869e238..5a4323422d82323e2fb480e634b4a43c21e0b0b1 100644 |
| --- a/grpc/grpcutil/errors.go |
| +++ b/grpc/grpcutil/errors.go |
| @@ -5,17 +5,26 @@ |
| package grpcutil |
| import ( |
| - "github.com/luci/luci-go/common/errors" |
| - |
| "google.golang.org/grpc" |
| "google.golang.org/grpc/codes" |
| + |
| + "github.com/luci/luci-go/common/errors" |
| + "github.com/luci/luci-go/common/retry" |
| ) |
| -var ( |
| - // Errf falls through to grpc.Errorf, with the notable exception that it isn't |
| - // named "Errorf" and, consequently, won't trigger "go vet" misuse errors. |
| - Errf = grpc.Errorf |
| +// Errf falls through to grpc.Errorf, but will tag the resulting error with |
| +// retry.Tag if it's transient. |
| +// |
| +// This is named `Errf` so that it won't trigger "go vet" misuse errors. |
| +func Errf(code codes.Code, fmt string, a ...interface{}) error { |
| + ret := grpc.Errorf(code, fmt, a...) |
| + if IsTransientCode(code) { |
| + ret = retry.Tag.Apply(ret) |
|
dnj
2017/06/24 14:53:55
This is concerning to me WRT current behavior. I l
iannucci
2017/06/24 20:16:10
I'll undo this for now then
|
| + } |
| + return ret |
| +} |
| +var ( |
| // OK is an empty grpc.OK status error. |
| OK = Errf(codes.OK, "") |
| @@ -81,30 +90,29 @@ func WrapIfTransient(err error) error { |
| } |
| if IsTransientCode(Code(err)) { |
| - err = errors.WrapTransient(err) |
| + err = retry.Tag.Apply(err) |
| } |
| return err |
| } |
| -const grpcCodeKey = "__grpcutil.Code" |
| +// Tag may be used to associate a gRPC status code with this error. |
| +// |
| +// The tag value MUST be a "google.golang.org/grpc/codes".Code. |
| +var Tag = errors.NewTagger("gRPC Code", func(v interface{}) { |
| + _ = v.(codes.Code) |
| +}) |
| // Code returns the gRPC code for a given error. |
| // |
| // In addition to the functionality of grpc.Code, this will unwrap any wrapped |
| // errors before asking for its code. |
| func Code(err error) codes.Code { |
| - if code := errors.ExtractData(err, grpcCodeKey); code != nil { |
| + if code, ok := Tag.ValueIn(err); ok { |
| return code.(codes.Code) |
| } |
| return grpc.Code(errors.Unwrap(err)) |
| } |
| -// Annotate begins annotating the error, and adds the given gRPC code. |
| -// This code may be extracted with the Code function in this package. |
| -func Annotate(err error, code codes.Code) *errors.Annotator { |
| - return errors.Annotate(err).D(grpcCodeKey, code) |
| -} |
| - |
| // ToGRPCErr is a shorthand for Errf(Code(err), "%s", err) |
| func ToGRPCErr(err error) error { |
| return Errf(Code(err), "%s", err) |