Skip to content

Commit

Permalink
fix: Add message for Reddit 403 response
Browse files Browse the repository at this point in the history
When Reddit API responds with error, the body may be formatted as
HTML and contains details about the problem.
  • Loading branch information
macie committed Jan 8, 2024
1 parent 690481f commit 001a20a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions reddit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/url"

"github.com/macie/opinions/html"
"github.com/macie/opinions/http"
)

Expand Down Expand Up @@ -55,6 +56,17 @@ func SearchReddit(ctx context.Context, client http.Client, query string) ([]Disc
return discussions, fmt.Errorf("cannot search Reddit: too many requests. Wait %s seconds", r.Header.Get("X-Ratelimit-Reset"))
}

if r.StatusCode == 403 {
var details string
body, err := html.Parse(r.Body)
if err != nil {
details = ""
}
details = html.Text(html.First(body, "p"))

return discussions, fmt.Errorf("cannot search Reddit: your IP address seems to be banned by Reddit: `GET %s` responded with `%s`", r.Request.URL, details)
}

return discussions, fmt.Errorf("cannot search Reddit: `GET %s` responded with status code %d", r.Request.URL, r.StatusCode)
}

Expand Down

0 comments on commit 001a20a

Please sign in to comment.