43 lines
1 KiB
Go
43 lines
1 KiB
Go
package misc
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"code.lila.network/adoralaura/go-urlsh/models"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func HandleError(c *fiber.Ctx, err error) error {
|
|
code := fiber.StatusInternalServerError
|
|
|
|
// Retrieve the custom status code if it's a *fiber.Error
|
|
var e *fiber.Error
|
|
if errors.As(err, &e) {
|
|
code = e.Code
|
|
}
|
|
|
|
// Set Content-Type: text/plain; charset=utf-8
|
|
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
|
|
|
|
// Return status code with error message
|
|
return c.Status(code).SendString(err.Error())
|
|
|
|
}
|
|
|
|
func New500Error() error {
|
|
return fiber.NewError(fiber.StatusInternalServerError, "500 Internal Server Error")
|
|
}
|
|
|
|
func New400Error() error {
|
|
return fiber.NewError(fiber.StatusBadRequest, "400 Bad Request")
|
|
}
|
|
|
|
func New400WithMessageError(msg string) error {
|
|
body, _ := json.Marshal(models.HttpErrorBody{Message: msg})
|
|
return fiber.NewError(fiber.StatusBadRequest, string(body[:]))
|
|
}
|
|
|
|
func New401Error() error {
|
|
return fiber.NewError(fiber.StatusUnauthorized, "401 Unauthorized")
|
|
}
|