v0.0.189
This commit is contained in:
@@ -72,7 +72,7 @@ type Builder struct {
|
||||
}
|
||||
|
||||
func Get(err error) *Builder {
|
||||
return &Builder{errorData: fromError(err)}
|
||||
return &Builder{errorData: FromError(err)}
|
||||
}
|
||||
|
||||
func New(t ErrorType, msg string) *Builder {
|
||||
@@ -80,7 +80,12 @@ func New(t ErrorType, msg string) *Builder {
|
||||
}
|
||||
|
||||
func Wrap(err error, msg string) *Builder {
|
||||
return &Builder{errorData: wrapExErr(fromError(err), msg, CatWrap, 1)}
|
||||
if !pkgconfig.RecursiveErrors {
|
||||
v := FromError(err)
|
||||
v.Message = msg
|
||||
return &Builder{errorData: v}
|
||||
}
|
||||
return &Builder{errorData: wrapExErr(FromError(err), msg, CatWrap, 1)}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -372,7 +377,7 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) {
|
||||
b.GinReq(ctx, g, g.Request)
|
||||
}
|
||||
|
||||
b.errorData.Output(ctx, g)
|
||||
b.errorData.Output(g)
|
||||
|
||||
if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal {
|
||||
b.errorData.Log(stackSkipLogger.Error())
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
var reflectTypeStr = reflect.TypeOf("")
|
||||
|
||||
func fromError(err error) *ExErr {
|
||||
func FromError(err error) *ExErr {
|
||||
if verr, ok := err.(*ExErr); ok {
|
||||
// A simple ExErr
|
||||
return verr
|
||||
|
||||
@@ -2,7 +2,6 @@ package exerr
|
||||
|
||||
import (
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ErrorCategory struct{ Category string }
|
||||
@@ -35,9 +34,20 @@ type ErrorType struct {
|
||||
}
|
||||
|
||||
var (
|
||||
TypeInternal = ErrorType{"Internal", langext.Ptr(http.StatusInternalServerError)}
|
||||
TypePanic = ErrorType{"Panic", langext.Ptr(http.StatusInternalServerError)}
|
||||
TypeWrap = ErrorType{"Wrap", nil}
|
||||
TypeInternal = ErrorType{"INTERNAL_ERROR", langext.Ptr(500)}
|
||||
TypePanic = ErrorType{"PANIC", langext.Ptr(500)}
|
||||
TypeNotImplemented = ErrorType{"NOT_IMPLEMENTED", langext.Ptr(500)}
|
||||
|
||||
TypeWrap = ErrorType{"Wrap", nil}
|
||||
|
||||
TypeBindFailURI = ErrorType{"BINDFAIL_URI", langext.Ptr(400)}
|
||||
TypeBindFailQuery = ErrorType{"BINDFAIL_QUERY", langext.Ptr(400)}
|
||||
TypeBindFailJSON = ErrorType{"BINDFAIL_JSON", langext.Ptr(400)}
|
||||
TypeBindFailFormData = ErrorType{"BINDFAIL_FORMDATA", langext.Ptr(400)}
|
||||
|
||||
TypeUnauthorized = ErrorType{"UNAUTHORIZED", langext.Ptr(401)}
|
||||
TypeAuthFailed = ErrorType{"AUTH_FAILED", langext.Ptr(401)}
|
||||
|
||||
// other values come from pkgconfig
|
||||
)
|
||||
|
||||
|
||||
@@ -6,29 +6,32 @@ import (
|
||||
)
|
||||
|
||||
type ErrorPackageConfig struct {
|
||||
ZeroLogErrTraces bool // autom print zerolog logs on .Build() (for SevErr and SevFatal)
|
||||
ZeroLogAllTraces bool // autom print zerolog logs on .Build() (for all Severities)
|
||||
RecursiveErrors bool // errors contains their Origin-Error
|
||||
ExtendedGinOutput bool // Log extended data (trace, meta, ...) to gin in err.Output()
|
||||
Types []ErrorType // all available error-types
|
||||
ZeroLogErrTraces bool // autom print zerolog logs on .Build() (for SevErr and SevFatal)
|
||||
ZeroLogAllTraces bool // autom print zerolog logs on .Build() (for all Severities)
|
||||
RecursiveErrors bool // errors contains their Origin-Error
|
||||
ExtendedGinOutput bool // Log extended data (trace, meta, ...) to gin in err.Output()
|
||||
ExtendGinOutput func(json map[string]any) // (Optionally) extend the gin output with more fields
|
||||
ExtendGinDataOutput func(json map[string]any) // (Optionally) extend the gin `__data` output with more fields
|
||||
}
|
||||
|
||||
type ErrorPackageConfigInit struct {
|
||||
ZeroLogErrTraces bool
|
||||
ZeroLogAllTraces bool
|
||||
RecursiveErrors bool
|
||||
ExtendedGinOutput bool
|
||||
InitTypes func(_ func(key string, defaultStatusCode *int) ErrorType)
|
||||
ZeroLogErrTraces bool
|
||||
ZeroLogAllTraces bool
|
||||
RecursiveErrors bool
|
||||
ExtendedGinOutput bool
|
||||
ExtendGinOutput *func(json map[string]any)
|
||||
ExtendGinDataOutput *func(json map[string]any)
|
||||
}
|
||||
|
||||
var initialized = false
|
||||
|
||||
var pkgconfig = ErrorPackageConfig{
|
||||
ZeroLogErrTraces: true,
|
||||
ZeroLogAllTraces: false,
|
||||
RecursiveErrors: true,
|
||||
ExtendedGinOutput: false,
|
||||
Types: []ErrorType{TypeInternal, TypePanic, TypeWrap},
|
||||
ZeroLogErrTraces: true,
|
||||
ZeroLogAllTraces: false,
|
||||
RecursiveErrors: true,
|
||||
ExtendedGinOutput: false,
|
||||
ExtendGinOutput: func(json map[string]any) {},
|
||||
ExtendGinDataOutput: func(json map[string]any) {},
|
||||
}
|
||||
|
||||
// Init initializes the exerr packages
|
||||
@@ -39,24 +42,13 @@ func Init(cfg ErrorPackageConfigInit) {
|
||||
panic("Cannot re-init error package")
|
||||
}
|
||||
|
||||
types := pkgconfig.Types
|
||||
|
||||
fnAddType := func(key string, defaultStatusCode *int) ErrorType {
|
||||
et := ErrorType{key, defaultStatusCode}
|
||||
types = append(types, et)
|
||||
return et
|
||||
}
|
||||
|
||||
if cfg.InitTypes != nil {
|
||||
cfg.InitTypes(fnAddType)
|
||||
}
|
||||
|
||||
pkgconfig = ErrorPackageConfig{
|
||||
ZeroLogErrTraces: cfg.ZeroLogErrTraces,
|
||||
ZeroLogAllTraces: cfg.ZeroLogAllTraces,
|
||||
RecursiveErrors: cfg.RecursiveErrors,
|
||||
ExtendedGinOutput: cfg.ExtendedGinOutput,
|
||||
Types: types,
|
||||
ZeroLogErrTraces: cfg.ZeroLogErrTraces,
|
||||
ZeroLogAllTraces: cfg.ZeroLogAllTraces,
|
||||
RecursiveErrors: cfg.RecursiveErrors,
|
||||
ExtendedGinOutput: cfg.ExtendedGinOutput,
|
||||
ExtendGinOutput: langext.Coalesce(cfg.ExtendGinOutput, func(json map[string]any) {}),
|
||||
ExtendGinDataOutput: langext.Coalesce(cfg.ExtendGinDataOutput, func(json map[string]any) {}),
|
||||
}
|
||||
|
||||
initialized = true
|
||||
|
||||
37
exerr/gin.go
37
exerr/gin.go
@@ -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})
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ func IsType(err error, errType ErrorType) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
bmerr := fromError(err)
|
||||
bmerr := FromError(err)
|
||||
for bmerr != nil {
|
||||
if bmerr.Type == errType {
|
||||
return true
|
||||
@@ -28,7 +28,7 @@ func IsFrom(e error, original error) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
bmerr := fromError(e)
|
||||
bmerr := FromError(e)
|
||||
for bmerr == nil {
|
||||
return false
|
||||
}
|
||||
@@ -48,7 +48,7 @@ func HasSourceMessage(e error, msg string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
bmerr := fromError(e)
|
||||
bmerr := FromError(e)
|
||||
for bmerr == nil {
|
||||
return false
|
||||
}
|
||||
@@ -71,7 +71,7 @@ func MessageMatch(e error, matcher func(string) bool) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
bmerr := fromError(e)
|
||||
bmerr := FromError(e)
|
||||
for bmerr == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user