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..d1c7705 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{}) { +// Returns a function that should be deferred to catch panics +// Example usage: defer CatchPanicCallback("MyFunction", func(err any) { /* cleanup */ })() +func CatchPanicCallback(location string, cb func(err any), args ...interface{}) func() { ctx, _ := extractContext(args...) - if err := recover(); err != nil { - callstack := debug.Stack() + return func() { + 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")