diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-06-16 21:41:22 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-06-16 21:41:22 +0530 |
commit | 71210ebc8e04d49a6afeeecee842b2e8b53f3c4f (patch) | |
tree | 87bf595e9095604dbf85cdac4cfd133856833f41 /anime | |
parent | 052b6604a04ca0909bad714981e3d94c6d9e20b4 (diff) |
handling server errors in user, manga and anime package
Diffstat (limited to 'anime')
-rw-r--r-- | anime/anime.go | 40 | ||||
-rw-r--r-- | anime/request_handler.go | 20 |
2 files changed, 41 insertions, 19 deletions
diff --git a/anime/anime.go b/anime/anime.go index c6a21e2..ed18a52 100644 --- a/anime/anime.go +++ b/anime/anime.go @@ -17,24 +17,21 @@ package anime import ( - "encoding/json" - "fmt" - "strconv" + "encoding/json" + "fmt" + "strconv" e "github.com/MikunoNaka/MAL2Go/errhandlers" u "github.com/MikunoNaka/MAL2Go/util" ) const BASE_URL string = "https://api.myanimelist.net/v2/anime" -// MAL Might change this -const maxAnimeLimit int = 500 - // in MAL documentation this is named Get Anime List func (c Client) SearchAnime(searchString string, limit, offset int, fields []string) ([]Anime, error) { var searchResults []Anime // error handling for limit - limitErr := e.LimitErrHandler(limit, maxAnimeLimit) + limitErr := e.LimitErrHandler(limit, 100) if limitErr != nil { return searchResults, limitErr } @@ -55,7 +52,10 @@ func (c Client) SearchAnime(searchString string, limit, offset int, fields []str // gets data from API and stores it in a struct var animeSearchData AnimeSearchRaw - data := c.requestHandler(endpoint) + data, err := c.requestHandler(endpoint) + if err != nil { + return searchResults, err + } json.Unmarshal([]byte(data), &animeSearchData) // Adding all the animes to another list to get formatted results later @@ -85,7 +85,10 @@ func (c Client) GetAnimeById(animeId int, fields []string) (Anime, error) { true, ) - data := c.requestHandler(endpoint) + data, err := c.requestHandler(endpoint) + if err != nil { + return anime, err + } json.Unmarshal([]byte(data), &anime) return anime, nil @@ -96,7 +99,7 @@ func (c Client) GetAnimeRanking(rankingType string, limit, offset int, fields [] var animeRanking []rAnime // error handling for limit - limitErr := e.LimitErrHandler(limit, maxAnimeLimit) + limitErr := e.LimitErrHandler(limit, 500) if limitErr != nil { return animeRanking, limitErr } @@ -121,7 +124,10 @@ func (c Client) GetAnimeRanking(rankingType string, limit, offset int, fields [] // gets data from API and stores it in a struct var rankingData RawRanking - data := c.requestHandler(endpoint) + data, err := c.requestHandler(endpoint) + if err != nil { + return animeRanking, err + } json.Unmarshal([]byte(data), &rankingData) // Adding all the animes in ranking list to a slice @@ -141,7 +147,7 @@ func (c Client) GetSeasonalAnime(year, season, sort string, limit, offset int, f var seasonalAnime SeasonalAnime // error handling for limit - limitErr := e.LimitErrHandler(limit, maxAnimeLimit) + limitErr := e.LimitErrHandler(limit, 500) if limitErr != nil { return seasonalAnime, limitErr } @@ -171,7 +177,10 @@ func (c Client) GetSeasonalAnime(year, season, sort string, limit, offset int, f // gets data from API and stores it in a struct var seasonalAnimeData SeasonalAnimeRaw - data := c.requestHandler(endpoint) + data, err := c.requestHandler(endpoint) + if err != nil { + return seasonalAnime, err + } json.Unmarshal([]byte(data), &seasonalAnimeData) // Adding all the animes to another list to get formatted results later @@ -215,7 +224,10 @@ func (c Client) GetSuggestedAnime(limit, offset int, fields []string) ([]Anime, // gets data from API and stores it in a struct var suggestedAnimeData SuggestedAnimeRaw - data := c.requestHandler(endpoint) + data, err := c.requestHandler(endpoint) + if err != nil { + return suggestedAnime, err + } json.Unmarshal([]byte(data), &suggestedAnimeData) // Adding all the animes to another list to get formatted results later diff --git a/anime/request_handler.go b/anime/request_handler.go index 00703bc..816417e 100644 --- a/anime/request_handler.go +++ b/anime/request_handler.go @@ -17,14 +17,17 @@ package anime import ( - "io/ioutil" - "log" - "net/http" + "io/ioutil" + "log" + "net/http" + "encoding/json" + "github.com/MikunoNaka/MAL2Go/util" + "errors" ) // Handles HTTP request with your OAuth token as a Header // TODO: Verify that this function is safe to use -func (c Client) requestHandler(endpoint string) string { +func (c Client) requestHandler(endpoint string) (string, error) { // generate request req, err := http.NewRequest("GET", endpoint, nil) if err != nil { @@ -45,5 +48,12 @@ func (c Client) requestHandler(endpoint string) string { log.Fatal(err) } - return string(body) + // error handling (if API returns error) + var errMsg util.APIError + json.Unmarshal(body, &errMsg) + if errMsg.Err != "" { + return string(body), errors.New(errMsg.Err + " " + errMsg.Msg) + } + + return string(body), nil } |