75 lines
2.0 KiB
Go
75 lines
2.0 KiB
Go
package exerr
|
|
|
|
import (
|
|
"fmt"
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
type ErrorPackageConfigInit struct {
|
|
ZeroLogErrTraces bool
|
|
ZeroLogAllTraces bool
|
|
RecursiveErrors bool
|
|
ExtendedGinOutput bool
|
|
InitTypes func(_ func(key string, defaultStatusCode *int) ErrorType)
|
|
}
|
|
|
|
var initialized = false
|
|
|
|
var pkgconfig = ErrorPackageConfig{
|
|
ZeroLogErrTraces: true,
|
|
ZeroLogAllTraces: false,
|
|
RecursiveErrors: true,
|
|
ExtendedGinOutput: false,
|
|
Types: []ErrorType{TypeInternal, TypePanic, TypeWrap},
|
|
}
|
|
|
|
// Init initializes the exerr packages
|
|
// Must be called at the program start, before (!) any errors
|
|
// Is not thread-safe
|
|
func Init(cfg ErrorPackageConfigInit) {
|
|
if initialized {
|
|
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,
|
|
}
|
|
|
|
initialized = true
|
|
}
|
|
|
|
func warnOnPkgConfigNotInitialized() {
|
|
if !initialized {
|
|
fmt.Printf("\n")
|
|
fmt.Printf("%s\n", langext.StrRepeat("=", 80))
|
|
fmt.Printf("%s\n", "[WARNING] exerr package used without initializiation")
|
|
fmt.Printf("%s\n", " call exerr.Init() in your main() function")
|
|
fmt.Printf("%s\n", langext.StrRepeat("=", 80))
|
|
fmt.Printf("\n")
|
|
}
|
|
}
|