Skip to content

Commit

Permalink
feat: add category and detail (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
abyssparanoia authored Nov 20, 2024
1 parent 5c57d92 commit 9995ad8
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ func (x values) clone() values {

// Error is error interface for deepalert to handle related variables
type Error struct {
msg string
id string
st *stack
cause error
values values
msg string
id string
st *stack
cause error
values values
category string
detail string
}

func newError() *Error {
Expand Down Expand Up @@ -102,6 +104,8 @@ func (x *Error) Printable() *printable {
StackTrace: x.Stacks(),
Cause: x.cause,
Values: make(map[string]any),
Category: x.category,
Detail: x.detail,
}
for k, v := range x.values {
e.Values[k] = v
Expand All @@ -115,6 +119,8 @@ type printable struct {
StackTrace []*Stack `json:"stacktrace"`
Cause error `json:"cause"`
Values map[string]any `json:"values"`
Category string `json:"category"`
Detail string `json:"detail"`
}

// Error returns error message for error interface
Expand Down Expand Up @@ -228,6 +234,24 @@ func (x *Error) Values() map[string]any {
return values
}

func (x *Error) Category() string {
return x.category
}

func (x *Error) WithCategory(category string) *Error {
x.category = category
return x
}

func (x *Error) Detail() string {
return x.detail
}

func (x *Error) WithDetail(detail string) *Error {
x.detail = detail
return x
}

func (x *Error) LogValue() slog.Value {
if x == nil {
return slog.AnyValue(nil)
Expand All @@ -251,6 +275,13 @@ 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 Down Expand Up @@ -291,6 +322,13 @@ 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 9995ad8

Please sign in to comment.