wrap some errors

This commit is contained in:
Michael Zhang 2021-07-20 14:37:16 -05:00
parent 78bdcfa7b2
commit a0f508b8c5
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
2 changed files with 12 additions and 7 deletions

View file

@ -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

View file

@ -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