Skip to content

Commit

Permalink
feat: change print sequence (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
abyssparanoia authored Nov 20, 2024
1 parent 9995ad8 commit aea9378
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ func (x *Error) LogValue() slog.Value {
attrs := []slog.Attr{
slog.String("message", x.msg),
}
if x.category != "" {
attrs = append(attrs, slog.String("category", x.category))
}
if x.detail != "" {
attrs = append(attrs, slog.String("detail", x.detail))
}
var values []any
for k, v := range x.values {
values = append(values, slog.Any(k, v))
Expand All @@ -275,13 +281,6 @@ func (x *Error) LogValue() slog.Value {

attrs = append(attrs, slog.Any("stacktrace", stacktrace))

if x.category != "" {
attrs = append(attrs, slog.String("category", x.category))
}
if x.detail != "" {
attrs = append(attrs, slog.String("detail", x.detail))
}

if x.cause != nil {
var errAttr slog.Attr
if lv, ok := x.cause.(slog.LogValuer); ok {
Expand All @@ -301,6 +300,12 @@ func (x *Error) MarshalLogObject(enc zapcore.ObjectEncoder) error {
return nil
}
enc.AddString("message", x.msg)
if x.category != "" {
enc.AddString("category", x.category)
}
if x.detail != "" {
enc.AddString("detail", x.detail)
}
enc.AddArray("values", zapcore.ArrayMarshalerFunc(func(inner zapcore.ArrayEncoder) error {
for k, v := range x.values {
inner.AppendObject(zapcore.ObjectMarshalerFunc(func(enc zapcore.ObjectEncoder) error {
Expand All @@ -322,13 +327,6 @@ func (x *Error) MarshalLogObject(enc zapcore.ObjectEncoder) error {
return nil
}))

if x.category != "" {
enc.AddString("category", x.category)
}
if x.detail != "" {
enc.AddString("detail", x.detail)
}

if x.cause != nil {
got := false
if inCause := x.Unwrap(); inCause != nil {
Expand Down

0 comments on commit aea9378

Please sign in to comment.