diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-06-16 22:20:17 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-06-16 22:20:17 +0530 |
commit | 05fe09f3ec167417ea94fc04148ac110f18c4ad6 (patch) | |
tree | 19d7c2713c8e24772ccbacc1572c354d48707dc9 /user/manga/request_handler.go | |
parent | 71210ebc8e04d49a6afeeecee842b2e8b53f3c4f (diff) |
added api error handling to user/anime and user/manga packages
Diffstat (limited to 'user/manga/request_handler.go')
-rw-r--r-- | user/manga/request_handler.go | 58 |
1 files changed, 40 insertions, 18 deletions
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 } |