@@ -12,6 +12,7 @@ import (
1212 "errors"
1313 "fmt"
1414 "net"
15+ "reflect"
1516 "strings"
1617
1718 "go.mongodb.org/mongo-driver/v2/bson"
@@ -138,6 +139,12 @@ func wrapErrors(err error) error {
138139 Code : me .Code ,
139140 Message : me .Message ,
140141 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 ,
141148 }
142149 }
143150
@@ -150,6 +157,12 @@ func wrapErrors(err error) error {
150157 return MarshalError {
151158 Value : marshalErr .Value ,
152159 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 ,
153166 }
154167 }
155168
@@ -227,15 +240,27 @@ func IsNetworkError(err error) bool {
227240type MarshalError struct {
228241 Value interface {}
229242 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
230252}
231253
232254// Error implements the error interface.
233255func (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 )
239264}
240265
241266func (me MarshalError ) Unwrap () error { return me .Err }
@@ -245,15 +270,27 @@ type MongocryptError struct {
245270 Code int32
246271 Message string
247272 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
248282}
249283
250284// Error implements the error interface.
251285func (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 )
257294}
258295
259296// Unwrap returns the underlying error.
@@ -344,7 +381,7 @@ type CommandError struct {
344381 Raw bson.Raw // The original server response containing the error.
345382
346383 // If wrappedMsgOnly is true, Error() only returns the error message from
347- // the Wrapped error.
384+ // the " Wrapped" error.
348385 //
349386 // This is typically only set by the wrapErrors function, which uses
350387 // CommandError to wrap driver.Error, allowing users to access the "Code",
@@ -356,7 +393,7 @@ type CommandError struct {
356393// Error implements the error interface.
357394func (e CommandError ) Error () string {
358395 // 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
360397 // for more info.
361398 if e .wrappedMsgOnly {
362399 return e .Wrapped .Error ()
0 commit comments