Skip to content

Commit

Permalink
create a new http response errors for XML bodies (#43)
Browse files Browse the repository at this point in the history
* create a new http response errors for XML bodies

* updated the endpoint

* updated the error message
  • Loading branch information
g8rswimmer authored Jan 4, 2021
1 parent 3a4a858 commit ff9c93b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 13 deletions.
53 changes: 46 additions & 7 deletions tweet.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ func (t *TweetRecentSearch) UnmarshalJSON(b []byte) error {
return nil
}

// HTTPError is a response error where the body is not JSON, but XML. This commonly seen in 404 errors.
type HTTPError struct {
Status string
StatusCode int
URL string
}

func (h *HTTPError) Error() string {
return fmt.Sprintf("twitter [%s] status: %s code: %d", h.URL, h.Status, h.StatusCode)
}

// TweetError is the group of errors in a response
type TweetError struct {
Parameters interface{} `json:"parameters"`
Expand Down Expand Up @@ -318,7 +329,11 @@ func (t *Tweet) Lookup(ctx context.Context, ids []string, options TweetFieldOpti
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := decoder.Decode(e); err != nil {
return nil, fmt.Errorf("tweet lookup response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -371,7 +386,11 @@ func (t *Tweet) RecentSearch(ctx context.Context, query string, searchOpts Tweet
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := decoder.Decode(e); err != nil {
return nil, fmt.Errorf("tweet recent search response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -427,7 +446,11 @@ func (t *Tweet) ApplyFilteredStreamRules(ctx context.Context, rules TweetSearchS
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := decoder.Decode(e); err != nil {
return nil, fmt.Errorf("tweet search stream rules response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -469,7 +492,11 @@ func (t *Tweet) FilteredStreamRules(ctx context.Context, ids []string) (*TweetSe
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := decoder.Decode(e); err != nil {
return nil, fmt.Errorf("tweet search stream rules response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -504,7 +531,11 @@ func (t *Tweet) FilteredStream(ctx context.Context, options TweetFieldOptions) (
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := decoder.Decode(e); err != nil {
return nil, fmt.Errorf("tweet lookup response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -539,7 +570,11 @@ func (t *Tweet) SampledStream(ctx context.Context, options TweetFieldOptions) (T
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := decoder.Decode(e); err != nil {
return nil, fmt.Errorf("tweet lookup response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -585,7 +620,11 @@ func (t *Tweet) HideReplies(ctx context.Context, id string, hidden bool) error {
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := decoder.Decode(e); err != nil {
return fmt.Errorf("tweet lookup response error decode: %w", err)
return &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return e
Expand Down
36 changes: 30 additions & 6 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ func (u *User) Lookup(ctx context.Context, ids []string, fieldOpts UserFieldOpti
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := decoder.Decode(e); err != nil {
return nil, fmt.Errorf("user lookup response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -227,7 +231,11 @@ func (u *User) LookupUsername(ctx context.Context, usernames []string, fieldOpts
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := decoder.Decode(e); err != nil {
return nil, fmt.Errorf("user lookup response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -282,7 +290,11 @@ func (u *User) LookupFollowing(ctx context.Context, id string, followOpts UserFo
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := json.Unmarshal(body, e); err != nil {
return nil, fmt.Errorf("user lookup response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -342,7 +354,11 @@ func (u *User) LookupFollowers(ctx context.Context, id string, followOpts UserFo
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := json.Unmarshal(body, e); err != nil {
return nil, fmt.Errorf("user lookup response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -402,7 +418,11 @@ func (u *User) Tweets(ctx context.Context, id string, tweetOpts UserTimelineOpts
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := json.Unmarshal(body, e); err != nil {
return nil, fmt.Errorf("user lookup response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down Expand Up @@ -450,7 +470,11 @@ func (u *User) Mentions(ctx context.Context, id string, tweetOpts UserTimelineOp
if resp.StatusCode != http.StatusOK {
e := &TweetErrorResponse{}
if err := json.Unmarshal(body, e); err != nil {
return nil, fmt.Errorf("user lookup response error decode: %w", err)
return nil, &HTTPError{
Status: resp.Status,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
e.StatusCode = resp.StatusCode
return nil, e
Expand Down

0 comments on commit ff9c93b

Please sign in to comment.