goext/exerr/gin.go

126 lines
3.3 KiB
Go
Raw 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-27 14:37:11 +02:00
"gogs.mikescher.com/BlackForestBytes/goext/langext"
2023-07-24 10:42:39 +02:00
"net/http"
"time"
)
2023-08-09 10:37:59 +02:00
func (ee *ExErr) toJson(depth int, applyExtendListener bool, outputMeta bool) langext.H {
2023-07-27 14:37:11 +02:00
ginJson := langext.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 {
2024-04-29 16:03:58 +02:00
ginJson["category"] = ee.Category.Category
2023-07-24 10:42:39 +02:00
}
if ee.Type != TypeWrap {
2024-04-29 16:03:58 +02:00
ginJson["type"] = ee.Type.Key
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 {
2024-04-29 16:03:58 +02:00
ginJson["severity"] = ee.Severity.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-08-09 10:37:59 +02:00
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
2023-07-24 10:42:39 +02:00
}
2023-07-27 14:37:11 +02:00
if applyExtendListener {
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-12-28 01:36:21 +01:00
func (ee *ExErr) ToDefaultAPIJson() (string, error) {
gjr := json.GoJsonRender{Data: ee.ToAPIJson(true, pkgconfig.ExtendedGinOutput, pkgconfig.IncludeMetaInGinOutput), NilSafeSlices: true, NilSafeMaps: true}
r, err := gjr.RenderString()
if err != nil {
return "", err
}
return r, nil
}
2023-07-27 14:37:11 +02:00
// 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`
2023-08-09 10:37:59 +02:00
// - [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 {
2023-07-27 14:37:11 +02:00
apiOutput := langext.H{
"errorid": ee.UniqueID,
"message": ee.RecursiveMessage(),
"errorcode": ee.RecursiveType().Key,
"category": ee.RecursiveCategory().Category,
}
if includeWrappedErrors {
2023-08-09 10:37:59 +02:00
apiOutput["__data"] = ee.toJson(0, applyExtendListener, includeMetaFields)
2023-07-27 14:37:11 +02:00
}
if applyExtendListener {
pkgconfig.ExtendGinOutput(ee, apiOutput)
}
return apiOutput
}
2023-07-24 11:11:15 +02:00
func (ee *ExErr) Output(g *gin.Context) {
2023-07-27 14:37:11 +02:00
warnOnPkgConfigNotInitialized()
2023-07-24 10:42:39 +02:00
var statuscode = http.StatusInternalServerError
var baseCat = ee.RecursiveCategory()
var baseType = ee.RecursiveType()
var baseStatuscode = ee.RecursiveStatuscode()
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
}
2023-08-09 10:37:59 +02:00
ginOutput := ee.ToAPIJson(true, pkgconfig.ExtendedGinOutput, pkgconfig.IncludeMetaInGinOutput)
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
}