errors
This commit is contained in:
parent
4b348c7732
commit
09f73cff09
40
errors.go
Normal file
40
errors.go
Normal file
@ -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
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user