From 1586314e3e000cf41b62740f5d3cba43e7b96612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 16 Jul 2025 17:13:07 +0200 Subject: [PATCH] v0.0.594 Add exerr OutputRaw(http.ResponseWriter) method --- exerr/builder.go | 25 +++++++++++++++++++++++-- exerr/gin.go | 34 ++++++++++++++++++++++++++++++++-- goextVersion.go | 4 ++-- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/exerr/builder.go b/exerr/builder.go index 3224f9e..ef6c132 100644 --- a/exerr/builder.go +++ b/exerr/builder.go @@ -456,10 +456,19 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) { // this is only here to add one level to the trace // so that .Build() and .Output() and .Print() have the same depth and our stack-skip logger can have the same skip-count - b.doOutput(ctx, g) + b.doGinOutput(ctx, g) } -func (b *Builder) doOutput(ctx context.Context, g *gin.Context) { +// OutputRaw works teh same as Output() - but does not depend on gin and works with a raw http.ResponseWriter +func (b *Builder) OutputRaw(w http.ResponseWriter) { + warnOnPkgConfigNotInitialized() + + // this is only here to add one level to the trace + // so that .Build() and .Output() and .Print() have the same depth and our stack-skip logger can have the same skip-count + b.doRawOutput(w) +} + +func (b *Builder) doGinOutput(ctx context.Context, g *gin.Context) { b.errorData.Output(g) if (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) && (pkgconfig.ZeroLogErrGinOutput || pkgconfig.ZeroLogAllGinOutput) { @@ -471,6 +480,18 @@ func (b *Builder) doOutput(ctx context.Context, g *gin.Context) { b.errorData.CallListener(MethodOutput, ListenerOpt{NoLog: b.noLog}) } +func (b *Builder) doRawOutput(w http.ResponseWriter) { + b.errorData.OutputRaw(w) + + if (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) && (pkgconfig.ZeroLogErrGinOutput || pkgconfig.ZeroLogAllGinOutput) { + b.errorData.Log(pkgconfig.ZeroLogger.Error()) + } else if (b.errorData.Severity == SevWarn) && (pkgconfig.ZeroLogAllGinOutput) { + b.errorData.Log(pkgconfig.ZeroLogger.Warn()) + } + + b.errorData.CallListener(MethodOutput, ListenerOpt{NoLog: b.noLog}) +} + // Print prints the error // If the error is SevErr we also send it to the error-service func (b *Builder) Print(ctxs ...context.Context) Proxy { diff --git a/exerr/gin.go b/exerr/gin.go index 1a04422..662ecb4 100644 --- a/exerr/gin.go +++ b/exerr/gin.go @@ -1,9 +1,9 @@ package exerr import ( - "github.com/gin-gonic/gin" json "git.blackforestbytes.com/BlackForestBytes/goext/gojson" "git.blackforestbytes.com/BlackForestBytes/goext/langext" + "github.com/gin-gonic/gin" "net/http" "time" ) @@ -68,7 +68,6 @@ func (ee *ExErr) ToDefaultAPIJson() (string, error) { gjr := json.GoJsonRender{Data: ee.ToAPIJson(true, pkgconfig.ExtendedGinOutput, pkgconfig.IncludeMetaInGinOutput), NilSafeSlices: true, NilSafeMaps: true} r, err := gjr.RenderString() - if err != nil { return "", err } @@ -143,3 +142,34 @@ func (ee *ExErr) Output(g *gin.Context) { g.Render(statuscode, json.GoJsonRender{Data: ginOutput, NilSafeSlices: true, NilSafeMaps: true}) } + +func (ee *ExErr) OutputRaw(w http.ResponseWriter) { + + warnOnPkgConfigNotInitialized() + + var statuscode = http.StatusInternalServerError + + var baseCat = ee.RecursiveCategory() + var baseType = ee.RecursiveType() + var baseStatuscode = ee.RecursiveStatuscode() + + if baseCat == CatUser { + statuscode = http.StatusBadRequest + } else if baseCat == CatSystem { + statuscode = http.StatusInternalServerError + } + + if baseStatuscode != nil { + statuscode = *ee.StatusCode + } else if baseType.DefaultStatusCode != nil { + statuscode = *baseType.DefaultStatusCode + } + + ginOutput, err := ee.ToDefaultAPIJson() + if err != nil { + panic(err) // cannot happen + } + + w.WriteHeader(statuscode) + _, _ = w.Write([]byte(ginOutput)) +} diff --git a/goextVersion.go b/goextVersion.go index e06a89e..516bf2e 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.593" +const GoextVersion = "0.0.594" -const GoextVersionTimestamp = "2025-07-16T12:50:36+0200" +const GoextVersionTimestamp = "2025-07-16T17:13:07+0200"