diff options
Diffstat (limited to 'user')
-rw-r--r-- | user/request_handler.go | 21 | ||||
-rw-r--r-- | user/user.go | 12 | ||||
-rw-r--r-- | user/user.structs.go | 5 |
3 files changed, 21 insertions, 17 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 } diff --git a/user/user.go b/user/user.go index 704fed7..0d1cbe5 100644 --- a/user/user.go +++ b/user/user.go @@ -30,14 +30,12 @@ func (c Client) GetSelfUserInfo() (UserInfo, error) { // get data from API var userData UserInfo - var errMessage Error - data := c.requestHandler(endpoint) - json.Unmarshal([]byte(data), &userData) - json.Unmarshal([]byte(data), &errMessage) - - if errMessage.Err != "" { - return userData, errors.New(errMessage.Err + " " + errMessage.Msg) + data, err := c.requestHandler(endpoint) + if err != nil { + return userData, err } + json.Unmarshal([]byte(data), &userData) + return userData, nil } diff --git a/user/user.structs.go b/user/user.structs.go index 731588d..b730033 100644 --- a/user/user.structs.go +++ b/user/user.structs.go @@ -28,8 +28,3 @@ type UserInfo struct { TimeZone string `json:"time_zone"` IsSupporter bool `json:"is_supporter"` } - -type Error struct { - Err string `json:"error"` - Msg string `json:"message"` -} |