From da3708dc5b2a2ec4cd0686c75fb79f413a027ae3 Mon Sep 17 00:00:00 2001 From: kot Date: Fri, 30 Oct 2020 17:31:19 +0500 Subject: [PATCH] change err --- go.mod | 2 +- struct.go | 12 +++++++----- validation.go | 12 ++++++++---- verror/error.go | 11 ++--------- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index c0a6df4..448c013 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.12 require ( github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a - github.com/cadyrov/goerr v1.0.7 + github.com/cadyrov/goerr v1.0.9 golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443 // indirect golang.org/x/text v0.3.2 // indirect ) diff --git a/struct.go b/struct.go index 29095a7..488ac8b 100644 --- a/struct.go +++ b/struct.go @@ -48,7 +48,7 @@ func ValidateStruct(structPtr interface{}, fields ...*FieldRules) goerr.IError { } value = value.Elem() - errs := verror.NewErrStack("validatoin error") + errs := verror.NewErrStack("validation_error") for _, fr := range fields { fv := reflect.ValueOf(fr.fieldPtr) @@ -63,17 +63,19 @@ func ValidateStruct(structPtr interface{}, fields ...*FieldRules) goerr.IError { if ft.Anonymous { // merge errors from anonymous struct field if es, ok := err.(verror.ErrStack); ok { - for name, value := range es.Details { - errs.Details[name] = value + details := es.GetDetails() + for i := range details { + errs.PushDetail(details[i]) } continue } } - errs.Details[getErrorFieldName(ft)] = err + err.SetID(getErrorFieldName(ft)) + errs.PushDetail(err) } } - if len(errs.Details) > 0 { + if len(errs.GetDetails()) > 0 { return errs } return nil diff --git a/validation.go b/validation.go index 56e44d3..a802469 100644 --- a/validation.go +++ b/validation.go @@ -86,11 +86,13 @@ func validateMap(rv reflect.Value) goerr.IError { for _, key := range rv.MapKeys() { if mv := rv.MapIndex(key).Interface(); mv != nil { if code, args := mv.(Validatable).Validate(); code != 0 { - errs.Details[fmt.Sprintf("%v", key.Interface())] = verror.NewGoErr(code, args) + e := verror.NewGoErr(code, args) + e.SetID(fmt.Sprintf("%v", key.Interface())) + errs.PushDetail(e) } } } - if len(errs.Details) > 0 { + if len(errs.GetDetails()) > 0 { return errs } return nil @@ -103,11 +105,13 @@ func validateSlice(rv reflect.Value) goerr.IError { for i := 0; i < l; i++ { if ev := rv.Index(i).Interface(); ev != nil { if code, args := ev.(Validatable).Validate(); code != 0 { - errs.Details[strconv.Itoa(i)] = verror.NewGoErr(code, args) + e := verror.NewGoErr(code, args) + e.SetID(strconv.Itoa(i)) + errs.PushDetail(e) } } } - if len(errs.Details) > 0 { + if len(errs.GetDetails()) > 0 { return errs } return nil diff --git a/verror/error.go b/verror/error.go index a634618..4db0566 100644 --- a/verror/error.go +++ b/verror/error.go @@ -118,18 +118,11 @@ var mpErr map[int]string = map[int]string{ 2880: "snils_not_correct", } -type ErrStack struct { - Details map[string]goerr.IError `json:"details"` - goerr.IError `json:"error"` -} +type ErrStack goerr.IError func NewErrStack(message string) ErrStack { - mp := make(map[string]goerr.IError) e := goerr.New(message) - return ErrStack{ - Details: mp, - IError: e, - } + return e } func NewGoErr(code int, args ...interface{}) goerr.IError {