diff --git a/app/brahma/api/murder/main.go b/app/brahma/api/murder/main.go index 7e043bf..4801337 100755 --- a/app/brahma/api/murder/main.go +++ b/app/brahma/api/murder/main.go @@ -8,6 +8,7 @@ import ( "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/handler" "git.icechen.cn/monorepo/backend/pkg/api" "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/recover" "github.com/gofiber/fiber/v2/middleware/requestid" ctxLogger "github.com/luizsuper/ctxLoggers" "go.uber.org/zap" @@ -22,11 +23,8 @@ const ( var ( err error - //Init App app = fiber.New(fiber.Config{ - ErrorHandler: func(ctx *fiber.Ctx, err error) error { - return ctx.Status(fiber.StatusInternalServerError).JSON(handler.Response{Message: "服务器内部错误"}) - }, + ErrorHandler: api.FiberMiddleWareErrHandler, }) ) @@ -49,12 +47,10 @@ func ResourceInit() { } func appInit() { - //requestId app.Use(requestid.New()) - //recover - app.Use(api.FibberMiddleWareRecoverHandler) - //requestId for gorm app.Use(api.FibberUserCtxWithTraceHandler) + app.Use(api.FibberMiddleWareRespHandler) + app.Use(recover.New(api.GetFibberRecoverConfig())) } func routerInit() { diff --git a/pkg/api/fiber.go b/pkg/api/fiber.go index dd91f1e..bb035da 100644 --- a/pkg/api/fiber.go +++ b/pkg/api/fiber.go @@ -50,9 +50,9 @@ func FibberMiddleWareRespHandler(c *fiber.Ctx) error { return nil } -// FibberMiddleWareRecoverHandler fiberPanicRecover -func FibberMiddleWareRecoverHandler() fiber.Handler { - return recover.New(recover.Config{ +// GetFibberRecoverConfig fiberPanicRecoverConfig +func GetFibberRecoverConfig() recover.Config { + return recover.Config{ EnableStackTrace: true, StackTraceHandler: func(c *fiber.Ctx, e interface{}) { buf := make([]byte, 1024*1024) @@ -60,7 +60,7 @@ func FibberMiddleWareRecoverHandler() fiber.Handler { fmt.Printf("panic: %v\n%s\n", e, buf) ctxLogger.FError(c, "panic", zap.String("", fmt.Sprintf("panic: %v\n%s\n", e, buf))) }, - }) + } } // FibberUserCtxWithTraceHandler 把fibberCtx的traceId 置入UserCtx中