aboutsummaryrefslogtreecommitdiff
path: root/manga
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-06-16 21:41:22 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-06-16 21:41:22 +0530
commit71210ebc8e04d49a6afeeecee842b2e8b53f3c4f (patch)
tree87bf595e9095604dbf85cdac4cfd133856833f41 /manga
parent052b6604a04ca0909bad714981e3d94c6d9e20b4 (diff)
handling server errors in user, manga and anime package
Diffstat (limited to 'manga')
-rw-r--r--manga/manga.go17
-rw-r--r--manga/request_handler.go20
2 files changed, 28 insertions, 9 deletions
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
}