goext/ginext/funcWrapper.go

39 lines
946 B
Go
Raw Permalink Normal View History

2023-07-18 14:40:10 +02:00
package ginext
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
2023-07-18 15:12:06 +02:00
type WHandlerFunc func(PreContext) HTTPResponse
2023-07-18 14:40:10 +02:00
func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
return func(g *gin.Context) {
g.Set("__returnRawErrors", w.returnRawErrors)
reqctx := g.Request.Context()
2023-07-18 15:12:06 +02:00
wrap, stackTrace, panicObj := callPanicSafe(fn, PreContext{wrapper: w, ginCtx: g})
2023-07-18 14:40:10 +02:00
if panicObj != nil {
fmt.Printf("\n======== ======== STACKTRACE ======== ========\n%s\n======== ======== ======== ========\n\n", stackTrace)
log.Error().
Interface("panicObj", panicObj).
Str("trace", stackTrace).
Msg("Panic occured (in gin handler)")
2023-07-18 15:59:12 +02:00
wrap = APIError(g, commonApiErr.Panic, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v", panicObj)))
2023-07-18 14:40:10 +02:00
}
if g.Writer.Written() {
panic("Writing in WrapperFunc is not supported")
}
if reqctx.Err() == nil {
wrap.Write(g)
}
}
}