Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Unified Diff: grpc/grpcutil/errors.go

Issue 2951393002: [errors] de-specialize Transient in favor of Tags. (Closed)
Patch Set: more refactor Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: grpc/grpcutil/errors.go
diff --git a/grpc/grpcutil/errors.go b/grpc/grpcutil/errors.go
index 760892e5f5a6c0232add46d4037538043869e238..e34f3216a004f95904eaa54abe83ec1a9afdac76 100644
--- a/grpc/grpcutil/errors.go
+++ b/grpc/grpcutil/errors.go
@@ -5,10 +5,11 @@
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/transient"
)
var (
@@ -81,30 +82,38 @@ func WrapIfTransient(err error) error {
}
if IsTransientCode(Code(err)) {
- err = errors.WrapTransient(err)
+ err = transient.Tag.Apply(err)
}
return err
}
-const grpcCodeKey = "__grpcutil.Code"
+type grpcCodeTag struct{ Key errors.TagKey }
dnj 2017/06/27 03:57:03 QQ: why is "Key" named here (and elsewhere)?
iannucci 2017/06/27 18:14:27 Because TagKey is a pointer type, it can't be embe
+
+func (g grpcCodeTag) With(code codes.Code) errors.TagValue { return errors.MkTagValue(g.Key, code) }
+func (g grpcCodeTag) In(err error) (v codes.Code, ok bool) {
+ d, ok := errors.TagValueIn(g.Key, err)
+ if ok {
+ v = d.(codes.Code)
+ }
+ return
+}
+
+// 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 = grpcCodeTag{errors.NewTagKey("gRPC 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 {
- return code.(codes.Code)
+ if code, ok := Tag.In(err); ok {
+ return 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)

Powered by Google App Engine
This is Rietveld 408576698