Skip to content

Commit

Permalink
Refactor error values map to use generic
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mizutani committed Jan 13, 2024
1 parent 1a802df commit 4907101
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ type Error struct {
id string
st *stack
cause error
values map[string]any
values map[any]any
}

func newError() *Error {
return &Error{
st: callers(),
values: make(map[string]any),
values: make(map[any]any),
id: uuid.New().String(),
}
}
Expand All @@ -78,7 +78,7 @@ func (x *Error) Printable() *printable {
ID: x.id,
StackTrace: x.Stacks(),
Cause: x.cause,
Values: make(map[string]any),
Values: make(map[any]any),
}
for k, v := range x.values {
e.Values[k] = v
Expand All @@ -87,11 +87,11 @@ func (x *Error) Printable() *printable {
}

type printable struct {
Message string `json:"message"`
ID string `json:"id"`
StackTrace []*Stack `json:"stacktrace"`
Cause error `json:"cause"`
Values map[string]any `json:"values"`
Message string `json:"message"`
ID string `json:"id"`
StackTrace []*Stack `json:"stacktrace"`
Cause error `json:"cause"`
Values map[any]any `json:"values"`
}

// Error returns error message for error interface
Expand All @@ -103,7 +103,7 @@ func (x *Error) Error() string {
return s
}

s = fmt.Sprintf("%s: %v", s, cause.Error())
s = fmt.Sprintf("%v: %v", s, cause.Error())

return s
}
Expand Down Expand Up @@ -165,8 +165,8 @@ func (x *Error) Wrap(cause error) *Error {
}

// Values returns map of key and value that is set by With. All wrapped goerr.Error key and values will be merged. Key and values of wrapped error is overwritten by upper goerr.Error.
func (x *Error) Values() map[string]any {
var values map[string]any
func (x *Error) Values() map[any]any {
var values map[any]any

if cause := x.Unwrap(); cause != nil {
if err, ok := cause.(*Error); ok {
Expand All @@ -175,7 +175,7 @@ func (x *Error) Values() map[string]any {
}

if values == nil {
values = make(map[string]any)
values = make(map[any]any)
}

for key, value := range x.values {
Expand All @@ -191,7 +191,7 @@ func (x *Error) LogValue() slog.Value {
}
var values []any
for k, v := range x.values {
values = append(values, slog.Any(k, v))
values = append(values, slog.Any(fmt.Sprintf("%v", k), v))
}
attrs = append(attrs, slog.Group("values", values...))

Expand Down

0 comments on commit 4907101

Please sign in to comment.