goext/exerr/errinit.go

51 lines
1.1 KiB
Go
Raw Normal View History

2023-02-08 18:45:31 +01:00
package exerr
type ErrorPackageConfig struct {
2023-02-15 16:04:19 +01:00
ZeroLogTraces bool // autom print zerolog logs on CreateError
RecursiveErrors bool // errors contains their Origin-Error
Types []ErrorType // all available error-types
}
type ErrorPackageConfigInit struct {
2023-02-08 18:45:31 +01:00
LogTraces bool
RecursiveErrors bool
2023-02-15 16:04:19 +01:00
InitTypes func(_ func(_ string) ErrorType)
2023-02-08 18:45:31 +01:00
}
2023-02-15 16:04:19 +01:00
var initialized = false
2023-02-08 18:45:31 +01:00
var pkgconfig = ErrorPackageConfig{
2023-02-15 16:04:19 +01:00
ZeroLogTraces: true,
RecursiveErrors: true,
Types: []ErrorType{TypeInternal},
2023-02-08 18:45:31 +01:00
}
// Init initializes the exerr packages
// Must be called at the program start, before (!) any errors
// Is not thread-safe
2023-02-15 16:04:19 +01:00
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
2023-02-08 18:45:31 +01:00
}