watch-tool/helpers/utils.go

47 lines
869 B
Go

package helpers
import (
"context"
"fmt"
"log/slog"
"runtime/debug"
)
type AppError struct {
Op string
Err error
Context string
}
func (e *AppError) Error() string {
if e.Context != "" {
return fmt.Sprintf("%s: %v (%s)", e.Op, e.Err, e.Context)
}
return fmt.Sprintf("%s: %v", e.Op, e.Err)
}
func (e *AppError) Unwrap() error {
return e.Err
}
func NewAppError(op string, err error, ctx string) error {
return &AppError{Op: op, Err: err, Context: ctx}
}
func SafeGo(ctx context.Context, name string, fn func()) {
go func() {
defer func() {
if r := recover(); r != nil {
stack := string(debug.Stack())
slog.Error("CRITICAL: Panic recovered in goroutine",
"goroutine", name,
"panic", r,
"stack", stack,
)
// Optional: Hier könnte man Metriken inkrementieren (siehe Observability)
}
}()
fn()
}()
}