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
cancelFunc context.CancelFunc
cancelled bool
ginContext *gin.Context
GinContext *gin.Context
}
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,
cancelFunc: cancelFn,
cancelled: false,
ginContext: g,
GinContext: g,
}
}
@ -44,8 +44,8 @@ func (ac *AppContext) Cancel() {
}
func (ac *AppContext) RequestURI() string {
if ac.ginContext != nil && ac.ginContext.Request != nil {
return ac.ginContext.Request.Method + " :: " + ac.ginContext.Request.RequestURI
if ac.GinContext != nil && ac.GinContext.Request != nil {
return ac.GinContext.Request.Method + " :: " + ac.GinContext.Request.RequestURI
} else {
return ""
}

View File

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

View File

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

View File

@ -4,7 +4,6 @@ import (
"context"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug"
)
@ -41,33 +40,33 @@ func (pctx *PreContext) Form(form any) *PreContext {
func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
if pctx.uri != 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 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.ginCtx.ContentType() == "application/json" {
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 {
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.ginCtx.ContentType() == "multipart/form-data" {
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 {
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"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr"
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug"
@ -103,15 +102,15 @@ func Redirect(sc int, newURL string) HTTPResponse {
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)
}
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 := ""
if g != nil && g.Request != nil {
reqUri = g.Request.Method + " :: " + g.Request.RequestURI

View File

@ -1,5 +1,5 @@
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"