Add error page for ratelimit
This commit is contained in:
parent
f0c1b7f4fe
commit
1263e66f45
|
@ -22,9 +22,11 @@ func HandleEmbed(c *fiber.Ctx) error {
|
|||
default:
|
||||
post, err = api.FetchMedia(c.Params("postID"))
|
||||
}
|
||||
if err != nil && err.Error() == "ratelimited by imgur" {
|
||||
return c.Status(429).Render("errors/429", nil)
|
||||
}
|
||||
if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) {
|
||||
c.Status(404)
|
||||
return c.Render("errors/404", nil)
|
||||
return c.Status(404).Render("errors/404", nil)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -23,9 +23,11 @@ func HandlePost(c *fiber.Ctx) error {
|
|||
default:
|
||||
post, err = api.FetchMedia(c.Params("postID"))
|
||||
}
|
||||
if err != nil && err.Error() == "ratelimited by imgur" {
|
||||
return c.Status(429).Render("errors/429", nil)
|
||||
}
|
||||
if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) {
|
||||
c.Status(404)
|
||||
return c.Render("errors/404", nil)
|
||||
return c.Status(404).Render("errors/404", nil)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -30,12 +30,14 @@ func HandleTag(c *fiber.Ctx) error {
|
|||
}
|
||||
|
||||
tag, err := api.FetchTag(c.Params("tag"), c.Query("sort"), page)
|
||||
if err != nil && err.Error() == "ratelimited by imgur" {
|
||||
return c.Status(429).Render("errors/429", nil)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tag.Display == "" {
|
||||
c.Status(404)
|
||||
return c.Render("errors/404", nil)
|
||||
return c.Status(404).Render("errors/404", nil)
|
||||
}
|
||||
|
||||
return c.Render("tag", fiber.Map{
|
||||
|
|
|
@ -2,7 +2,6 @@ package pages
|
|||
|
||||
import (
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/api"
|
||||
"codeberg.org/video-prize-ranch/rimgo/utils"
|
||||
|
@ -25,30 +24,24 @@ func HandleUser(c *fiber.Ctx) error {
|
|||
pageNumber = 0
|
||||
}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(2)
|
||||
user, err := api.User{}, error(nil)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
user, err = api.FetchUser(c.Params("userID"))
|
||||
}()
|
||||
user, err := api.FetchUser(c.Params("userID"))
|
||||
if err != nil && err.Error() == "ratelimited by imgur" {
|
||||
return c.Status(429).Render("errors/429", nil)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
submissions, err := []api.Submission{}, error(nil)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
submissions, err = api.FetchSubmissions(c.Params("userID"), "newest", page)
|
||||
}()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
if user.Username == "" {
|
||||
c.Status(404)
|
||||
return c.Render("errors/404", nil)
|
||||
return c.Status(404).Render("errors/404", nil)
|
||||
}
|
||||
|
||||
submissions, err := api.FetchSubmissions(c.Params("userID"), "newest", page)
|
||||
if err != nil && err.Error() == "ratelimited by imgur" {
|
||||
c.Status(429)
|
||||
return c.Render("errors/429", nil)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Render("user", fiber.Map{
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
@ -30,6 +31,13 @@ func GetJSON(url string) (gjson.Result, error) {
|
|||
if err != nil {
|
||||
return gjson.Result{}, err
|
||||
}
|
||||
rateLimitRemaining := res.Header.Get("X-RateLimit-UserRemaining")
|
||||
if rateLimitRemaining != "" {
|
||||
ratelimit, _ := strconv.Atoi(rateLimitRemaining)
|
||||
if ratelimit <= 0 {
|
||||
return gjson.Result{}, fmt.Errorf("ratelimited by imgur")
|
||||
}
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
{{> 'partials/head' }}
|
||||
|
||||
<title>404 - rimgo</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
{{> 'partials/header' }}
|
||||
|
||||
<main>
|
||||
<h2 class="errorTitle">Rate limited by Imgur</h2>
|
||||
<p class="errorTitle">This instance has been temporarily blocked by Imgur. <a href="https://codeberg.org/video-prize-ranch/rimgo#instances">Try using another instance</a>.</p>
|
||||
</main>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue