v0.0.233 IncludeMetaInGinOutput

This commit is contained in:
2023-08-09 10:37:59 +02:00
parent ac416f7b69
commit 99cd92729e
4 changed files with 57 additions and 13 deletions

View File

@@ -8,7 +8,7 @@ import (
"time"
)
func (ee *ExErr) toJson(depth int, applyExtendListener bool) langext.H {
func (ee *ExErr) toJson(depth int, applyExtendListener bool, outputMeta bool) langext.H {
ginJson := langext.H{}
if ee.UniqueID != "" {
@@ -39,7 +39,15 @@ func (ee *ExErr) toJson(depth int, applyExtendListener bool) langext.H {
ginJson["wrappedErrType"] = ee.WrappedErrType
}
if ee.OriginalError != nil {
ginJson["original"] = ee.OriginalError.toJson(depth+1, applyExtendListener)
ginJson["original"] = ee.OriginalError.toJson(depth+1, applyExtendListener, outputMeta)
}
if outputMeta {
metaJson := langext.H{}
for metaKey, metaVal := range ee.Meta {
metaJson[metaKey] = metaVal.rawValueForJson()
}
ginJson["meta"] = metaJson
}
if applyExtendListener {
@@ -55,7 +63,8 @@ func (ee *ExErr) toJson(depth int, applyExtendListener bool) langext.H {
// 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 {
// - [includeMetaFields]: if true we also include meta-values (aka from `.Str(key, value).Build()`), needs includeWrappedErrors=true
func (ee *ExErr) ToAPIJson(applyExtendListener bool, includeWrappedErrors bool, includeMetaFields bool) langext.H {
apiOutput := langext.H{
"errorid": ee.UniqueID,
@@ -65,7 +74,7 @@ func (ee *ExErr) ToAPIJson(applyExtendListener bool, includeWrappedErrors bool)
}
if includeWrappedErrors {
apiOutput["__data"] = ee.toJson(0, applyExtendListener)
apiOutput["__data"] = ee.toJson(0, applyExtendListener, includeMetaFields)
}
if applyExtendListener {
@@ -97,7 +106,7 @@ func (ee *ExErr) Output(g *gin.Context) {
statuscode = *baseType.DefaultStatusCode
}
ginOutput := ee.ToAPIJson(true, pkgconfig.ExtendedGinOutput)
ginOutput := ee.ToAPIJson(true, pkgconfig.ExtendedGinOutput, pkgconfig.IncludeMetaInGinOutput)
g.Render(statuscode, json.GoJsonRender{Data: ginOutput, NilSafeSlices: true, NilSafeMaps: true})
}