goext/ginext/funcWrapper.go

40 lines
841 B
Go
Raw Permalink Normal View History

2023-07-18 14:40:10 +02:00
package ginext
import (
"fmt"
"github.com/gin-gonic/gin"
2023-07-24 11:11:15 +02:00
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
2023-07-18 14:40:10 +02:00
)
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) {
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 {
2023-07-24 11:11:15 +02:00
2023-07-18 14:40:10 +02:00
fmt.Printf("\n======== ======== STACKTRACE ======== ========\n%s\n======== ======== ======== ========\n\n", stackTrace)
2023-07-24 11:11:15 +02:00
err := exerr.
New(exerr.TypePanic, "Panic occured (in gin handler)").
Any("panicObj", panicObj).
2023-07-18 14:40:10 +02:00
Str("trace", stackTrace).
2023-07-24 11:11:15 +02:00
Build()
wrap = APIError(g, err)
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)
}
}
}