From 6267dc56f2fbccf87ebf9b5daad6fed6e7a0207c Mon Sep 17 00:00:00 2001 From: liuhaotian Date: Fri, 14 Jan 2022 04:09:44 +0800 Subject: [PATCH] =?UTF-8?q?fix=20:=20brahma=20api=20=E4=B8=AD=E9=97=B4?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/brahma/api/murder/main.go | 12 ++++-------- pkg/api/fiber.go | 8 ++++---- 2 files changed, 8 insertions(+), 12 deletions(-) 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中