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

@@ -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