Skip to content

Commit

Permalink
change err
Browse files Browse the repository at this point in the history
  • Loading branch information
cadyrov committed Oct 30, 2020
1 parent 91eb35b commit da3708d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
12 changes: 7 additions & 5 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 2 additions & 9 deletions verror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit da3708d

Please sign in to comment.