1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package controllers
import (
"errors"
"imageboard/config"
"imageboard/utils/shortcuts"
"strings"
"github.com/gofiber/fiber/v2"
)
type TemplateError struct {
Template string
ErrorMessage string
StatusCode int
}
func TemplateErrorController(ctx *fiber.Ctx, err TemplateError, bind fiber.Map) error {
bind["Error"] = err.ErrorMessage
return shortcuts.RenderWithStatus(ctx, err.Template, bind, err.StatusCode)
}
func GenericErrorController(ctx *fiber.Ctx, title string, err error, statusCode int) error {
ctx.Locals("Title", title)
if strings.HasSuffix(ctx.Path(), ".json") {
return ctx.Status(statusCode).JSON(fiber.Map{
"error": err.Error(),
})
}
if len(ctx.Path()) > 1 && strings.Contains(ctx.Path()[1:], ".") && !strings.HasSuffix(ctx.Path(), ".html") {
return ctx.SendStatus(statusCode)
}
return shortcuts.RenderWithStatus(ctx, config.TEMPLATE_ERROR, fiber.Map{
"Title": title,
"Error": err.Error(),
}, statusCode)
}
func NotFoundController(ctx *fiber.Ctx) error {
error := errors.New("The page you are looking for does not exist.")
return GenericErrorController(ctx, "Page Not Found", error, fiber.StatusNotFound)
}
func InternalServerErrorController(ctx *fiber.Ctx, err error) error {
return GenericErrorController(ctx, "Internal Server Error", err, fiber.StatusInternalServerError)
}
func BadRequestController(ctx *fiber.Ctx, err error) error {
return GenericErrorController(ctx, "Bad Request", err, fiber.StatusBadRequest)
}
func UnauthorizedController(ctx *fiber.Ctx, err error) error {
return GenericErrorController(ctx, "Unauthorized", err, fiber.StatusUnauthorized)
}
func ForbiddenController(ctx *fiber.Ctx, err error) error {
return GenericErrorController(ctx, "Forbidden", err, fiber.StatusForbidden)
}
|