50
exerr/errinit.go
Normal file
50
exerr/errinit.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package exerr
|
||||
|
||||
type ErrorPackageConfig struct {
|
||||
ZeroLogTraces bool // autom print zerolog logs on CreateError
|
||||
RecursiveErrors bool // errors contains their Origin-Error
|
||||
Types []ErrorType // all available error-types
|
||||
}
|
||||
|
||||
type ErrorPackageConfigInit struct {
|
||||
LogTraces bool
|
||||
RecursiveErrors bool
|
||||
InitTypes func(_ func(_ string) ErrorType)
|
||||
}
|
||||
|
||||
var initialized = false
|
||||
|
||||
var pkgconfig = ErrorPackageConfig{
|
||||
ZeroLogTraces: true,
|
||||
RecursiveErrors: true,
|
||||
Types: []ErrorType{TypeInternal},
|
||||
}
|
||||
|
||||
// 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(v string) ErrorType {
|
||||
et := ErrorType{v}
|
||||
types = append(types, et)
|
||||
return et
|
||||
}
|
||||
|
||||
if cfg.InitTypes != nil {
|
||||
cfg.InitTypes(fnAddType)
|
||||
}
|
||||
|
||||
pkgconfig = ErrorPackageConfig{
|
||||
ZeroLogTraces: cfg.LogTraces,
|
||||
RecursiveErrors: cfg.RecursiveErrors,
|
||||
Types: types,
|
||||
}
|
||||
|
||||
initialized = true
|
||||
}
|
Reference in New Issue
Block a user