diff --git a/goinsta.go b/goinsta.go index e6122bff..e1142b4a 100644 --- a/goinsta.go +++ b/goinsta.go @@ -332,7 +332,7 @@ func (inst *Instagram) Login() error { if err != nil { return err } - body, err := inst.sendRequest( + _, err = inst.sendRequest( &reqOptions{ Endpoint: urlLogin, Query: generateSignature(b2s(result)), @@ -348,20 +348,9 @@ func (inst *Instagram) Login() error { // getting account data res := accountResp{} - err = json.Unmarshal(body, &res) - if err != nil { - ierr := instaError{} - err = json.Unmarshal(body, &ierr) - if err != nil { - err = instaToErr(ierr) - } - return err - } inst.Account = &res.Account inst.Account.inst = inst - inst.rankToken = strconv.FormatInt(inst.Account.ID, 10) + "_" + inst.uuid - inst.zrToken() return err diff --git a/request.go b/request.go index 4f1c277d..e9b19bcf 100644 --- a/request.go +++ b/request.go @@ -113,29 +113,35 @@ func (insta *Instagram) sendRequest(o *reqOptions) (body []byte, err error) { } body, err = ioutil.ReadAll(resp.Body) - if err != nil { - return + if err == nil { + err = isError(resp.StatusCode, body) } + return body, err +} - switch resp.StatusCode { +func isError(code int, body []byte) (err error) { + switch code { case 200: + case 503: + return Error503{ + Message: "Instagram API error. Try it later.", + } case 400: - ierr := instaError400{} + ierr := Error400{} err = json.Unmarshal(body, &ierr) if err == nil && ierr.Payload.Message != "" { - return nil, instaToErr(ierr) + return ierr } fallthrough default: - ierr := instaError{} + ierr := ErrorN{} err = json.Unmarshal(body, &ierr) if err != nil { - return nil, fmt.Errorf("Invalid status code: %d: %s", resp.StatusCode, body) + return fmt.Errorf("Invalid status code: %d: %s", code, body) } - return nil, instaToErr(ierr) + return ierr } - - return body, err + return nil } func (insta *Instagram) prepareData(other ...map[string]interface{}) (string, error) { diff --git a/types.go b/types.go index 7d597cc8..7f7e87b2 100644 --- a/types.go +++ b/types.go @@ -28,13 +28,28 @@ type PicURLInfo struct { Width int `json:"width"` } -type instaError struct { +// ErrorN is general instagram error +type ErrorN struct { Message string `json:"message"` Status string `json:"status"` ErrorType string `json:"error_type"` } -type instaError400 struct { +// Error503 is instagram API error +type Error503 struct { + Message string +} + +func (e Error503) Error() string { + return e.Message +} + +func (e ErrorN) Error() string { + return fmt.Sprintf("%s: %s (%s)", e.Status, e.Message, e.ErrorType) +} + +// Error400 is error returned by HTTP 400 status code. +type Error400 struct { Action string `json:"action"` StatusCode string `json:"status_code"` Payload struct { @@ -44,14 +59,8 @@ type instaError400 struct { Status string `json:"status"` } -func instaToErr(err interface{}) error { - switch ierr := err.(type) { - case instaError: - return fmt.Errorf("%s: %s (%s)", ierr.Status, ierr.Message, ierr.ErrorType) - case instaError400: - return fmt.Errorf("%s: %s", ierr.Status, ierr.Payload.Message) - } - return fmt.Errorf("Unknown error :)") +func (e Error400) Error() string { + return fmt.Sprintf("%s: %s", e.Status, e.Payload.Message) } // Nametag is part of the account information.