This commit is contained in:
Mike Schwörer 2023-07-18 15:59:12 +02:00
parent d78550672e
commit 8d0ef0f002
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
6 changed files with 17 additions and 20 deletions

View File

@ -10,7 +10,7 @@ type AppContext struct {
inner context.Context inner context.Context
cancelFunc context.CancelFunc cancelFunc context.CancelFunc
cancelled bool cancelled bool
ginContext *gin.Context GinContext *gin.Context
} }
func CreateAppContext(g *gin.Context, innerCtx context.Context, cancelFn context.CancelFunc) *AppContext { func CreateAppContext(g *gin.Context, innerCtx context.Context, cancelFn context.CancelFunc) *AppContext {
@ -18,7 +18,7 @@ func CreateAppContext(g *gin.Context, innerCtx context.Context, cancelFn context
inner: innerCtx, inner: innerCtx,
cancelFunc: cancelFn, cancelFunc: cancelFn,
cancelled: false, cancelled: false,
ginContext: g, GinContext: g,
} }
} }
@ -44,8 +44,8 @@ func (ac *AppContext) Cancel() {
} }
func (ac *AppContext) RequestURI() string { func (ac *AppContext) RequestURI() string {
if ac.ginContext != nil && ac.ginContext.Request != nil { if ac.GinContext != nil && ac.GinContext.Request != nil {
return ac.ginContext.Request.Method + " :: " + ac.ginContext.Request.RequestURI return ac.GinContext.Request.Method + " :: " + ac.GinContext.Request.RequestURI
} else { } else {
return "" return ""
} }

View File

@ -1,4 +1,4 @@
package commonapierr package commonApiErr
type APIErrorCode struct { type APIErrorCode struct {
HTTPStatusCode int HTTPStatusCode int

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr"
) )
type WHandlerFunc func(PreContext) HTTPResponse type WHandlerFunc func(PreContext) HTTPResponse
@ -25,7 +24,7 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
Interface("panicObj", panicObj). Interface("panicObj", panicObj).
Str("trace", stackTrace). Str("trace", stackTrace).
Msg("Panic occured (in gin handler)") Msg("Panic occured (in gin handler)")
wrap = APIError(g, commonapierr.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() { if g.Writer.Written() {

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug" "runtime/debug"
) )
@ -41,33 +40,33 @@ func (pctx *PreContext) Form(form any) *PreContext {
func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
if pctx.uri != nil { if pctx.uri != nil {
if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil { if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailURI, "Failed to read uri", err)) return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailURI, "Failed to read uri", err))
} }
} }
if pctx.query != nil { if pctx.query != nil {
if err := pctx.ginCtx.ShouldBindQuery(pctx.query); err != nil { if err := pctx.ginCtx.ShouldBindQuery(pctx.query); err != nil {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailQuery, "Failed to read query", err)) return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailQuery, "Failed to read query", err))
} }
} }
if pctx.body != nil { if pctx.body != nil {
if pctx.ginCtx.ContentType() == "application/json" { if pctx.ginCtx.ContentType() == "application/json" {
if err := pctx.ginCtx.ShouldBindJSON(pctx.body); err != nil { if err := pctx.ginCtx.ShouldBindJSON(pctx.body); err != nil {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailJSON, "Failed to read body", err)) return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailJSON, "Failed to read body", err))
} }
} else { } else {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailJSON, "missing JSON body", nil)) return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailJSON, "missing JSON body", nil))
} }
} }
if pctx.form != nil { if pctx.form != nil {
if pctx.ginCtx.ContentType() == "multipart/form-data" { if pctx.ginCtx.ContentType() == "multipart/form-data" {
if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil { if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailFormData, "Failed to read multipart-form", err)) return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailFormData, "Failed to read multipart-form", err))
} }
} else { } else {
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailJSON, "missing form body", nil)) return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailJSON, "missing form body", nil))
} }
} }

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr"
json "gogs.mikescher.com/BlackForestBytes/goext/gojson" json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug" "runtime/debug"
@ -103,15 +102,15 @@ func Redirect(sc int, newURL string) HTTPResponse {
return &redirectHTTPResponse{statusCode: sc, url: newURL} return &redirectHTTPResponse{statusCode: sc, url: newURL}
} }
func APIError(g *gin.Context, errcode commonapierr.APIErrorCode, msg string, e error) HTTPResponse { func APIError(g *gin.Context, errcode commonApiErr.APIErrorCode, msg string, e error) HTTPResponse {
return createApiError(g, errcode, msg, e) return createApiError(g, errcode, msg, e)
} }
func NotImplemented(g *gin.Context) HTTPResponse { func NotImplemented(g *gin.Context) HTTPResponse {
return createApiError(g, commonapierr.NotImplemented, "", nil) return createApiError(g, commonApiErr.NotImplemented, "", nil)
} }
func createApiError(g *gin.Context, errcode commonapierr.APIErrorCode, msg string, e error) HTTPResponse { func createApiError(g *gin.Context, errcode commonApiErr.APIErrorCode, msg string, e error) HTTPResponse {
reqUri := "" reqUri := ""
if g != nil && g.Request != nil { if g != nil && g.Request != nil {
reqUri = g.Request.Method + " :: " + g.Request.RequestURI reqUri = g.Request.Method + " :: " + g.Request.RequestURI

View File

@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.174" const GoextVersion = "0.0.175"
const GoextVersionTimestamp = "2023-07-18T15:23:32+0200" const GoextVersionTimestamp = "2023-07-18T15:59:12+0200"