diff --git a/exerr/gin.go b/exerr/gin.go index e77def4..917ccdf 100644 --- a/exerr/gin.go +++ b/exerr/gin.go @@ -3,12 +3,13 @@ package exerr import ( "github.com/gin-gonic/gin" json "gogs.mikescher.com/BlackForestBytes/goext/gojson" + "gogs.mikescher.com/BlackForestBytes/goext/langext" "net/http" "time" ) -func (ee *ExErr) toJson(depth int) gin.H { - ginJson := gin.H{} +func (ee *ExErr) toJson(depth int, applyExtendListener bool) langext.H { + ginJson := langext.H{} if ee.UniqueID != "" { ginJson["id"] = ee.UniqueID @@ -38,21 +39,51 @@ func (ee *ExErr) toJson(depth int) gin.H { ginJson["wrappedErrType"] = ee.WrappedErrType } if ee.OriginalError != nil { - ginJson["original"] = ee.OriginalError.toJson(depth + 1) + ginJson["original"] = ee.OriginalError.toJson(depth+1, applyExtendListener) } - pkgconfig.ExtendGinDataOutput(ee, depth, ginJson) + if applyExtendListener { + pkgconfig.ExtendGinDataOutput(ee, depth, ginJson) + } return ginJson } +// ToAPIJson converts the ExError to a json object +// (the same object as used in the Output(gin) method) +// +// Parameters: +// - [applyExtendListener]: if false the pkgconfig.ExtendGinOutput / pkgconfig.ExtendGinDataOutput will not be applied +// - [includeWrappedErrors]: if false we do not include the recursive/wrapped errors in `__data` +func (ee *ExErr) ToAPIJson(applyExtendListener bool, includeWrappedErrors bool) langext.H { + + apiOutput := langext.H{ + "errorid": ee.UniqueID, + "message": ee.RecursiveMessage(), + "errorcode": ee.RecursiveType().Key, + "category": ee.RecursiveCategory().Category, + } + + if includeWrappedErrors { + apiOutput["__data"] = ee.toJson(0, applyExtendListener) + } + + if applyExtendListener { + pkgconfig.ExtendGinOutput(ee, apiOutput) + } + + return apiOutput +} + func (ee *ExErr) Output(g *gin.Context) { + + warnOnPkgConfigNotInitialized() + var statuscode = http.StatusInternalServerError var baseCat = ee.RecursiveCategory() var baseType = ee.RecursiveType() var baseStatuscode = ee.RecursiveStatuscode() - var baseMessage = ee.RecursiveMessage() if baseCat == CatUser { statuscode = http.StatusBadRequest @@ -66,20 +97,7 @@ func (ee *ExErr) Output(g *gin.Context) { statuscode = *baseType.DefaultStatusCode } - warnOnPkgConfigNotInitialized() - - ginOutput := gin.H{ - "errorid": ee.UniqueID, - "message": baseMessage, - "errorcode": baseType.Key, - "category": baseCat.Category, - } - - if pkgconfig.ExtendedGinOutput { - ginOutput["__data"] = ee.toJson(0) - } - - pkgconfig.ExtendGinOutput(ee, ginOutput) + ginOutput := ee.ToAPIJson(true, pkgconfig.ExtendedGinOutput) g.Render(statuscode, json.GoJsonRender{Data: ginOutput, NilSafeSlices: true, NilSafeMaps: true}) } diff --git a/goextVersion.go b/goextVersion.go index 7530483..5b8ef8b 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.214" +const GoextVersion = "0.0.215" -const GoextVersionTimestamp = "2023-07-27T09:58:10+0200" +const GoextVersionTimestamp = "2023-07-27T14:37:11+0200"