gensupport: revert change to send with context
This reverts a change that removed the usage of the
custom send-with-context. Although req.WithContext is
now usable, it does not return context.Canceled -
it returns an error like "[...] context canceled".
That constitutes a breaking API change, so let's
continue using the old behavior.
Change-Id: Ic89974880c8758bf0e10f1054b02e8e90e5aec19
Reviewed-on: https://code-review.googlesource.com/c/35371
Reviewed-by: Eno Compton <enocom@google.com>
diff --git a/gensupport/send.go b/gensupport/send.go
index 015e861..5799393 100644
--- a/gensupport/send.go
+++ b/gensupport/send.go
@@ -49,7 +49,7 @@
}
// Send request.
- resp, err := client.Do(req.WithContext(ctx))
+ resp, err := send(ctx, client, req)
// Call returned funcs in reverse order.
for i := len(post) - 1; i >= 0; i-- {
@@ -60,6 +60,23 @@
return resp, err
}
+func send(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
+ if client == nil {
+ client = http.DefaultClient
+ }
+ resp, err := client.Do(req.WithContext(ctx))
+ // If we got an error, and the context has been canceled,
+ // the context's error is probably more useful.
+ if err != nil {
+ select {
+ case <-ctx.Done():
+ err = ctx.Err()
+ default:
+ }
+ }
+ return resp, err
+}
+
// DecodeResponse decodes the body of res into target. If there is no body,
// target is unchanged.
func DecodeResponse(target interface{}, res *http.Response) error {