aboutsummaryrefslogtreecommitdiff
path: root/user/request_handler.go
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 /user/request_handler.go
parent052b6604a04ca0909bad714981e3d94c6d9e20b4 (diff)
handling server errors in user, manga and anime package
Diffstat (limited to 'user/request_handler.go')
-rw-r--r--user/request_handler.go21
1 files changed, 16 insertions, 5 deletions
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
}