Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

Commit

Permalink
Added new error reporting types
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrr committed Jun 12, 2018
1 parent 6b8d08d commit 92e5e3b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
13 changes: 1 addition & 12 deletions goinsta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -348,20 +348,9 @@ func (inst *Instagram) Login() error {
// getting account data
res := accountResp{}

err = json.Unmarshal(body, &res)

This comment has been minimized.

Copy link
@sticreations

sticreations Jul 1, 2018

Now There the Account will be nil without unmarshalling. Is there a different approach to this now?

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
Expand Down
26 changes: 16 additions & 10 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 19 additions & 10 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down

0 comments on commit 92e5e3b

Please sign in to comment.