Skip to content

Commit f564a11

Browse files
committed
Enrich error message for reduce function
Signed-off-by: Dinesh <dineshudt17@gmail.com>
1 parent 78bc879 commit f564a11

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

reduce.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ func Reduce(in, out, reduceFn interface{}) error {
2222
outputKind := output.Elem().Kind()
2323
reducerFnType := reducer.Type()
2424
if outputKind != reducerFnType.In(0).Kind() {
25-
return fmt.Errorf("reducer function's first argument has to be the type of out")
25+
return fmt.Errorf("reduceFn's first argument's type(%s) has to be the type of out(%s)", reducerFnType.In(0).Kind(), outputKind)
2626
}
2727
if input.Type().Elem().Kind() != reducerFnType.In(1).Kind() {
28-
return fmt.Errorf("reducer function's second argument has to be the type of element of input slice")
28+
return fmt.Errorf("reduceFn's second argument's type(%s) has to be the type of element of input slice(%s)", reducerFnType.In(1).Kind(), input.Type().Elem().Kind())
2929
}
3030
if outputKind != reducerFnType.Out(0).Kind() {
31-
return fmt.Errorf("reducer function's return type has to be the type of out")
31+
return fmt.Errorf("reduceFn's return type(%s) has to be the type of out(%s)", reducerFnType.Out(0).Kind(), outputKind)
3232
}
3333

3434
result := output.Elem()
@@ -48,13 +48,13 @@ func Reduce(in, out, reduceFn interface{}) error {
4848
func validateReducer(reducer reflect.Value) error {
4949
reducerFnType := reducer.Type()
5050
if reducer.Kind() != reflect.Func {
51-
return fmt.Errorf("reduceFn has to be a function")
51+
return fmt.Errorf("reduceFn has to be a (func) and not (%s)", reducer.Kind())
5252
}
5353
if reducerFnType.NumIn() != 2 {
54-
return fmt.Errorf("reducer function has to take exactly two argument")
54+
return fmt.Errorf("reduceFn has to take exactly 2 arguments and not %d argument(s)", reducerFnType.NumIn())
5555
}
5656
if reducerFnType.NumOut() != 1 {
57-
return fmt.Errorf("reducer function should return only one return value")
57+
return fmt.Errorf("reduceFn should have only one return value and not %d return type(s)", reducerFnType.NumOut())
5858
}
5959
return nil
6060
}

reduce_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestReduce(t *testing.T) {
8383

8484
err := godash.Reduce(in, &out, 7)
8585

86-
assert.EqualError(t, err, "reduceFn has to be a function")
86+
assert.EqualError(t, err, "reduceFn has to be a (func) and not (int)")
8787
})
8888

8989
t.Run("should not accept reducer function that do not take exactly two argument", func(t *testing.T) {
@@ -92,12 +92,12 @@ func TestReduce(t *testing.T) {
9292

9393
{
9494
err := godash.Reduce(in, &out, func() int { return 0 })
95-
assert.EqualError(t, err, "reducer function has to take exactly two argument")
95+
assert.EqualError(t, err, "reduceFn has to take exactly 2 arguments and not 0 argument(s)")
9696
}
9797

9898
{
9999
err := godash.Reduce(in, &out, func(int) int { return 0 })
100-
assert.EqualError(t, err, "reducer function has to take exactly two argument")
100+
assert.EqualError(t, err, "reduceFn has to take exactly 2 arguments and not 1 argument(s)")
101101
}
102102
})
103103

@@ -107,12 +107,12 @@ func TestReduce(t *testing.T) {
107107

108108
{
109109
err := godash.Reduce(in, &out, func(int, int) {})
110-
assert.EqualError(t, err, "reducer function should return only one return value")
110+
assert.EqualError(t, err, "reduceFn should have only one return value and not 0 return type(s)")
111111
}
112112

113113
{
114114
err := godash.Reduce(in, &out, func(int, int) (int, int) { return 0, 0 })
115-
assert.EqualError(t, err, "reducer function should return only one return value")
115+
assert.EqualError(t, err, "reduceFn should have only one return value and not 2 return type(s)")
116116
}
117117
})
118118

@@ -122,7 +122,7 @@ func TestReduce(t *testing.T) {
122122

123123
{
124124
err := godash.Reduce(in, &out, func(string, int) int { return 0 })
125-
assert.EqualError(t, err, "reducer function's first argument has to be the type of out")
125+
assert.EqualError(t, err, "reduceFn's first argument's type(string) has to be the type of out(int)")
126126
}
127127

128128
{
@@ -137,7 +137,7 @@ func TestReduce(t *testing.T) {
137137

138138
{
139139
err := godash.Reduce(in, &out, func(string, string) string { return "" })
140-
assert.EqualError(t, err, "reducer function's second argument has to be the type of element of input slice")
140+
assert.EqualError(t, err, "reduceFn's second argument's type(string) has to be the type of element of input slice(int)")
141141
}
142142

143143
{
@@ -152,7 +152,7 @@ func TestReduce(t *testing.T) {
152152

153153
{
154154
err := godash.Reduce(in, &out, func(string, int) int { return 0 })
155-
assert.EqualError(t, err, "reducer function's return type has to be the type of out")
155+
assert.EqualError(t, err, "reduceFn's return type(int) has to be the type of out(string)")
156156
}
157157

158158
{

0 commit comments

Comments
 (0)