fixed error subject missing in some cases

This commit is contained in:
yusing 2024-10-01 05:14:56 +08:00
parent e951194bee
commit f5a36f94bb
2 changed files with 8 additions and 3 deletions

View file

@ -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 {

View file

@ -2,6 +2,7 @@ package error
import (
stderrors "errors"
"reflect"
)
var (
@ -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"
@ -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())
}