From 05fe09f3ec167417ea94fc04148ac110f18c4ad6 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Thu, 16 Jun 2022 22:20:17 +0530 Subject: added api error handling to user/anime and user/manga packages --- user/manga/request_handler.go | 58 +++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 18 deletions(-) (limited to 'user/manga/request_handler.go') diff --git a/user/manga/request_handler.go b/user/manga/request_handler.go index ff2b0df..0258b38 100644 --- a/user/manga/request_handler.go +++ b/user/manga/request_handler.go @@ -16,22 +16,32 @@ package manga import ( - "encoding/json" - "io/ioutil" - "log" - "net/http" - "net/url" - "strconv" - "strings" + "encoding/json" + "io/ioutil" + "log" + "net/http" + "net/url" + "strconv" + "strings" + "errors" + "github.com/MikunoNaka/MAL2Go/util" ) -type serverResponse struct { - Message string - Error string +type UpdateResponse struct { + Status string `json:"status"` + Score int `json:"score"` + VolumesRead int `json:"num_volumes_read"` + ChaptersRead int `json:"num_chapters_read"` + IsRereading bool `json:"is_rereading"` + Priority string `json:"priority"` + TimesReread string `json:"num_times_reread"` + RereadValue string `json:"reread_value"` + Tags string `json:"tags"` + Comments string `json:"string"` } // Handles HTTP request with your OAuth token as a Header -func (c Client) requestHandler(endpoint, method string) string { +func (c Client) requestHandler(endpoint, method string) (string, error) { // generate request req, err := http.NewRequest(method, endpoint, nil) if err != nil { @@ -52,16 +62,23 @@ func (c Client) requestHandler(endpoint, method string) string { log.Fatal(err) } + // 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) + } + // for DeleteAnime, its endpoint returns null data if method == "DELETE" { - return strconv.Itoa(res.StatusCode) + return strconv.Itoa(res.StatusCode), nil } - return string(body) + return string(body), nil } // for PUT requests (used for updating anime) -func (c Client) putRequestHandler(endpoint string, params url.Values) serverResponse { +func (c Client) putRequestHandler(endpoint string, params url.Values) (UpdateResponse, error) { paramsEncoded := params.Encode() // generate request @@ -87,10 +104,15 @@ func (c Client) putRequestHandler(endpoint string, params url.Values) serverResp log.Fatal(err) } - // TODO: there are other serverResponses. Add them - // server response, ie message / error - var resp serverResponse + // error handling (if API returns error) + var errMsg util.APIError + json.Unmarshal(body, &errMsg) + if errMsg.Err != "" { + return UpdateResponse{}, errors.New(errMsg.Err + " " + errMsg.Msg) + } + + var resp UpdateResponse json.Unmarshal(body, &resp) - return resp + return resp, nil } -- cgit v1.2.3