2024-05-14 20:36:48 +08:00
|
|
|
package web
|
|
|
|
|
2024-08-26 16:06:43 +08:00
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2024-05-14 20:36:48 +08:00
|
|
|
type ListResponse[E any] struct {
|
|
|
|
Total int64 `json:"total"`
|
|
|
|
List []E `json:"list"`
|
|
|
|
}
|
2024-08-26 16:06:43 +08:00
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Msg string `json:"msg,omitempty"`
|
|
|
|
Data interface{} `json:"data,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrHandle(ctx fiber.Ctx, err error) error {
|
|
|
|
// Status code defaults to 500
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send custom error page
|
|
|
|
err = ctx.Status(code).JSON(Response{
|
|
|
|
Code: code,
|
|
|
|
Msg: err.Error(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// In case the SendFile fails
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return from handler
|
|
|
|
return nil
|
|
|
|
}
|