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

Unified Diff: common/lhttp/client.go

Issue 2984913002: Add HTTP error annotation. (Closed)
Patch Set: Created 3 years, 5 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
« no previous file with comments | « common/errors/tags.go ('k') | common/lhttp/client_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/lhttp/client.go
diff --git a/common/lhttp/client.go b/common/lhttp/client.go
index 217adfef1d35dc1b1fa42930192e2f8dd6ae8b38..4115746066ed261b589be2a7d6b1804de4a253c4 100644
--- a/common/lhttp/client.go
+++ b/common/lhttp/client.go
@@ -54,6 +54,20 @@ type ErrorHandler func(resp *http.Response, err error) error
// documentation.
type RequestGen func() (*http.Request, error)
+var httpTagKey = errors.NewTagKey("this is an HTTP error")
+
+func applyHTTPTag(err error, status int) error {
+ return errors.TagValue{Key: httpTagKey, Value: status}.Apply(err)
+}
+
+func IsHTTPError(err error) (status int, ok bool) {
+ d, ok := errors.TagValueIn(httpTagKey, err)
+ if ok {
+ status = d.(int)
+ }
+ return
+}
+
// NewRequest returns a retriable request.
//
// The handler func is responsible for closing the response Body before
@@ -117,10 +131,11 @@ func NewRequest(ctx context.Context, c *http.Client, rFn retry.Factory, rgen Req
return handler(resp)
}
+ err = applyHTTPTag(err, status)
return errorHandler(resp, err)
}, nil)
if err != nil {
- err = fmt.Errorf("%v (attempts: %d)", err, attempts)
+ err = errors.Annotate(err, "gave up after %d attempts", attempts).Err()
}
return status, err
}
« no previous file with comments | « common/errors/tags.go ('k') | common/lhttp/client_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698