♻️ Refactor: Improve FromContext error handling in session middleware

pull/3339/head
JIeJaitt 2025-03-12 16:34:33 +08:00
parent 14a8cc4220
commit e8b9fd632b
1 changed files with 5 additions and 6 deletions

View File

@ -8,7 +8,6 @@ import (
"sync"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
)
// Middleware holds session data and configuration.
@ -178,16 +177,16 @@ func releaseMiddleware(m *Middleware) {
// If there is no Middleware, nil is returned.
func FromContext(c any) *Middleware {
switch ctx := c.(type) {
case fiber.Ctx:
if m, ok := ctx.Locals(middlewareContextKey).(*Middleware); ok && m != nil {
return m
}
case context.Context:
if m, ok := ctx.Value(middlewareContextKey).(*Middleware); ok {
return m
}
case fiber.Ctx:
if m, ok := ctx.Locals(middlewareContextKey).(*Middleware); ok && m != nil {
return m
}
default:
log.Errorf("Unsupported context type: %T. Expected fiber.Ctx or context.Context", c)
panic("unsupported context type, expected fiber.Ctx or context.Context")
}
return nil
}