zen/core/zerr/valuer.go
2024-08-22 17:26:53 +07:00

32 lines
725 B
Go

package zerr
import (
"log/slog"
"reflect"
)
type errorValuer struct {
error
}
func (ev errorValuer) LogValue() slog.Value {
if ev.error == nil {
return slog.AnyValue(nil)
}
if lv, ok := ev.error.(slog.LogValuer); ok {
return lv.LogValue()
}
if unwrap, ok := ev.error.(interface{ Unwrap() []error }); ok {
return (&Err{errs: unwrap.Unwrap()}).LogValue()
}
attrs := make([]slog.Attr, 0, 3)
typ := reflect.TypeOf(ev.error).String()
attrs = append(attrs, slog.String("type", typ))
attrs = append(attrs, slog.String("error", ev.error.Error()))
if typ == "*errors.errorString" {
return slog.GroupValue(attrs...)
}
attrs = append(attrs, slog.Any("details", ev.error))
return slog.GroupValue(attrs...)
}