From a0f508b8c558f8a45d7c599d6e938b743aa0a22e Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Tue, 20 Jul 2021 14:37:16 -0500 Subject: [PATCH] wrap some errors --- osuapi/beatmapsets.go | 4 +++- osuapi/osuapi.go | 15 +++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/osuapi/beatmapsets.go b/osuapi/beatmapsets.go index f52ed4a..ed0a36b 100644 --- a/osuapi/beatmapsets.go +++ b/osuapi/beatmapsets.go @@ -6,6 +6,8 @@ import ( "io/ioutil" "net/url" "os" + + "github.com/pkg/errors" ) func (api *Osuapi) SearchBeatmaps(rankStatus string) (beatmapSearch BeatmapSearch, err error) { @@ -15,7 +17,7 @@ func (api *Osuapi) SearchBeatmaps(rankStatus string) (beatmapSearch BeatmapSearc url := "/beatmapsets/search?" + query err = api.Request("GET", url, &beatmapSearch) if err != nil { - return + return errors.Wrap("failed to search beatmaps") } return diff --git a/osuapi/osuapi.go b/osuapi/osuapi.go index 8dbaaf0..adea136 100644 --- a/osuapi/osuapi.go +++ b/osuapi/osuapi.go @@ -11,6 +11,7 @@ import ( "sync" "time" + "github.com/pkg/errors" "golang.org/x/sync/semaphore" "subscribe-bot/config" @@ -73,17 +74,20 @@ func (api *Osuapi) Token() (token string, err error) { strings.NewReader(data), ) if err != nil { + err = errors.Wrap("failed to make POST request") return } var osuToken OsuToken respBody, err := ioutil.ReadAll(resp.Body) if err != nil { + err = errors.Wrap("failed to read response body") return } err = json.Unmarshal(respBody, &osuToken) if err != nil { + err = errors.Wrap("failed to unmarshal response body as json") return } @@ -106,16 +110,15 @@ func (api *Osuapi) Request0(action string, url string) (resp *http.Response, err token, err := api.Token() if err != nil { + err = errors.Wrap("failed to fetch token") return } req.Header.Add("Authorization", "Bearer "+token) - if err != nil { - return - } resp, err = api.httpClient.Do(req) if err != nil { + err = errors.Wrap("http client failed") return } @@ -141,17 +144,17 @@ func (api *Osuapi) Request0(action string, url string) (resp *http.Response, err func (api *Osuapi) Request(action string, url string, result interface{}) (err error) { resp, err := api.Request0(action, url) if err != nil { - return + return errors.Wrap("base request failed") } data, err := ioutil.ReadAll(resp.Body) if err != nil { - return + return errors.Wrap("failed to read http response body") } err = json.Unmarshal(data, result) if err != nil { - return + return errors.Wrap("failed to unmarshal http response as json") } return