Skip to content

Commit

Permalink
fixed error subject missing in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yusing committed Sep 30, 2024
1 parent e951194 commit f5a36f9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 1 addition & 2 deletions internal/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ func (ne NestedError) Subjectf(format string, args ...any) NestedError {
if strings.Contains(format, "%w") {
panic("Subjectf format should not contain %w")
}
ne.subject = fmt.Sprintf(format, args...)
return ne
return ne.Subject(fmt.Sprintf(format, args...))
}

func (ne NestedError) JSONObject() jsonNestedError {
Expand Down
8 changes: 7 additions & 1 deletion internal/error/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package error

import (
stderrors "errors"
"reflect"
)

var (
Expand All @@ -13,6 +14,7 @@ var (
ErrMissing = stderrors.New("missing")
ErrDuplicated = stderrors.New("duplicated")
ErrOutOfRange = stderrors.New("out of range")
ErrTypeError = stderrors.New("type error")
)

const fmtSubjectWhat = "%w %v: %q"
Expand Down Expand Up @@ -57,6 +59,10 @@ func Duplicated(subject, what any) NestedError {
return errorf("%w %v: %v", ErrDuplicated, subject, what)
}

func OutOfRange(subject string, value any) NestedError {
func OutOfRange(subject any, value any) NestedError {
return errorf("%v %w: %v", subject, ErrOutOfRange, value)
}

func TypeError(subject any, from, to reflect.Value) NestedError {
return errorf("%v %w: %T -> %T", subject, ErrTypeError, from.Interface(), to.Interface())
}

0 comments on commit f5a36f9

Please sign in to comment.