Improved error handling

This commit is contained in:
video-prize-ranch 2022-09-13 16:31:55 -04:00
parent 1263e66f45
commit 5944b7253d
No known key found for this signature in database
GPG Key ID: D8EAA4C5B12A7281
7 changed files with 68 additions and 5 deletions

16
main.go
View File

@ -30,6 +30,22 @@ func main() {
Prefork: utils.Config["fiberPrefork"].(bool), Prefork: utils.Config["fiberPrefork"].(bool),
UnescapePath: true, UnescapePath: true,
StreamRequestBody: 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{ app.Use(recover.New(recover.Config{

View File

@ -25,7 +25,7 @@ func HandleEmbed(c *fiber.Ctx) error {
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", nil) 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) return c.Status(404).Render("errors/404", nil)
} }
if err != nil { if err != nil {

View File

@ -26,10 +26,10 @@ func HandlePost(c *fiber.Ctx) error {
if err != nil && err.Error() == "ratelimited by imgur" { if err != nil && err.Error() == "ratelimited by imgur" {
return c.Status(429).Render("errors/429", nil) 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) return c.Status(404).Render("errors/404", nil)
} }
if err != nil { if err != nil {
return err return err
} }

24
static/css/error.css Normal file
View File

@ -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;
}

View File

@ -4,7 +4,7 @@
<head> <head>
{{> 'partials/head' }} {{> 'partials/head' }}
<title>404 - rimgo</title> <title>404 Not Found - rimgo</title>
</head> </head>
<body> <body>

View File

@ -4,7 +4,7 @@
<head> <head>
{{> 'partials/head' }} {{> 'partials/head' }}
<title>404 - rimgo</title> <title>Rate limited by Imgur - rimgo</title>
</head> </head>
<body> <body>

23
views/errors/error.hbs Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
{{> 'partials/head' }}
<link rel="stylesheet" href="/static/css/error.css" />
<title>Error - rimgo</title>
</head>
<body>
{{> 'partials/header' }}
<main>
<h2 class="errorTitle">An error occurred</h2>
<p>You may have found a bug in rimgo. If this is a bug, open an issue on <a href="https://codeberg.org/video-prize-ranch/rimgo/issues/new">Codeberg</a>.</p>
<code>{{err}}</code>
</main>
</body>
</html>