diff --git a/main.go b/main.go index 9f902f3..441d6ec 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,22 @@ func main() { Prefork: utils.Config["fiberPrefork"].(bool), UnescapePath: true, StreamRequestBody: true, + ErrorHandler: func(ctx *fiber.Ctx, err error) error { + code := fiber.StatusInternalServerError + + if e, ok := err.(*fiber.Error); ok { + code = e.Code + } + + err = ctx.Status(code).Render("errors/error", fiber.Map{ + "err": err, + }) + if err != nil { + return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error") + } + + return nil + }, }) app.Use(recover.New(recover.Config{ diff --git a/pages/embed.go b/pages/embed.go index d289585..456a725 100644 --- a/pages/embed.go +++ b/pages/embed.go @@ -25,7 +25,7 @@ func HandleEmbed(c *fiber.Ctx) error { 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")) { + if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") { return c.Status(404).Render("errors/404", nil) } if err != nil { diff --git a/pages/post.go b/pages/post.go index ac6cac6..85d744e 100644 --- a/pages/post.go +++ b/pages/post.go @@ -26,10 +26,10 @@ func HandlePost(c *fiber.Ctx) error { 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")) { + if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") { return c.Status(404).Render("errors/404", nil) } - if err != nil { + if err != nil { return err } diff --git a/static/css/error.css b/static/css/error.css new file mode 100644 index 0000000..19b29fc --- /dev/null +++ b/static/css/error.css @@ -0,0 +1,24 @@ +h2, p { + text-align: center; +} + +h2 { + margin-bottom: 0; +} + +main { + display: flex; + align-items: center; + flex-direction: column; +} + +code { + display: block; + padding: 0.5em; + background-color: #383838; + width: 50%; + margin: 0.5em auto; + word-wrap: break-word; + border-radius: 4px; + font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; +} \ No newline at end of file diff --git a/views/errors/404.hbs b/views/errors/404.hbs index 2cb1b83..eb19da4 100644 --- a/views/errors/404.hbs +++ b/views/errors/404.hbs @@ -4,7 +4,7 @@ {{> 'partials/head' }} - 404 - rimgo + 404 Not Found - rimgo diff --git a/views/errors/429.hbs b/views/errors/429.hbs index 2170c37..1b15f16 100644 --- a/views/errors/429.hbs +++ b/views/errors/429.hbs @@ -4,7 +4,7 @@ {{> 'partials/head' }} - 404 - rimgo + Rate limited by Imgur - rimgo diff --git a/views/errors/error.hbs b/views/errors/error.hbs new file mode 100644 index 0000000..39b1ed4 --- /dev/null +++ b/views/errors/error.hbs @@ -0,0 +1,23 @@ + + + + + {{> 'partials/head' }} + + + + Error - rimgo + + + + {{> 'partials/header' }} + +
+

An error occurred

+

You may have found a bug in rimgo. If this is a bug, open an issue on Codeberg.

+ {{err}} +
+ + + + \ No newline at end of file