v0.0.173
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
package ginext
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/ginext/apierr"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
type WHandlerFunc func(*gin.Context) HTTPResponse
|
||||
type WHandlerFunc func(PreContext) HTTPResponse
|
||||
|
||||
func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
|
||||
|
||||
@@ -19,14 +22,14 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
|
||||
|
||||
reqctx := g.Request.Context()
|
||||
|
||||
wrap, stackTrace, panicObj := callPanicSafe(fn, g)
|
||||
wrap, stackTrace, panicObj := callPanicSafe(fn, PreContext{wrapper: w, ginCtx: 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)))
|
||||
wrap = APIError(g, commonapierr.Panic, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v", panicObj)))
|
||||
}
|
||||
|
||||
if g.Writer.Written() {
|
||||
@@ -39,7 +42,75 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func callPanicSafe(fn WHandlerFunc, g *gin.Context) (res HTTPResponse, stackTrace string, panicObj any) {
|
||||
type PreContext struct {
|
||||
ginCtx *gin.Context
|
||||
wrapper *GinWrapper
|
||||
uri any
|
||||
query any
|
||||
body any
|
||||
form any
|
||||
}
|
||||
|
||||
func (pctx *PreContext) URI(uri any) *PreContext {
|
||||
pctx.uri = uri
|
||||
return pctx
|
||||
}
|
||||
|
||||
func (pctx *PreContext) Query(query any) *PreContext {
|
||||
pctx.query = query
|
||||
return pctx
|
||||
}
|
||||
|
||||
func (pctx *PreContext) Body(body any) *PreContext {
|
||||
pctx.body = body
|
||||
return pctx
|
||||
}
|
||||
|
||||
func (pctx *PreContext) Form(form any) *PreContext {
|
||||
pctx.form = form
|
||||
return pctx
|
||||
}
|
||||
|
||||
func (pctx PreContext) Start() (*AppContext, *HTTPResponse) {
|
||||
if pctx.uri != nil {
|
||||
if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil {
|
||||
return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailURI, "Failed to read uri", err))
|
||||
}
|
||||
}
|
||||
|
||||
if pctx.query != nil {
|
||||
if err := pctx.ginCtx.ShouldBindQuery(pctx.query); err != nil {
|
||||
return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailQuery, "Failed to read query", err))
|
||||
}
|
||||
}
|
||||
|
||||
if pctx.body != nil {
|
||||
if pctx.ginCtx.ContentType() == "application/json" {
|
||||
if err := pctx.ginCtx.ShouldBindJSON(pctx.body); err != nil {
|
||||
return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailJSON, "Failed to read body", err))
|
||||
}
|
||||
} else {
|
||||
return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailJSON, "missing JSON body", nil))
|
||||
}
|
||||
}
|
||||
|
||||
if pctx.form != nil {
|
||||
if pctx.ginCtx.ContentType() == "multipart/form-data" {
|
||||
if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil {
|
||||
return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailFormData, "Failed to read multipart-form", err))
|
||||
}
|
||||
} else {
|
||||
return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailJSON, "missing form body", nil))
|
||||
}
|
||||
}
|
||||
|
||||
ictx, cancel := context.WithTimeout(context.Background(), pctx.wrapper.requestTimeout)
|
||||
actx := CreateAppContext(pctx.ginCtx, ictx, cancel)
|
||||
|
||||
return actx, nil
|
||||
}
|
||||
|
||||
func callPanicSafe(fn WHandlerFunc, pctx PreContext) (res HTTPResponse, stackTrace string, panicObj any) {
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
res = nil
|
||||
@@ -48,6 +119,6 @@ func callPanicSafe(fn WHandlerFunc, g *gin.Context) (res HTTPResponse, stackTrac
|
||||
}
|
||||
}()
|
||||
|
||||
res = fn(g)
|
||||
res = fn(pctx)
|
||||
return res, "", nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user