errs: log fields now not nested if error implements slog.LogValuer interface

This commit is contained in:
Tigor Hutasuhut 2024-04-30 21:45:09 +07:00
parent 1c1c0d03d9
commit c5c8058376

View file

@ -55,16 +55,14 @@ func (er *Err) LogValue() slog.Value {
if er.origin == nil { if er.origin == nil {
values = append(values, slog.Any("error", er.origin)) values = append(values, slog.Any("error", er.origin))
} else if lv, ok := er.origin.(slog.LogValuer); ok {
values = append(values, slog.Attr{Key: "error", Value: lv.LogValue()})
} else { } else {
errGroupValues := make([]slog.Attr, 0, 3) values = append(values, slog.Attr{Key: "error", Value: slog.GroupValue(
errGroupValues = append(errGroupValues, slog.String("type", reflect.TypeOf(er.origin).String())) slog.String("type", reflect.TypeOf(er.origin).String()),
if lv, ok := er.origin.(slog.LogValuer); ok { slog.String("message", er.origin.Error()),
errGroupValues = append(errGroupValues, slog.Attr{Key: "data", Value: lv.LogValue()}) slog.Any("data", er.origin),
} else { )})
errGroupValues = append(errGroupValues, slog.String("message", er.origin.Error()), slog.Any("data", er.origin))
}
values = append(values, slog.Attr{Key: "error", Value: slog.GroupValue(errGroupValues...)})
} }
return slog.GroupValue(values...) return slog.GroupValue(values...)