mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-05 14:24:02 +02:00
chore: better error formatting
This commit is contained in:
parent
658332005d
commit
49ee9c908a
1 changed files with 12 additions and 5 deletions
|
@ -5,7 +5,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime/debug"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -158,7 +157,7 @@ func dive(dst reflect.Value) (v reflect.Value, t reflect.Type, err gperr.Error)
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
dst.Set(reflect.MakeSlice(dstT, 0, 0))
|
dst.Set(reflect.MakeSlice(dstT, 0, 0))
|
||||||
default:
|
default:
|
||||||
err = gperr.Errorf("deserialize: %w for dst %s", ErrInvalidType, dstT.String())
|
err = gperr.Errorf("dive: %w for dst %s", ErrInvalidType, dstT.String())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,7 +188,7 @@ func MapUnmarshalValidate(src SerializedObject, dst any) (err gperr.Error) {
|
||||||
dstV.Set(reflect.Zero(dstT))
|
dstV.Set(reflect.Zero(dstT))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return gperr.Errorf("deserialize: src is %w and dst is not settable\n%s", ErrNilValue, debug.Stack())
|
return gperr.Errorf("unmarshal: src is %w and dst is not settable", ErrNilValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
if dstT.Implements(mapUnmarshalerType) {
|
if dstT.Implements(mapUnmarshalerType) {
|
||||||
|
@ -209,7 +208,7 @@ func MapUnmarshalValidate(src SerializedObject, dst any) (err gperr.Error) {
|
||||||
// convert target fields to lower no-snake
|
// convert target fields to lower no-snake
|
||||||
// then check if the field of data is in the target
|
// then check if the field of data is in the target
|
||||||
|
|
||||||
errs := gperr.NewBuilder("deserialize error")
|
errs := gperr.NewBuilder()
|
||||||
|
|
||||||
switch dstV.Kind() {
|
switch dstV.Kind() {
|
||||||
case reflect.Struct, reflect.Interface:
|
case reflect.Struct, reflect.Interface:
|
||||||
|
@ -422,6 +421,9 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr gpe
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, gperr.Wrap(err)
|
return true, gperr.Wrap(err)
|
||||||
}
|
}
|
||||||
|
if d < 0 {
|
||||||
|
return true, gperr.New("duration must be greater than 0 if defined")
|
||||||
|
}
|
||||||
dst.Set(reflect.ValueOf(d))
|
dst.Set(reflect.ValueOf(d))
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
@ -440,7 +442,12 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr gpe
|
||||||
i, err = strconv.ParseFloat(src, dstT.Bits())
|
i, err = strconv.ParseFloat(src, dstT.Bits())
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, gperr.Wrap(err)
|
// avoid long error message
|
||||||
|
var numErr *strconv.NumError
|
||||||
|
if errors.As(err, &numErr) {
|
||||||
|
return true, gperr.Wrap(numErr.Err, dstT.Name()).Subject(src)
|
||||||
|
}
|
||||||
|
return true, gperr.Wrap(err).Subject(src)
|
||||||
}
|
}
|
||||||
dst.Set(reflect.ValueOf(i).Convert(dstT))
|
dst.Set(reflect.ValueOf(i).Convert(dstT))
|
||||||
return
|
return
|
||||||
|
|
Loading…
Add table
Reference in a new issue