From 5e6032c91d7fc64390568fbc3072efd576d688be Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 30 Dec 2025 11:02:05 +0000 Subject: [PATCH 1/4] Initial plan From e10e2e1c27a7c338557294124c2944fc3d0b52cd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 30 Dec 2025 11:06:43 +0000 Subject: [PATCH 2/4] Fix recover() usage in CatchPanic functions by returning deferred function Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com> --- pkg/errortracking/README.md | 4 ++-- pkg/logger/logger.go | 48 +++++++++++++++++++++---------------- pkg/security/provider.go | 4 ++-- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/pkg/errortracking/README.md b/pkg/errortracking/README.md index a9950c2..89436ff 100644 --- a/pkg/errortracking/README.md +++ b/pkg/errortracking/README.md @@ -90,12 +90,12 @@ Panics are automatically captured when using the logger's panic handlers: ```go // Using CatchPanic -defer logger.CatchPanic("MyFunction") +defer logger.CatchPanic("MyFunction")() // Using CatchPanicCallback defer logger.CatchPanicCallback("MyFunction", func(err any) { // Custom cleanup -}) +})() // Using HandlePanic defer func() { diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 5e7996c..f0382d0 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -148,35 +148,41 @@ func Debug(template string, args ...interface{}) { } // CatchPanic - Handle panic -func CatchPanicCallback(location string, cb func(err any), args ...interface{}) { - ctx, _ := extractContext(args...) - if err := recover(); err != nil { - callstack := debug.Stack() +// Returns a function that should be deferred to catch panics +// Example usage: defer CatchPanicCallback("MyFunction", callback)() +func CatchPanicCallback(location string, cb func(err any), args ...interface{}) func() { + return func() { + ctx, _ := extractContext(args...) + if err := recover(); err != nil { + callstack := debug.Stack() - if Logger != nil { - Error("Panic in %s : %v", location, err, ctx) // Pass context implicitly - } else { - fmt.Printf("%s:PANIC->%+v", location, err) - debug.PrintStack() - } + if Logger != nil { + Error("Panic in %s : %v", location, err, ctx) // Pass context implicitly + } else { + fmt.Printf("%s:PANIC->%+v", location, err) + debug.PrintStack() + } - // Send to error tracker - if errorTracker != nil { - errorTracker.CapturePanic(ctx, err, callstack, map[string]interface{}{ - "location": location, - "process_id": os.Getpid(), - }) - } + // Send to error tracker + if errorTracker != nil { + errorTracker.CapturePanic(ctx, err, callstack, map[string]interface{}{ + "location": location, + "process_id": os.Getpid(), + }) + } - if cb != nil { - cb(err) + if cb != nil { + cb(err) + } } } } // CatchPanic - Handle panic -func CatchPanic(location string, args ...interface{}) { - CatchPanicCallback(location, nil, args...) +// Returns a function that should be deferred to catch panics +// Example usage: defer CatchPanic("MyFunction")() +func CatchPanic(location string, args ...interface{}) func() { + return CatchPanicCallback(location, nil, args...) } // HandlePanic logs a panic and returns it as an error diff --git a/pkg/security/provider.go b/pkg/security/provider.go index 2eb7b4a..7dcf609 100644 --- a/pkg/security/provider.go +++ b/pkg/security/provider.go @@ -296,7 +296,7 @@ func setColSecValue(fieldsrc reflect.Value, colsec ColumnSecurity, fieldTypeName } func (m *SecurityList) ApplyColumnSecurity(records reflect.Value, modelType reflect.Type, pUserID int, pSchema, pTablename string) (reflect.Value, error) { - defer logger.CatchPanic("ApplyColumnSecurity") + defer logger.CatchPanic("ApplyColumnSecurity")() if m.ColumnSecurity == nil { return records, fmt.Errorf("security not initialized") @@ -437,7 +437,7 @@ func (m *SecurityList) LoadRowSecurity(ctx context.Context, pUserID int, pSchema } func (m *SecurityList) GetRowSecurityTemplate(pUserID int, pSchema, pTablename string) (RowSecurity, error) { - defer logger.CatchPanic("GetRowSecurityTemplate") + defer logger.CatchPanic("GetRowSecurityTemplate")() if m.RowSecurity == nil { return RowSecurity{}, fmt.Errorf("security not initialized") From 4cb35a78b0f3171cb298920a51d4c15e1ee7f26a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 30 Dec 2025 11:07:46 +0000 Subject: [PATCH 3/4] Improve CatchPanicCallback: extract context early and clarify example Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com> --- pkg/logger/logger.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index f0382d0..d1c7705 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -149,10 +149,10 @@ func Debug(template string, args ...interface{}) { // CatchPanic - Handle panic // Returns a function that should be deferred to catch panics -// Example usage: defer CatchPanicCallback("MyFunction", callback)() +// Example usage: defer CatchPanicCallback("MyFunction", func(err any) { /* cleanup */ })() func CatchPanicCallback(location string, cb func(err any), args ...interface{}) func() { + ctx, _ := extractContext(args...) return func() { - ctx, _ := extractContext(args...) if err := recover(); err != nil { callstack := debug.Stack() From 92a83e9cc6669e162e19d47eb1f5c584d916c4ae Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 30 Dec 2025 11:09:06 +0000 Subject: [PATCH 4/4] Final update Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com> --- go.mod | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index d6baffe..47c9df6 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/google/uuid v1.6.0 github.com/gorilla/mux v1.8.1 github.com/jackc/pgx/v5 v5.6.0 + github.com/klauspost/compress v1.18.0 github.com/prometheus/client_golang v1.23.2 github.com/redis/go-redis/v9 v9.17.1 github.com/spf13/viper v1.21.0 @@ -29,6 +30,7 @@ require ( go.opentelemetry.io/otel/sdk v1.38.0 go.opentelemetry.io/otel/trace v1.38.0 go.uber.org/zap v1.27.0 + golang.org/x/crypto v0.43.0 golang.org/x/time v0.14.0 gorm.io/driver/postgres v1.6.0 gorm.io/gorm v1.25.12 @@ -68,7 +70,6 @@ require ( github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/klauspost/compress v1.18.0 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/magiconair/properties v1.8.10 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -117,7 +118,6 @@ require ( go.uber.org/multierr v1.10.0 // indirect go.yaml.in/yaml/v2 v2.4.2 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect - golang.org/x/crypto v0.43.0 // indirect golang.org/x/exp v0.0.0-20251113190631-e25ba8c21ef6 // indirect golang.org/x/net v0.45.0 // indirect golang.org/x/sync v0.18.0 // indirect