diff --git a/minmax.go b/minmax.go index fa3b23a..c2e7a31 100644 --- a/minmax.go +++ b/minmax.go @@ -59,7 +59,7 @@ func (r *ThresholdRule) Exclusive() *ThresholdRule { } // Validate checks if the given value is valid or not. -func (r *ThresholdRule) Validate(value interface{}) error { +func (r *ThresholdRule) Validate(value interface{}) ExternalError { value, isNil := Indirect(value) if isNil || IsEmpty(value) { return nil @@ -70,7 +70,7 @@ func (r *ThresholdRule) Validate(value interface{}) error { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: v, err := ToInt(value) if err != nil { - return err + return NewExternalError(err, 1000) } if r.compareInt(rv.Int(), v) { return nil @@ -79,7 +79,7 @@ func (r *ThresholdRule) Validate(value interface{}) error { case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: v, err := ToUint(value) if err != nil { - return err + return NewExternalError(err, 1000) } if r.compareUint(rv.Uint(), v) { return nil @@ -88,7 +88,7 @@ func (r *ThresholdRule) Validate(value interface{}) error { case reflect.Float32, reflect.Float64: v, err := ToFloat(value) if err != nil { - return err + return NewExternalError(err, 1000) } if r.compareFloat(rv.Float(), v) { return nil @@ -97,21 +97,21 @@ func (r *ThresholdRule) Validate(value interface{}) error { case reflect.Struct: t, ok := r.threshold.(time.Time) if !ok { - return fmt.Errorf("type not supported: %v", rv.Type()) + return NewExternalError(fmt.Errorf("type not supported: %v", rv.Type()), 1000) } v, ok := value.(time.Time) if !ok { - return fmt.Errorf("cannot convert %v to time.Time", reflect.TypeOf(value)) + return NewExternalError(fmt.Errorf("cannot convert %v to time.Time", reflect.TypeOf(value)), 1000) } if v.IsZero() || r.compareTime(t, v) { return nil } default: - return fmt.Errorf("type not supported: %v", rv.Type()) + return NewExternalError(fmt.Errorf("type not supported: %v", rv.Type()), 1000) } - return errors.New(r.message) + return NewExternalError(errors.New(r.message), 1000) } // Error sets the error message for the rule. diff --git a/minmax_test.go b/minmax_test.go index f746f4c..ad54423 100644 --- a/minmax_test.go +++ b/minmax_test.go @@ -23,33 +23,33 @@ func TestMin(t *testing.T) { // int cases {"t1.1", 1, false, 1, ""}, {"t1.2", 1, false, 2, ""}, - {"t1.3", 1, false, -1, "must be no less than 1"}, + {"t1.3", 1, false, -1, "must be no less than 1, ErrCode: 1000"}, {"t1.4", 1, false, 0, ""}, - {"t1.5", 1, true, 1, "must be greater than 1"}, - {"t1.6", 1, false, "1", "cannot convert string to int64"}, - {"t1.7", "1", false, 1, "type not supported: string"}, + {"t1.5", 1, true, 1, "must be greater than 1, ErrCode: 1000"}, + {"t1.6", 1, false, "1", "cannot convert string to int64, ErrCode: 1000"}, + {"t1.7", "1", false, 1, "type not supported: string, ErrCode: 1000"}, // uint cases {"t2.1", uint(2), false, uint(2), ""}, {"t2.2", uint(2), false, uint(3), ""}, - {"t2.3", uint(2), false, uint(1), "must be no less than 2"}, + {"t2.3", uint(2), false, uint(1), "must be no less than 2, ErrCode: 1000"}, {"t2.4", uint(2), false, uint(0), ""}, - {"t2.5", uint(2), true, uint(2), "must be greater than 2"}, - {"t2.6", uint(2), false, "1", "cannot convert string to uint64"}, + {"t2.5", uint(2), true, uint(2), "must be greater than 2, ErrCode: 1000"}, + {"t2.6", uint(2), false, "1", "cannot convert string to uint64, ErrCode: 1000"}, // float cases {"t3.1", float64(2), false, float64(2), ""}, {"t3.2", float64(2), false, float64(3), ""}, - {"t3.3", float64(2), false, float64(1), "must be no less than 2"}, + {"t3.3", float64(2), false, float64(1), "must be no less than 2, ErrCode: 1000"}, {"t3.4", float64(2), false, float64(0), ""}, - {"t3.5", float64(2), true, float64(2), "must be greater than 2"}, - {"t3.6", float64(2), false, "1", "cannot convert string to float64"}, + {"t3.5", float64(2), true, float64(2), "must be greater than 2, ErrCode: 1000"}, + {"t3.6", float64(2), false, "1", "cannot convert string to float64, ErrCode: 1000"}, // Time cases {"t4.1", date20000601, false, date20000601, ""}, {"t4.2", date20000601, false, date20001201, ""}, - {"t4.3", date20000601, false, date20000101, "must be no less than 2000-06-01 00:00:00 +0000 UTC"}, + {"t4.3", date20000601, false, date20000101, "must be no less than 2000-06-01 00:00:00 +0000 UTC, ErrCode: 1000"}, {"t4.4", date20000601, false, date0, ""}, - {"t4.5", date20000601, true, date20000601, "must be greater than 2000-06-01 00:00:00 +0000 UTC"}, - {"t4.6", date20000601, true, 1, "cannot convert int to time.Time"}, - {"t4.7", struct{}{}, false, 1, "type not supported: struct {}"}, + {"t4.5", date20000601, true, date20000601, "must be greater than 2000-06-01 00:00:00 +0000 UTC, ErrCode: 1000"}, + {"t4.6", date20000601, true, 1, "cannot convert int to time.Time, ErrCode: 1000"}, + {"t4.7", struct{}{}, false, 1, "type not supported: struct {}, ErrCode: 1000"}, {"t4.8", date0, false, date20000601, ""}, }