2022-01-13 14:28:50 +08:00
|
|
|
// Package api 关于所有第三方框架的二次封装
|
|
|
|
package api
|
2022-01-13 13:40:03 +08:00
|
|
|
|
|
|
|
import (
|
2022-01-13 14:28:50 +08:00
|
|
|
"context"
|
2022-01-13 14:48:28 +08:00
|
|
|
"fmt"
|
2022-01-13 13:40:03 +08:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2022-01-13 14:48:28 +08:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
|
|
|
ctxLogger "github.com/luizsuper/ctxLoggers"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"runtime"
|
2022-01-13 13:40:03 +08:00
|
|
|
)
|
|
|
|
|
2022-01-13 15:27:19 +08:00
|
|
|
// FiberMiddleWareErrHandler fiber 错误处理的中间件
|
|
|
|
func FiberMiddleWareErrHandler(ctx *fiber.Ctx, err error) error {
|
2022-01-13 13:40:03 +08:00
|
|
|
code := fiber.StatusInternalServerError
|
|
|
|
msg := ""
|
|
|
|
|
|
|
|
if e, ok := err.(*fiber.Error); ok {
|
|
|
|
code = e.Code
|
|
|
|
msg = e.Message
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
msg = err.Error()
|
|
|
|
}
|
|
|
|
|
2022-01-13 14:28:50 +08:00
|
|
|
return ctx.Status(code).JSON(Response{
|
2022-01-13 13:40:03 +08:00
|
|
|
Code: code,
|
|
|
|
Message: msg,
|
|
|
|
Data: nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-13 15:27:19 +08:00
|
|
|
// FibberMiddleWareRespHandler fiber resp的统一处理
|
|
|
|
func FibberMiddleWareRespHandler(c *fiber.Ctx) error {
|
2022-01-13 14:48:28 +08:00
|
|
|
err := c.Next()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-01-13 13:40:03 +08:00
|
|
|
}
|
|
|
|
|
2022-01-13 14:28:50 +08:00
|
|
|
if respContentType := string(c.Response().Header.ContentType()); respContentType == fiber.MIMEApplicationJSON {
|
|
|
|
resp := Response{
|
|
|
|
Code: 0,
|
|
|
|
Message: "",
|
|
|
|
Data: c.Response().Body(),
|
|
|
|
}
|
|
|
|
return c.JSON(resp)
|
|
|
|
}
|
|
|
|
|
2022-01-13 14:52:47 +08:00
|
|
|
return nil
|
2022-01-13 14:28:50 +08:00
|
|
|
}
|
|
|
|
|
2022-01-14 04:09:44 +08:00
|
|
|
// GetFibberRecoverConfig fiberPanicRecoverConfig
|
|
|
|
func GetFibberRecoverConfig() recover.Config {
|
|
|
|
return recover.Config{
|
2022-01-13 14:48:28 +08:00
|
|
|
EnableStackTrace: true,
|
|
|
|
StackTraceHandler: func(c *fiber.Ctx, e interface{}) {
|
|
|
|
buf := make([]byte, 1024*1024)
|
|
|
|
buf = buf[:runtime.Stack(buf, false)]
|
|
|
|
fmt.Printf("panic: %v\n%s\n", e, buf)
|
|
|
|
ctxLogger.FError(c, "panic", zap.String("", fmt.Sprintf("panic: %v\n%s\n", e, buf)))
|
|
|
|
},
|
2022-01-14 04:09:44 +08:00
|
|
|
}
|
2022-01-13 14:48:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FibberUserCtxWithTraceHandler 把fibberCtx的traceId 置入UserCtx中
|
|
|
|
func FibberUserCtxWithTraceHandler(c *fiber.Ctx) error {
|
2022-01-13 14:28:50 +08:00
|
|
|
c.SetUserContext(context.WithValue(context.Background(), fiber.HeaderXRequestID, c.Response().Header.Peek(fiber.HeaderXRequestID)))
|
|
|
|
return c.Next()
|
|
|
|
}
|
2022-01-13 13:40:03 +08:00
|
|
|
|
|
|
|
func NewFError(msg string) error {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFErrorWithCode(code int, msg string) error {
|
|
|
|
return fiber.NewError(code, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WarpFError(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func WarpFErrorWithCode(code int, err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fiber.NewError(code, err.Error())
|
|
|
|
}
|