goext/exerr/gin.go

86 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-07-24 10:42:39 +02:00
package exerr
import (
"github.com/gin-gonic/gin"
2023-07-24 11:11:15 +02:00
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
2023-07-24 10:42:39 +02:00
"net/http"
"time"
)
2023-07-24 11:38:57 +02:00
func (ee *ExErr) toJson(depth int) gin.H {
ginJson := gin.H{}
2023-07-24 10:42:39 +02:00
if ee.UniqueID != "" {
2023-07-24 11:38:57 +02:00
ginJson["id"] = ee.UniqueID
2023-07-24 10:42:39 +02:00
}
if ee.Category != CatWrap {
2023-07-24 11:38:57 +02:00
ginJson["category"] = ee.Category
2023-07-24 10:42:39 +02:00
}
if ee.Type != TypeWrap {
2023-07-24 11:38:57 +02:00
ginJson["type"] = ee.Type
2023-07-24 10:42:39 +02:00
}
if ee.StatusCode != nil {
2023-07-24 11:38:57 +02:00
ginJson["statuscode"] = ee.StatusCode
2023-07-24 10:42:39 +02:00
}
if ee.Message != "" {
2023-07-24 11:38:57 +02:00
ginJson["message"] = ee.Message
2023-07-24 10:42:39 +02:00
}
if ee.Caller != "" {
2023-07-24 11:38:57 +02:00
ginJson["caller"] = ee.Caller
2023-07-24 10:42:39 +02:00
}
if ee.Severity != SevErr {
2023-07-24 11:38:57 +02:00
ginJson["severity"] = ee.Severity
2023-07-24 10:42:39 +02:00
}
if ee.Timestamp != (time.Time{}) {
2023-07-24 11:38:57 +02:00
ginJson["time"] = ee.Timestamp.Format(time.RFC3339)
2023-07-24 10:42:39 +02:00
}
2023-07-24 11:11:15 +02:00
if ee.WrappedErrType != "" {
2023-07-24 11:38:57 +02:00
ginJson["wrappedErrType"] = ee.WrappedErrType
2023-07-24 11:11:15 +02:00
}
2023-07-24 10:42:39 +02:00
if ee.OriginalError != nil {
2023-07-24 11:38:57 +02:00
ginJson["original"] = ee.OriginalError.toJson(depth + 1)
2023-07-24 10:42:39 +02:00
}
2023-07-24 11:38:57 +02:00
pkgconfig.ExtendGinDataOutput(ee, depth, ginJson)
2023-07-24 11:11:15 +02:00
2023-07-24 11:38:57 +02:00
return ginJson
2023-07-24 10:42:39 +02:00
}
2023-07-24 11:11:15 +02:00
func (ee *ExErr) Output(g *gin.Context) {
2023-07-24 10:42:39 +02:00
var statuscode = http.StatusInternalServerError
var baseCat = ee.RecursiveCategory()
var baseType = ee.RecursiveType()
var baseStatuscode = ee.RecursiveStatuscode()
2023-07-24 12:27:06 +02:00
var baseMessage = ee.RecursiveMessage()
2023-07-24 10:42:39 +02:00
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
}
warnOnPkgConfigNotInitialized()
2023-07-24 11:11:15 +02:00
ginOutput := gin.H{
"errorid": ee.UniqueID,
2023-07-24 12:27:06 +02:00
"message": baseMessage,
"errorcode": baseType.Key,
"category": baseCat.Category,
2023-07-24 11:11:15 +02:00
}
2023-07-24 10:42:39 +02:00
if pkgconfig.ExtendedGinOutput {
2023-07-24 11:38:57 +02:00
ginOutput["__data"] = ee.toJson(0)
2023-07-24 10:42:39 +02:00
}
2023-07-24 11:11:15 +02:00
2023-07-24 11:38:57 +02:00
pkgconfig.ExtendGinOutput(ee, ginOutput)
2023-07-24 11:11:15 +02:00
g.Render(statuscode, json.GoJsonRender{Data: ginOutput, NilSafeSlices: true, NilSafeMaps: true})
2023-07-24 10:42:39 +02:00
}