v0.0.370 improve sq errors
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m36s

This commit is contained in:
Mike Schwörer 2024-01-13 14:10:25 +01:00
parent aae8a706e9
commit be24f7a190
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
4 changed files with 18 additions and 8 deletions

View File

@ -68,6 +68,7 @@ func init() {
} }
type Builder struct { type Builder struct {
wrappedErr error
errorData *ExErr errorData *ExErr
containsGinData bool containsGinData bool
noLog bool noLog bool
@ -89,9 +90,9 @@ func Wrap(err error, msg string) *Builder {
if !pkgconfig.RecursiveErrors { if !pkgconfig.RecursiveErrors {
v := FromError(err) v := FromError(err)
v.Message = msg v.Message = msg
return &Builder{errorData: v} return &Builder{wrappedErr: err, errorData: v}
} }
return &Builder{errorData: wrapExErr(FromError(err), msg, CatWrap, 1)} return &Builder{wrappedErr: err, errorData: wrapExErr(FromError(err), msg, CatWrap, 1)}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -414,6 +415,10 @@ func extractHeader(header map[string][]string) []string {
func (b *Builder) Build() error { func (b *Builder) Build() error {
warnOnPkgConfigNotInitialized() warnOnPkgConfigNotInitialized()
if pkgconfig.DisableErrorWrapping && b.wrappedErr != nil {
return b.wrappedErr
}
if pkgconfig.ZeroLogErrTraces && !b.noLog && (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) { if pkgconfig.ZeroLogErrTraces && !b.noLog && (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) {
b.errorData.ShortLog(stackSkipLogger.Error()) b.errorData.ShortLog(stackSkipLogger.Error())
} else if pkgconfig.ZeroLogAllTraces && !b.noLog { } else if pkgconfig.ZeroLogAllTraces && !b.noLog {

View File

@ -13,6 +13,7 @@ type ErrorPackageConfig struct {
IncludeMetaInGinOutput bool // Log meta fields ( from e.g. `.Str(key, val).Build()` ) to gin in err.Output() IncludeMetaInGinOutput bool // Log meta fields ( from e.g. `.Str(key, val).Build()` ) to gin in err.Output()
ExtendGinOutput func(err *ExErr, json map[string]any) // (Optionally) extend the gin output with more fields ExtendGinOutput func(err *ExErr, json map[string]any) // (Optionally) extend the gin output with more fields
ExtendGinDataOutput func(err *ExErr, depth int, json map[string]any) // (Optionally) extend the gin `__data` output with more fields ExtendGinDataOutput func(err *ExErr, depth int, json map[string]any) // (Optionally) extend the gin `__data` output with more fields
DisableErrorWrapping bool // Disables the exerr.Wrap()...Build() function - will always return the original error
} }
type ErrorPackageConfigInit struct { type ErrorPackageConfigInit struct {
@ -23,6 +24,7 @@ type ErrorPackageConfigInit struct {
IncludeMetaInGinOutput *bool IncludeMetaInGinOutput *bool
ExtendGinOutput func(err *ExErr, json map[string]any) ExtendGinOutput func(err *ExErr, json map[string]any)
ExtendGinDataOutput func(err *ExErr, depth int, json map[string]any) ExtendGinDataOutput func(err *ExErr, depth int, json map[string]any)
DisableErrorWrapping *bool
} }
var initialized = false var initialized = false
@ -35,6 +37,7 @@ var pkgconfig = ErrorPackageConfig{
IncludeMetaInGinOutput: true, IncludeMetaInGinOutput: true,
ExtendGinOutput: func(err *ExErr, json map[string]any) {}, ExtendGinOutput: func(err *ExErr, json map[string]any) {},
ExtendGinDataOutput: func(err *ExErr, depth int, json map[string]any) {}, ExtendGinDataOutput: func(err *ExErr, depth int, json map[string]any) {},
DisableErrorWrapping: false,
} }
// Init initializes the exerr packages // Init initializes the exerr packages
@ -63,6 +66,7 @@ func Init(cfg ErrorPackageConfigInit) {
IncludeMetaInGinOutput: langext.Coalesce(cfg.IncludeMetaInGinOutput, pkgconfig.IncludeMetaInGinOutput), IncludeMetaInGinOutput: langext.Coalesce(cfg.IncludeMetaInGinOutput, pkgconfig.IncludeMetaInGinOutput),
ExtendGinOutput: ego, ExtendGinOutput: ego,
ExtendGinDataOutput: egdo, ExtendGinDataOutput: egdo,
DisableErrorWrapping: langext.Coalesce(cfg.DisableErrorWrapping, pkgconfig.DisableErrorWrapping),
} }
initialized = true initialized = true

View File

@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.369" const GoextVersion = "0.0.370"
const GoextVersionTimestamp = "2024-01-13T02:01:30+0100" const GoextVersionTimestamp = "2024-01-13T14:10:25+0100"

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"sync" "sync"
) )
@ -56,7 +57,7 @@ func (db *database) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Resul
} }
if err != nil { if err != nil {
return nil, err return nil, exerr.Wrap(err, "Failed to [exec] sql statement").Str("original_sql", origsql).Str("sql", sqlstr).Any("sql_params", prep).Build()
} }
return res, nil return res, nil
} }
@ -77,7 +78,7 @@ func (db *database) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx.Ro
} }
if err != nil { if err != nil {
return nil, err return nil, exerr.Wrap(err, "Failed to [query] sql statement").Str("original_sql", origsql).Str("sql", sqlstr).Any("sql_params", prep).Build()
} }
return rows, nil return rows, nil
} }
@ -97,7 +98,7 @@ func (db *database) Ping(ctx context.Context) error {
} }
if err != nil { if err != nil {
return err return exerr.Wrap(err, "Failed to [ping] sql database").Build()
} }
return nil return nil
} }
@ -117,7 +118,7 @@ func (db *database) BeginTransaction(ctx context.Context, iso sql.IsolationLevel
xtx, err := db.db.BeginTxx(ctx, &sql.TxOptions{Isolation: iso}) xtx, err := db.db.BeginTxx(ctx, &sql.TxOptions{Isolation: iso})
if err != nil { if err != nil {
return nil, err return nil, exerr.Wrap(err, "Failed to start sql transaction").Build()
} }
for _, v := range db.lstr { for _, v := range db.lstr {