Skip to content

Commit

Permalink
chore: implement stack trace handler in recover middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
zikani03 committed Sep 25, 2024
1 parent 2472c8c commit c8e82cd
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion internal/api/middleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package api

import (
"fmt"
"os"
"runtime/debug"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
Expand All @@ -17,7 +21,17 @@ func (api *API) middleware() {
AllowMethods: "*",
}))
app.Use(pprof.New())
app.Use(recover.New())

app.Use(recover.New(recover.Config{
EnableStackTrace: true,
StackTraceHandler: func(c *fiber.Ctx, e interface{}) {
if api.logger != nil {
api.logger.Error("failed to process a request", "errorStackTrace", fmt.Sprintf("panic: %v\n%s\n", e, debug.Stack()))
} else {
_, _ = os.Stderr.WriteString(fmt.Sprintf("panic: %v\n%s\n", e, debug.Stack())) //nolint:errcheck
}
},
}))
app.Use(logger.New(logger.Config{
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
}))
Expand Down

0 comments on commit c8e82cd

Please sign in to comment.