v0.0.215 Add (ee *ExErr) ToAPIJson

This commit is contained in:
Mike Schwörer 2023-07-27 14:37:11 +02:00
parent 3b30bb049e
commit 91f4793678
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 39 additions and 21 deletions

View File

@ -3,12 +3,13 @@ package exerr
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
json "gogs.mikescher.com/BlackForestBytes/goext/gojson" json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http" "net/http"
"time" "time"
) )
func (ee *ExErr) toJson(depth int) gin.H { func (ee *ExErr) toJson(depth int, applyExtendListener bool) langext.H {
ginJson := gin.H{} ginJson := langext.H{}
if ee.UniqueID != "" { if ee.UniqueID != "" {
ginJson["id"] = ee.UniqueID ginJson["id"] = ee.UniqueID
@ -38,21 +39,51 @@ func (ee *ExErr) toJson(depth int) gin.H {
ginJson["wrappedErrType"] = ee.WrappedErrType ginJson["wrappedErrType"] = ee.WrappedErrType
} }
if ee.OriginalError != nil { if ee.OriginalError != nil {
ginJson["original"] = ee.OriginalError.toJson(depth + 1) ginJson["original"] = ee.OriginalError.toJson(depth+1, applyExtendListener)
} }
if applyExtendListener {
pkgconfig.ExtendGinDataOutput(ee, depth, ginJson) pkgconfig.ExtendGinDataOutput(ee, depth, ginJson)
}
return 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) { func (ee *ExErr) Output(g *gin.Context) {
warnOnPkgConfigNotInitialized()
var statuscode = http.StatusInternalServerError var statuscode = http.StatusInternalServerError
var baseCat = ee.RecursiveCategory() var baseCat = ee.RecursiveCategory()
var baseType = ee.RecursiveType() var baseType = ee.RecursiveType()
var baseStatuscode = ee.RecursiveStatuscode() var baseStatuscode = ee.RecursiveStatuscode()
var baseMessage = ee.RecursiveMessage()
if baseCat == CatUser { if baseCat == CatUser {
statuscode = http.StatusBadRequest statuscode = http.StatusBadRequest
@ -66,20 +97,7 @@ func (ee *ExErr) Output(g *gin.Context) {
statuscode = *baseType.DefaultStatusCode statuscode = *baseType.DefaultStatusCode
} }
warnOnPkgConfigNotInitialized() ginOutput := ee.ToAPIJson(true, pkgconfig.ExtendedGinOutput)
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)
g.Render(statuscode, json.GoJsonRender{Data: ginOutput, NilSafeSlices: true, NilSafeMaps: true}) g.Render(statuscode, json.GoJsonRender{Data: ginOutput, NilSafeSlices: true, NilSafeMaps: true})
} }

View File

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