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 | |
parent | 052b6604a04ca0909bad714981e3d94c6d9e20b4 (diff) |
handling server errors in user, manga and anime package
-rw-r--r-- | anime/anime.go | 40 | ||||
-rw-r--r-- | anime/request_handler.go | 20 | ||||
-rw-r--r-- | manga/manga.go | 17 | ||||
-rw-r--r-- | manga/request_handler.go | 20 | ||||
-rw-r--r-- | user/request_handler.go | 21 | ||||
-rw-r--r-- | user/user.go | 12 | ||||
-rw-r--r-- | user/user.structs.go | 5 | ||||
-rw-r--r-- | util/structs.go | 13 |
8 files changed, 94 insertions, 54 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 } diff --git a/manga/manga.go b/manga/manga.go index eecf734..c83200a 100644 --- a/manga/manga.go +++ b/manga/manga.go @@ -51,7 +51,10 @@ func (c Client) SearchManga(searchString string, limit, offset int, fields []str // gets data from API and stores it in a struct var mangaSearchData MangaSearchRaw - data := c.requestHandler(endpoint) + data, err := c.requestHandler(endpoint) + if err != nil { + return searchResults, err + } json.Unmarshal([]byte(data), &mangaSearchData) for _, element := range mangaSearchData.Data { @@ -78,7 +81,10 @@ func (c Client) GetMangaById(mangaId int, fields []string) (Manga, error) { true, ) - data := c.requestHandler(endpoint) + data, err := c.requestHandler(endpoint) + if err != nil { + return manga, err + } json.Unmarshal([]byte(data), &manga) return manga, nil @@ -114,7 +120,10 @@ func (c Client) GetMangaRanking(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 mangaRanking, err + } json.Unmarshal([]byte(data), &rankingData) // Adding all the mangas in ranking list to a slice @@ -123,7 +132,7 @@ func (c Client) GetMangaRanking(rankingType string, limit, offset int, fields [] m := manga.Manga m.RankNum = manga.Ranking.Rank - // add newManga to list + // add manga to list mangaRanking = append(mangaRanking, m) } diff --git a/manga/request_handler.go b/manga/request_handler.go index 892289b..8484a57 100644 --- a/manga/request_handler.go +++ b/manga/request_handler.go @@ -17,13 +17,16 @@ package manga 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 -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 { @@ -44,5 +47,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 } diff --git a/user/request_handler.go b/user/request_handler.go index 47a1699..b401fc1 100644 --- a/user/request_handler.go +++ b/user/request_handler.go @@ -17,13 +17,16 @@ package user import ( - "io/ioutil" - "log" - "net/http" + "io/ioutil" + "log" + "net/http" + "github.com/MikunoNaka/MAL2Go/util" + "errors" + "encoding/json" ) // Handles HTTP request with your OAuth token as a Header -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 { @@ -44,5 +47,13 @@ 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 } diff --git a/user/user.go b/user/user.go index 704fed7..0d1cbe5 100644 --- a/user/user.go +++ b/user/user.go @@ -30,14 +30,12 @@ func (c Client) GetSelfUserInfo() (UserInfo, error) { // get data from API var userData UserInfo - var errMessage Error - data := c.requestHandler(endpoint) - json.Unmarshal([]byte(data), &userData) - json.Unmarshal([]byte(data), &errMessage) - - if errMessage.Err != "" { - return userData, errors.New(errMessage.Err + " " + errMessage.Msg) + data, err := c.requestHandler(endpoint) + if err != nil { + return userData, err } + json.Unmarshal([]byte(data), &userData) + return userData, nil } diff --git a/user/user.structs.go b/user/user.structs.go index 731588d..b730033 100644 --- a/user/user.structs.go +++ b/user/user.structs.go @@ -28,8 +28,3 @@ type UserInfo struct { TimeZone string `json:"time_zone"` IsSupporter bool `json:"is_supporter"` } - -type Error struct { - Err string `json:"error"` - Msg string `json:"message"` -} diff --git a/util/structs.go b/util/structs.go index cb1f052..798b8e0 100644 --- a/util/structs.go +++ b/util/structs.go @@ -48,23 +48,18 @@ var DefaultMangaFields []string = []string{ "related_manga", "recommendations", "serialization", } -// contains previous/next page for anime list -// we don't actually need this. -// TODO: for compatibility's sake, keep this but also define methods -// to get the prev. and next page's elements automatically -type ListPaging struct { - NextPage string `json:"next"` - PrevPage string `json:"previous"` +type APIError struct { + Err string `json:"error"` + Msg string `json:"message"` } -/* these structs are used +/* these structs are used * both by anime and manga package */ type Picture struct { Medium string `json:"medium"` Large string `json:"large"` } -// for some reason the API returns them as string type StatusStatistics struct { Watching string `json:"watching"` Completed string `json:"completed"` |