Skip to content

Commit 3908a1e

Browse files
skriptblespeakeang
andcommitted
Add wrapped error to MarshalError output
Closes #113 Change-Id: I6f03ccb37641d3522fd0f04772e991659eb7fc16 Co-authored-by: speakeang <[email protected]> Co-authored-by: Kris Brandow <[email protected]>
1 parent 95034ee commit 3908a1e

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

mongo/mongo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type MarshalError struct {
5555

5656
// Error implements the error interface.
5757
func (me MarshalError) Error() string {
58-
return fmt.Sprintf("cannot transform type %s to a *bsonx.Document", reflect.TypeOf(me.Value))
58+
return fmt.Sprintf("cannot transform type %s to a BSON Document: %v", reflect.TypeOf(me.Value), me.Err)
5959
}
6060

6161
// Pipeline is a type that makes creating aggregation pipelines easier. It is a

mongo/mongo_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func TestTransformDocument(t *testing.T) {
5757
"unsupported type",
5858
[]string{"foo", "bar"},
5959
nil,
60-
MarshalError{Value: []string{"foo", "bar"}, Err: errors.New("invalid state transition: TopLevel -> ArrayMode")},
60+
MarshalError{
61+
Value: []string{"foo", "bar"},
62+
Err: errors.New("WriteArray can only write a Array while positioned on a Element or Value but is positioned on a TopLevel")},
6163
},
6264
{
6365
"nil",
@@ -162,7 +164,11 @@ func TestTransformAggregatePipeline(t *testing.T) {
162164
arr bsonx.Arr
163165
err error
164166
}{
165-
{"Pipeline/error", Pipeline{{{"hello", func() {}}}}, bsonx.Arr{}, MarshalError{Value: primitive.D{}}},
167+
{
168+
"Pipeline/error",
169+
Pipeline{{{"hello", func() {}}}}, bsonx.Arr{},
170+
MarshalError{Value: primitive.D{}, Err: errors.New("no encoder found for func()")},
171+
},
166172
{
167173
"Pipeline/success",
168174
Pipeline{{{"hello", "world"}}, {{"pi", 3.14159}}},
@@ -188,7 +194,7 @@ func TestTransformAggregatePipeline(t *testing.T) {
188194
"primitive.A/error",
189195
primitive.A{"5"},
190196
bsonx.Arr{},
191-
MarshalError{Value: string("")},
197+
MarshalError{Value: string(""), Err: errors.New("WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel")},
192198
},
193199
{
194200
"primitive.A/success",
@@ -203,7 +209,7 @@ func TestTransformAggregatePipeline(t *testing.T) {
203209
"bson.A/error",
204210
bson.A{"5"},
205211
bsonx.Arr{},
206-
MarshalError{Value: string("")},
212+
MarshalError{Value: string(""), Err: errors.New("WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel")},
207213
},
208214
{
209215
"bson.A/success",
@@ -218,7 +224,7 @@ func TestTransformAggregatePipeline(t *testing.T) {
218224
"[]interface{}/error",
219225
[]interface{}{"5"},
220226
bsonx.Arr{},
221-
MarshalError{Value: string("")},
227+
MarshalError{Value: string(""), Err: errors.New("WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel")},
222228
},
223229
{
224230
"[]interface{}/success",
@@ -269,7 +275,7 @@ func TestTransformAggregatePipeline(t *testing.T) {
269275
"array/error",
270276
[1]interface{}{int64(42)},
271277
bsonx.Arr{},
272-
MarshalError{Value: int64(0)},
278+
MarshalError{Value: int64(0), Err: errors.New("WriteInt64 can only write while positioned on a Element or Value but is positioned on a TopLevel")},
273279
},
274280
{
275281
"array/success",
@@ -281,7 +287,7 @@ func TestTransformAggregatePipeline(t *testing.T) {
281287
"slice/error",
282288
[]interface{}{int64(42)},
283289
bsonx.Arr{},
284-
MarshalError{Value: int64(0)},
290+
MarshalError{Value: int64(0), Err: errors.New("WriteInt64 can only write while positioned on a Element or Value but is positioned on a TopLevel")},
285291
},
286292
{
287293
"slice/success",

0 commit comments

Comments
 (0)