This commit is contained in:
2023-07-24 11:11:15 +02:00
parent 2e6ca48d22
commit 16c66ee28c
12 changed files with 100 additions and 163 deletions

View File

@@ -1,8 +1,8 @@
package exerr
import (
"context"
"github.com/gin-gonic/gin"
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"net/http"
"time"
)
@@ -34,14 +34,19 @@ func (ee *ExErr) toJson() gin.H {
if ee.Timestamp != (time.Time{}) {
json["time"] = ee.Timestamp.Format(time.RFC3339)
}
if ee.WrappedErrType != "" {
json["wrappedErrType"] = ee.WrappedErrType
}
if ee.OriginalError != nil {
json["original"] = ee.OriginalError.toJson()
}
pkgconfig.ExtendGinDataOutput(json)
return json
}
func (ee *ExErr) Output(ctx context.Context, g *gin.Context) {
func (ee *ExErr) Output(g *gin.Context) {
var statuscode = http.StatusInternalServerError
var baseCat = ee.RecursiveCategory()
@@ -62,20 +67,18 @@ func (ee *ExErr) Output(ctx context.Context, g *gin.Context) {
warnOnPkgConfigNotInitialized()
if pkgconfig.ExtendedGinOutput {
g.JSON(statuscode, gin.H{
"errorid": ee.UniqueID,
"error": ee.RecursiveMessage(),
"errorcategory": ee.RecursiveCategory(),
"errortype": ee.RecursiveType(),
"errodata": ee.toJson(),
})
} else {
g.JSON(statuscode, gin.H{
"errorid": ee.UniqueID,
"error": ee.RecursiveMessage(),
"errorcategory": ee.RecursiveCategory(),
"errortype": ee.RecursiveType(),
})
ginOutput := gin.H{
"errorid": ee.UniqueID,
"message": ee.RecursiveMessage(),
"errorcode": ee.RecursiveType(),
"category": ee.RecursiveCategory(),
}
if pkgconfig.ExtendedGinOutput {
ginOutput["__data"] = ee.toJson()
}
pkgconfig.ExtendGinOutput(ginOutput)
g.Render(statuscode, json.GoJsonRender{Data: ginOutput, NilSafeSlices: true, NilSafeMaps: true})
}