goext/ginext/funcWrapper.go
Mike Schwörer 3188b951fb
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m35s
v0.0.348 added listener and options to goext
2023-12-27 20:29:37 +01:00

61 lines
1.3 KiB
Go

package ginext
import (
"fmt"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
)
type WHandlerFunc func(PreContext) HTTPResponse
func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
return func(g *gin.Context) {
reqctx := g.Request.Context()
pctx := PreContext{
wrapper: w,
ginCtx: g,
persistantData: &preContextData{},
}
for _, lstr := range w.listenerBeforeRequest {
lstr(g)
}
wrap, stackTrace, panicObj := callPanicSafe(fn, pctx)
if panicObj != nil {
fmt.Printf("\n======== ======== STACKTRACE ======== ========\n%s\n======== ======== ======== ========\n\n", stackTrace)
err := exerr.
New(exerr.TypePanic, "Panic occured (in gin handler)").
Any("panicObj", panicObj).
Str("trace", stackTrace).
Build()
wrap = Error(err)
}
if g.Writer.Written() {
panic("Writing in WrapperFunc is not supported")
}
if pctx.persistantData.sessionObj != nil {
err := pctx.persistantData.sessionObj.Finish(reqctx, wrap)
if err != nil {
wrap = Error(exerr.Wrap(err, "Failed to finish session").Any("originalResponse", wrap).Build())
}
}
for _, lstr := range w.listenerAfterRequest {
lstr(g, wrap)
}
if reqctx.Err() == nil {
wrap.Write(g)
}
}
}