From 09f73cff09a8a032e9a216e59c389c15745992c1 Mon Sep 17 00:00:00 2001 From: irlndts Date: Wed, 9 Mar 2016 18:14:42 +0300 Subject: [PATCH] errors --- errors.go | 40 +++++++++++++++++++++++++++++++++++++ examples/discogs_example.go | 8 ++++++-- releases.go | 5 +++-- 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 errors.go diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..714ddaf --- /dev/null +++ b/errors.go @@ -0,0 +1,40 @@ +package discogs + +import ( + "fmt" +) + +// APIError represents a Twitter API Error response +// https://dev.twitter.com/overview/api/response-codes +type APIError struct { + Message string `json:"message"` +} + +func (e APIError) Error() string { + if e.Message != "" { + return fmt.Sprintf("discogs: %v", e.Message) + } + return "" +} + +// Empty returns true if empty. Otherwise, at least 1 error message/code is +// present and false is returned. +func (e APIError) Empty() bool { + if e.Message == "" { + return true + } + return false +} + +// relevantError returns any non-nil http-related error (creating the request, +// getting the response, decoding) if any. If the decoded apiError is non-zero +// the apiError is returned. Otherwise, no errors occurred, returns nil. +func relevantError(httpError error, apiError APIError) error { + if httpError != nil { + return httpError + } + if apiError.Empty() { + return nil + } + return apiError +} diff --git a/examples/discogs_example.go b/examples/discogs_example.go index 5b71dc2..355199f 100644 --- a/examples/discogs_example.go +++ b/examples/discogs_example.go @@ -12,6 +12,10 @@ func main() { d := discogs.NewClient(client).UserAgent("TestDiscogsClient/0.0.1 +http://irlndts.moscow") params := &discogs.ReleaseParams{Release_id: "1"} release, _, err := d.Release.Release(params) - fmt.Println(release) - fmt.Println(err) + if err != nil { + fmt.Println(err) + } else { + fmt.Println(release) + } + //fmt.Println(resp) } diff --git a/releases.go b/releases.go index a1e240e..debe435 100644 --- a/releases.go +++ b/releases.go @@ -151,6 +151,7 @@ func newReleaseService(api *apirequest.API) *ReleaseService { func (self *ReleaseService) Release(params *ReleaseParams) (*Release, *http.Response, error) { release := new(Release) - resp, err := self.api.New().Get(params.Release_id).QueryStruct(params).Receive(release, nil) - return release, resp, err + apiError := new(APIError) + resp, err := self.api.New().Get(params.Release_id).QueryStruct(params).Receive(release, apiError) + return release, resp, relevantError(err, *apiError) }