ducky/subscriptions
9:8eb19bcbf17d Browse Files
Return errors from responses in client. When the client makes a request, non-200 responses _are not_ considered errors. So we need to check the response.Errors property, and if it has errors, _then_ we consider the request to have an error. To make this happen, we created an httpErrors type that fulfills the error interface and just wraps the response Errors property. Then callers can type-cast it and interrogate it.
1.1 --- a/client/client.go Mon Jul 13 23:38:22 2015 -0400 1.2 +++ b/client/client.go Sat Jul 18 03:26:56 2015 -0400 1.3 @@ -4,11 +4,13 @@ 1.4 "bytes" 1.5 "encoding/json" 1.6 "errors" 1.7 + "fmt" 1.8 "io" 1.9 "net/http" 1.10 "strings" 1.11 "time" 1.12 1.13 + commonAPI "code.secondbit.org/api.hg" 1.14 "code.secondbit.org/uuid.hg" 1.15 1.16 "code.secondbit.org/ducky/subscriptions.hg/api" 1.17 @@ -86,9 +88,18 @@ 1.18 return response, err 1.19 } 1.20 } 1.21 + if len(response.Errors) > 0 { 1.22 + return response, httpErrors(response.Errors) 1.23 + } 1.24 return response, nil 1.25 } 1.26 1.27 +type httpErrors []commonAPI.RequestError 1.28 + 1.29 +func (h httpErrors) Error() string { 1.30 + return fmt.Sprintf("%+#v", h) 1.31 +} 1.32 + 1.33 func (c *Client) Get(url string, scopes []string, subject uuid.ID) (api.Response, error) { 1.34 return c.do("GET", url, nil, scopes, subject) 1.35 }