goext/exerr/errinit.go
Mike Schwörer 56684b2c0b
exerr [WIP]
(cherry picked from commit c0443af63b)
2023-07-19 19:24:43 +02:00

51 lines
1.1 KiB
Go

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
}