exerr [WIP]

(cherry picked from commit c0443af63b)
This commit is contained in:
2023-02-15 16:04:19 +01:00
parent 1ea6695f82
commit 56684b2c0b
10 changed files with 1430 additions and 366 deletions

50
exerr/errinit.go Normal file
View 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
}