35 lines
798 B
Go
35 lines
798 B
Go
package misc
|
|
|
|
import (
|
|
"errors"
|
|
"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 New401Error() error {
|
|
return fiber.NewError(fiber.StatusUnauthorized, "401 Unauthorized")
|
|
}
|