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

Side by Side Diff: server/auth/internal/fetch.go

Issue 2951393002: [errors] de-specialize Transient in favor of Tags. (Closed)
Patch Set: more refactor 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package internal 5 package internal
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "encoding/json" 9 "encoding/json"
10 "fmt" 10 "fmt"
11 "io/ioutil" 11 "io/ioutil"
12 "net/http" 12 "net/http"
13 13
14 "github.com/luci/luci-go/common/errors"
15 "github.com/luci/luci-go/common/logging" 14 "github.com/luci/luci-go/common/logging"
15 "github.com/luci/luci-go/common/retry/transient"
16 16
17 "golang.org/x/net/context" 17 "golang.org/x/net/context"
18 "golang.org/x/net/context/ctxhttp" 18 "golang.org/x/net/context/ctxhttp"
19 ) 19 )
20 20
21 // ClientFactory knows how to produce http.Client that attach proper OAuth 21 // ClientFactory knows how to produce http.Client that attach proper OAuth
22 // headers. 22 // headers.
23 // 23 //
24 // If 'scopes' is empty, the factory should return a client that makes anonymous 24 // If 'scopes' is empty, the factory should return a client that makes anonymous
25 // requests. 25 // requests.
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 func fetchJSON(c context.Context, client *http.Client, val interface{}, f func() (*http.Request, error)) error { 111 func fetchJSON(c context.Context, client *http.Client, val interface{}, f func() (*http.Request, error)) error {
112 r, err := f() 112 r, err := f()
113 if err != nil { 113 if err != nil {
114 logging.Errorf(c, "auth: URL fetch failed - %s", err) 114 logging.Errorf(c, "auth: URL fetch failed - %s", err)
115 return err 115 return err
116 } 116 }
117 logging.Infof(c, "auth: %s %s", r.Method, r.URL) 117 logging.Infof(c, "auth: %s %s", r.Method, r.URL)
118 resp, err := ctxhttp.Do(c, client, r) 118 resp, err := ctxhttp.Do(c, client, r)
119 if err != nil { 119 if err != nil {
120 logging.Errorf(c, "auth: URL fetch failed, can't connect - %s", err) 120 logging.Errorf(c, "auth: URL fetch failed, can't connect - %s", err)
121 » » return errors.WrapTransient(err) 121 » » return transient.Tag.Apply(err)
122 } 122 }
123 defer func() { 123 defer func() {
124 ioutil.ReadAll(resp.Body) 124 ioutil.ReadAll(resp.Body)
125 resp.Body.Close() 125 resp.Body.Close()
126 }() 126 }()
127 if resp.StatusCode >= 300 { 127 if resp.StatusCode >= 300 {
128 body, _ := ioutil.ReadAll(resp.Body) 128 body, _ := ioutil.ReadAll(resp.Body)
129 // Opportunistically try to unmarshal the response. Works with J SON APIs. 129 // Opportunistically try to unmarshal the response. Works with J SON APIs.
130 if val != nil { 130 if val != nil {
131 json.Unmarshal(body, val) 131 json.Unmarshal(body, val)
132 } 132 }
133 logging.Errorf(c, "auth: URL fetch failed - HTTP %d - %s", resp. StatusCode, string(body)) 133 logging.Errorf(c, "auth: URL fetch failed - HTTP %d - %s", resp. StatusCode, string(body))
134 err := fmt.Errorf("auth: HTTP code (%d) when fetching %s", resp. StatusCode, r.URL) 134 err := fmt.Errorf("auth: HTTP code (%d) when fetching %s", resp. StatusCode, r.URL)
135 if resp.StatusCode >= 500 { 135 if resp.StatusCode >= 500 {
136 » » » return errors.WrapTransient(err) 136 » » » return transient.Tag.Apply(err)
137 } 137 }
138 return err 138 return err
139 } 139 }
140 if val != nil { 140 if val != nil {
141 if err = json.NewDecoder(resp.Body).Decode(val); err != nil { 141 if err = json.NewDecoder(resp.Body).Decode(val); err != nil {
142 logging.Errorf(c, "auth: URL fetch failed, bad JSON - %s ", err) 142 logging.Errorf(c, "auth: URL fetch failed, bad JSON - %s ", err)
143 return fmt.Errorf("auth: can't deserialize JSON at %q - %s", r.URL, err) 143 return fmt.Errorf("auth: can't deserialize JSON at %q - %s", r.URL, err)
144 } 144 }
145 } 145 }
146 return nil 146 return nil
147 } 147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698