goext/exerr/gin.go

82 lines
1.7 KiB
Go

package exerr
import (
"context"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func (ee *ExErr) toJson() gin.H {
json := gin.H{}
if ee.UniqueID != "" {
json["id"] = ee.UniqueID
}
if ee.Category != CatWrap {
json["category"] = ee.Category
}
if ee.Type != TypeWrap {
json["type"] = ee.Type
}
if ee.StatusCode != nil {
json["statuscode"] = ee.StatusCode
}
if ee.Message != "" {
json["message"] = ee.Message
}
if ee.Caller != "" {
json["caller"] = ee.Caller
}
if ee.Severity != SevErr {
json["severity"] = ee.Severity
}
if ee.Timestamp != (time.Time{}) {
json["time"] = ee.Timestamp.Format(time.RFC3339)
}
if ee.OriginalError != nil {
json["original"] = ee.OriginalError.toJson()
}
return json
}
func (ee *ExErr) Output(ctx context.Context, g *gin.Context) {
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
}
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(),
})
}
}