Documentation
¶
Overview ¶
Example ¶
Example demonstrates the error handler usage with console output
package main
import (
"errors"
"fmt"
"github.com/777genius/agent-notifications/internal/errorhandler"
"github.com/777genius/agent-notifications/internal/logging"
)
func main() {
// Initialize logger (in real usage this would be done in main.go)
if _, err := logging.InitLogger("."); err != nil {
fmt.Printf("Failed to initialize logger: %v\n", err)
return
}
defer logging.Close()
// Initialize error handler with console output enabled
errorhandler.Init(true, false, true)
// Example 1: Handle a normal error
err := errors.New("connection timeout")
errorhandler.HandleError(err, "Failed to connect to webhook")
// Example 2: Log info message
errorhandler.Info("Notification sent successfully")
// Example 3: Log warning
errorhandler.Warn("Rate limit at 80%% capacity")
// Example 4: Protected function execution
errorhandler.WithRecovery(func() {
fmt.Println("Safe execution")
})
// Output will be in console with [claude-notifications] prefix:
// [claude-notifications] [timestamp] [ERROR] Failed to connect to webhook: connection timeout
// [claude-notifications] [timestamp] [INFO] Notification sent successfully
// [claude-notifications] [timestamp] [WARN] Rate limit at 80% capacity
}
Output:
Index ¶
- func Debug(format string, args ...interface{})
- func HandleCriticalError(err error, context string)
- func HandleError(err error, context string)
- func HandlePanic()
- func Info(format string, args ...interface{})
- func Reset()
- func SafeGo(fn func())
- func Warn(format string, args ...interface{})
- func WithRecovery(fn func())
- func WithRecoveryFunc(fn func() error) error
- type ErrorHandler
- func (h *ErrorHandler) Debug(format string, args ...interface{})
- func (h *ErrorHandler) HandleCriticalError(err error, context string)
- func (h *ErrorHandler) HandleError(err error, context string)
- func (h *ErrorHandler) HandlePanic()
- func (h *ErrorHandler) Info(format string, args ...interface{})
- func (h *ErrorHandler) Warn(format string, args ...interface{})
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
func Debug(format string, args ...interface{})
Debug logs a debug message using the default handler
func HandleCriticalError ¶
HandleCriticalError handles a critical error using the default handler
func HandleError ¶
HandleError handles a general error using the default handler
func Info ¶
func Info(format string, args ...interface{})
Info logs an info message using the default handler
func Reset ¶
func Reset()
Reset resets the error handler (for testing only) WARNING: This is not thread-safe and should only be called in tests when no other goroutines are using the error handler
func SafeGo ¶
func SafeGo(fn func())
SafeGo runs a goroutine with panic recovery
Example ¶
ExampleSafeGo demonstrates safe goroutine execution
package main
import (
"fmt"
"github.com/777genius/agent-notifications/internal/errorhandler"
)
func main() {
errorhandler.Init(true, false, true)
done := make(chan bool)
// This goroutine is protected from panics
errorhandler.SafeGo(func() {
defer func() { done <- true }()
fmt.Println("Running in safe goroutine")
})
<-done
}
Output:
func Warn ¶
func Warn(format string, args ...interface{})
Warn logs a warning using the default handler
func WithRecoveryFunc ¶
WithRecoveryFunc wraps a function that returns an error with panic recovery
Types ¶
type ErrorHandler ¶
type ErrorHandler struct {
// contains filtered or unexported fields
}
ErrorHandler provides global error handling and logging
func GetHandler ¶
func GetHandler() *ErrorHandler
GetHandler returns the default error handler (auto-initializes with defaults if needed)
func Init ¶
func Init(logToConsole, exitOnCritical, recoveryEnabled bool) *ErrorHandler
Init initializes the global error handler with custom settings If handler is already initialized, returns the existing handler
func (*ErrorHandler) Debug ¶
func (h *ErrorHandler) Debug(format string, args ...interface{})
Debug logs a debug message
func (*ErrorHandler) HandleCriticalError ¶
func (h *ErrorHandler) HandleCriticalError(err error, context string)
HandleCriticalError handles a critical error that may require program termination
func (*ErrorHandler) HandleError ¶
func (h *ErrorHandler) HandleError(err error, context string)
HandleError handles a general error
func (*ErrorHandler) HandlePanic ¶
func (h *ErrorHandler) HandlePanic()
HandlePanic recovers from a panic and logs it
func (*ErrorHandler) Info ¶
func (h *ErrorHandler) Info(format string, args ...interface{})
Info logs an informational message
func (*ErrorHandler) Warn ¶
func (h *ErrorHandler) Warn(format string, args ...interface{})
Warn logs a warning message