2023-02-15 16:04:19 +01:00
|
|
|
package exerr
|
|
|
|
|
2023-07-24 10:42:39 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
)
|
|
|
|
|
2023-02-15 16:04:19 +01:00
|
|
|
type ErrorPackageConfig struct {
|
2023-07-24 11:11:15 +02:00
|
|
|
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()
|
|
|
|
ExtendGinOutput func(json map[string]any) // (Optionally) extend the gin output with more fields
|
|
|
|
ExtendGinDataOutput func(json map[string]any) // (Optionally) extend the gin `__data` output with more fields
|
2023-02-15 16:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorPackageConfigInit struct {
|
2023-07-24 11:11:15 +02:00
|
|
|
ZeroLogErrTraces bool
|
|
|
|
ZeroLogAllTraces bool
|
|
|
|
RecursiveErrors bool
|
|
|
|
ExtendedGinOutput bool
|
|
|
|
ExtendGinOutput *func(json map[string]any)
|
|
|
|
ExtendGinDataOutput *func(json map[string]any)
|
2023-02-15 16:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var initialized = false
|
|
|
|
|
|
|
|
var pkgconfig = ErrorPackageConfig{
|
2023-07-24 11:11:15 +02:00
|
|
|
ZeroLogErrTraces: true,
|
|
|
|
ZeroLogAllTraces: false,
|
|
|
|
RecursiveErrors: true,
|
|
|
|
ExtendedGinOutput: false,
|
|
|
|
ExtendGinOutput: func(json map[string]any) {},
|
|
|
|
ExtendGinDataOutput: func(json map[string]any) {},
|
2023-02-15 16:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
pkgconfig = ErrorPackageConfig{
|
2023-07-24 11:11:15 +02:00
|
|
|
ZeroLogErrTraces: cfg.ZeroLogErrTraces,
|
|
|
|
ZeroLogAllTraces: cfg.ZeroLogAllTraces,
|
|
|
|
RecursiveErrors: cfg.RecursiveErrors,
|
|
|
|
ExtendedGinOutput: cfg.ExtendedGinOutput,
|
|
|
|
ExtendGinOutput: langext.Coalesce(cfg.ExtendGinOutput, func(json map[string]any) {}),
|
|
|
|
ExtendGinDataOutput: langext.Coalesce(cfg.ExtendGinDataOutput, func(json map[string]any) {}),
|
2023-02-15 16:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
initialized = true
|
|
|
|
}
|
2023-07-24 10:42:39 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|