From be24f7a190b5159c2d3edd81d3beac43fda5078d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 13 Jan 2024 14:10:25 +0100 Subject: [PATCH] v0.0.370 improve sq errors --- exerr/builder.go | 9 +++++++-- exerr/errinit.go | 4 ++++ goextVersion.go | 4 ++-- sq/database.go | 9 +++++---- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/exerr/builder.go b/exerr/builder.go index 3dcc651..7af499f 100644 --- a/exerr/builder.go +++ b/exerr/builder.go @@ -68,6 +68,7 @@ func init() { } type Builder struct { + wrappedErr error errorData *ExErr containsGinData bool noLog bool @@ -89,9 +90,9 @@ func Wrap(err error, msg string) *Builder { if !pkgconfig.RecursiveErrors { v := FromError(err) 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 { warnOnPkgConfigNotInitialized() + if pkgconfig.DisableErrorWrapping && b.wrappedErr != nil { + return b.wrappedErr + } + if pkgconfig.ZeroLogErrTraces && !b.noLog && (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) { b.errorData.ShortLog(stackSkipLogger.Error()) } else if pkgconfig.ZeroLogAllTraces && !b.noLog { diff --git a/exerr/errinit.go b/exerr/errinit.go index 076f465..b35f321 100644 --- a/exerr/errinit.go +++ b/exerr/errinit.go @@ -13,6 +13,7 @@ type ErrorPackageConfig struct { 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 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 { @@ -23,6 +24,7 @@ type ErrorPackageConfigInit struct { IncludeMetaInGinOutput *bool ExtendGinOutput func(err *ExErr, json map[string]any) ExtendGinDataOutput func(err *ExErr, depth int, json map[string]any) + DisableErrorWrapping *bool } var initialized = false @@ -35,6 +37,7 @@ var pkgconfig = ErrorPackageConfig{ IncludeMetaInGinOutput: true, ExtendGinOutput: func(err *ExErr, json map[string]any) {}, ExtendGinDataOutput: func(err *ExErr, depth int, json map[string]any) {}, + DisableErrorWrapping: false, } // Init initializes the exerr packages @@ -63,6 +66,7 @@ func Init(cfg ErrorPackageConfigInit) { IncludeMetaInGinOutput: langext.Coalesce(cfg.IncludeMetaInGinOutput, pkgconfig.IncludeMetaInGinOutput), ExtendGinOutput: ego, ExtendGinDataOutput: egdo, + DisableErrorWrapping: langext.Coalesce(cfg.DisableErrorWrapping, pkgconfig.DisableErrorWrapping), } initialized = true diff --git a/goextVersion.go b/goextVersion.go index 5cfbdf5..d6eefbe 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ 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" diff --git a/sq/database.go b/sq/database.go index 7a3ac59..8477e93 100644 --- a/sq/database.go +++ b/sq/database.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "github.com/jmoiron/sqlx" + "gogs.mikescher.com/BlackForestBytes/goext/exerr" "gogs.mikescher.com/BlackForestBytes/goext/langext" "sync" ) @@ -56,7 +57,7 @@ func (db *database) Exec(ctx context.Context, sqlstr string, prep PP) (sql.Resul } 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 } @@ -77,7 +78,7 @@ func (db *database) Query(ctx context.Context, sqlstr string, prep PP) (*sqlx.Ro } 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 } @@ -97,7 +98,7 @@ func (db *database) Ping(ctx context.Context) error { } if err != nil { - return err + return exerr.Wrap(err, "Failed to [ping] sql database").Build() } 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}) if err != nil { - return nil, err + return nil, exerr.Wrap(err, "Failed to start sql transaction").Build() } for _, v := range db.lstr {