From 71210ebc8e04d49a6afeeecee842b2e8b53f3c4f Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Thu, 16 Jun 2022 21:41:22 +0530 Subject: handling server errors in user, manga and anime package --- anime/request_handler.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'anime/request_handler.go') 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 } -- cgit v1.2.3