v0.0.172
This commit is contained in:
53
ginext/funcWrapper.go
Normal file
53
ginext/funcWrapper.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package ginext
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/ginext/apierr"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
type WHandlerFunc func(*gin.Context) HTTPResponse
|
||||
|
||||
func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
|
||||
|
||||
return func(g *gin.Context) {
|
||||
|
||||
g.Set("__returnRawErrors", w.returnRawErrors)
|
||||
|
||||
reqctx := g.Request.Context()
|
||||
|
||||
wrap, stackTrace, panicObj := callPanicSafe(fn, g)
|
||||
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)")
|
||||
wrap = APIError(g, apierr.Panic, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v", panicObj)))
|
||||
}
|
||||
|
||||
if g.Writer.Written() {
|
||||
panic("Writing in WrapperFunc is not supported")
|
||||
}
|
||||
|
||||
if reqctx.Err() == nil {
|
||||
wrap.Write(g)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func callPanicSafe(fn WHandlerFunc, g *gin.Context) (res HTTPResponse, stackTrace string, panicObj any) {
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
res = nil
|
||||
stackTrace = string(debug.Stack())
|
||||
panicObj = rec
|
||||
}
|
||||
}()
|
||||
|
||||
res = fn(g)
|
||||
return res, "", nil
|
||||
}
|
Reference in New Issue
Block a user