From 49ee9c908a8b5a5aeba8511a7424a199c2c47473 Mon Sep 17 00:00:00 2001 From: yusing Date: Thu, 10 Apr 2025 04:52:44 +0800 Subject: [PATCH] chore: better error formatting --- internal/utils/serialization.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/utils/serialization.go b/internal/utils/serialization.go index 25eb3fcb..9430905e 100644 --- a/internal/utils/serialization.go +++ b/internal/utils/serialization.go @@ -5,7 +5,6 @@ import ( "errors" "os" "reflect" - "runtime/debug" "strconv" "strings" "time" @@ -158,7 +157,7 @@ func dive(dst reflect.Value) (v reflect.Value, t reflect.Type, err gperr.Error) case reflect.Slice: dst.Set(reflect.MakeSlice(dstT, 0, 0)) default: - err = gperr.Errorf("deserialize: %w for dst %s", ErrInvalidType, dstT.String()) + err = gperr.Errorf("dive: %w for dst %s", ErrInvalidType, dstT.String()) return } } @@ -189,7 +188,7 @@ func MapUnmarshalValidate(src SerializedObject, dst any) (err gperr.Error) { dstV.Set(reflect.Zero(dstT)) 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) { @@ -209,7 +208,7 @@ func MapUnmarshalValidate(src SerializedObject, dst any) (err gperr.Error) { // convert target fields to lower no-snake // then check if the field of data is in the target - errs := gperr.NewBuilder("deserialize error") + errs := gperr.NewBuilder() switch dstV.Kind() { case reflect.Struct, reflect.Interface: @@ -422,6 +421,9 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr gpe if err != nil { 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)) return default: @@ -440,7 +442,12 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr gpe i, err = strconv.ParseFloat(src, dstT.Bits()) } 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)) return