@@ -12,6 +12,7 @@ import (
12
12
"errors"
13
13
"fmt"
14
14
"net"
15
+ "reflect"
15
16
"strings"
16
17
17
18
"go.mongodb.org/mongo-driver/v2/bson"
@@ -138,6 +139,12 @@ func wrapErrors(err error) error {
138
139
Code : me .Code ,
139
140
Message : me .Message ,
140
141
wrapped : err ,
142
+
143
+ // Set wrappedMsgOnly=true here so that the Code and Message are not
144
+ // repeated multiple times in the error string. We expect that the
145
+ // wrapped mongocrypt.Error already contains that info in the error
146
+ // string.
147
+ wrappedMsgOnly : true ,
141
148
}
142
149
}
143
150
@@ -150,6 +157,12 @@ func wrapErrors(err error) error {
150
157
return MarshalError {
151
158
Value : marshalErr .Value ,
152
159
Err : err ,
160
+
161
+ // Set wrappedMsgOnly=true here so that the Value is not repeated
162
+ // multiple times in the error string. We expect that the wrapped
163
+ // codecutil.MarshalError already contains that info in the error
164
+ // string.
165
+ wrappedMsgOnly : true ,
153
166
}
154
167
}
155
168
@@ -227,15 +240,27 @@ func IsNetworkError(err error) bool {
227
240
type MarshalError struct {
228
241
Value interface {}
229
242
Err error
243
+
244
+ // If wrappedMsgOnly is true, Error() only returns the error message from
245
+ // the "Err" error.
246
+ //
247
+ // This is typically only set by the wrapErrors function, which uses
248
+ // MarshalError to wrap codecutil.MarshalError, allowing users to access the
249
+ // "Value" from the underlying error but preventing duplication in the error
250
+ // string.
251
+ wrappedMsgOnly bool
230
252
}
231
253
232
254
// Error implements the error interface.
233
255
func (me MarshalError ) Error () string {
234
- // MarshalError in this package is only used as a wrapper for
235
- // codecutil.MarshalError that provides access to the "Value". Return the
236
- // error string without modifying it to prevent duplicating the error
237
- // string.
238
- return me .Err .Error ()
256
+ // If the MarshalError was created with wrappedMsgOnly=true, only return the
257
+ // error from the wrapped error. See the MarshalError.wrappedMsgOnly docs
258
+ // for more info.
259
+ if me .wrappedMsgOnly {
260
+ return me .Err .Error ()
261
+ }
262
+
263
+ return fmt .Sprintf ("cannot marshal type %s to a BSON Document: %v" , reflect .TypeOf (me .Value ), me .Err )
239
264
}
240
265
241
266
func (me MarshalError ) Unwrap () error { return me .Err }
@@ -245,15 +270,27 @@ type MongocryptError struct {
245
270
Code int32
246
271
Message string
247
272
wrapped error
273
+
274
+ // If wrappedMsgOnly is true, Error() only returns the error message from
275
+ // the "wrapped" error.
276
+ //
277
+ // This is typically only set by the wrapErrors function, which uses
278
+ // MarshalError to wrap mongocrypt.Error, allowing users to access the
279
+ // "Code" and "Message" from the underlying error but preventing duplication
280
+ // in the error string.
281
+ wrappedMsgOnly bool
248
282
}
249
283
250
284
// Error implements the error interface.
251
285
func (m MongocryptError ) Error () string {
252
- // MongocryptError in this package is only used as a wrapper for
253
- // mongocrypt.Error that provides access to the "Code" and "Message". Return
254
- // the error string without modifying it to prevent duplicating the error
255
- // string.
256
- return m .wrapped .Error ()
286
+ // If the MongocryptError was created with wrappedMsgOnly=true, only return
287
+ // the error from the wrapped error. See the MongocryptError.wrappedMsgOnly
288
+ // docs for more info.
289
+ if m .wrappedMsgOnly {
290
+ return m .wrapped .Error ()
291
+ }
292
+
293
+ return fmt .Sprintf ("mongocrypt error %d: %v" , m .Code , m .Message )
257
294
}
258
295
259
296
// Unwrap returns the underlying error.
@@ -344,7 +381,7 @@ type CommandError struct {
344
381
Raw bson.Raw // The original server response containing the error.
345
382
346
383
// If wrappedMsgOnly is true, Error() only returns the error message from
347
- // the Wrapped error.
384
+ // the " Wrapped" error.
348
385
//
349
386
// This is typically only set by the wrapErrors function, which uses
350
387
// CommandError to wrap driver.Error, allowing users to access the "Code",
@@ -356,7 +393,7 @@ type CommandError struct {
356
393
// Error implements the error interface.
357
394
func (e CommandError ) Error () string {
358
395
// If the CommandError was created with wrappedMsgOnly=true, only return the
359
- // error from the Wrapped error. See the CommandError.wrappedMsgOnly docs
396
+ // error from the wrapped error. See the CommandError.wrappedMsgOnly docs
360
397
// for more info.
361
398
if e .wrappedMsgOnly {
362
399
return e .Wrapped .Error ()
0 commit comments