1
1
//! Contains the `Error` and `Result` types that `mongodb` uses.
2
2
3
- use std:: {
4
- fmt:: { self , Debug } ,
5
- sync:: Arc ,
6
- } ;
3
+ use std:: { collections:: HashSet , fmt:: { self , Debug } , sync:: Arc } ;
7
4
8
5
use serde:: Deserialize ;
9
6
use thiserror:: Error ;
@@ -43,10 +40,17 @@ pub type Result<T> = std::result::Result<T, Error>;
43
40
pub struct Error {
44
41
/// The type of error that occurred.
45
42
pub kind : Box < ErrorKind > ,
46
- labels : Vec < String > ,
43
+ pub ( crate ) labels : HashSet < String > ,
47
44
}
48
45
49
46
impl Error {
47
+ pub ( crate ) fn new ( kind : ErrorKind , labels : Option < impl IntoIterator < Item =String > > ) -> Self {
48
+ Self {
49
+ kind : Box :: new ( kind) ,
50
+ labels : labels. map ( |labels| labels. into_iter ( ) . collect ( ) ) . unwrap_or_default ( ) ,
51
+ }
52
+ }
53
+
50
54
pub ( crate ) fn pool_cleared_error ( address : & ServerAddress ) -> Self {
51
55
ErrorKind :: ConnectionPoolCleared {
52
56
message : format ! (
@@ -159,19 +163,8 @@ impl Error {
159
163
}
160
164
161
165
/// Returns the labels for this error.
162
- pub fn labels ( & self ) -> & [ String ] {
163
- match self . kind . as_ref ( ) {
164
- ErrorKind :: Command ( ref err) => & err. labels ,
165
- ErrorKind :: Write ( ref err) => match err {
166
- WriteFailure :: WriteError ( _) => & self . labels ,
167
- WriteFailure :: WriteConcernError ( ref err) => & err. labels ,
168
- } ,
169
- ErrorKind :: BulkWrite ( ref err) => match err. write_concern_error {
170
- Some ( ref err) => & err. labels ,
171
- None => & self . labels ,
172
- } ,
173
- _ => & self . labels ,
174
- }
166
+ pub fn labels ( & self ) -> & HashSet < String > {
167
+ & self . labels
175
168
}
176
169
177
170
/// Whether this error contains the specified label.
@@ -184,30 +177,7 @@ impl Error {
184
177
/// Adds the given label to this error.
185
178
pub ( crate ) fn add_label < T : AsRef < str > > ( & mut self , label : T ) {
186
179
let label = label. as_ref ( ) . to_string ( ) ;
187
- match self . kind . as_mut ( ) {
188
- ErrorKind :: Command ( err) => {
189
- err. labels . push ( label) ;
190
- }
191
- ErrorKind :: Write ( err) => match err {
192
- WriteFailure :: WriteError ( _) => {
193
- self . labels . push ( label) ;
194
- }
195
- WriteFailure :: WriteConcernError ( err) => {
196
- err. labels . push ( label) ;
197
- }
198
- } ,
199
- ErrorKind :: BulkWrite ( err) => match err. write_concern_error . as_mut ( ) {
200
- Some ( write_concern_error) => {
201
- write_concern_error. labels . push ( label) ;
202
- }
203
- None => {
204
- self . labels . push ( label) ;
205
- }
206
- } ,
207
- _ => {
208
- self . labels . push ( label) ;
209
- }
210
- }
180
+ self . labels . insert ( label) ;
211
181
}
212
182
213
183
pub ( crate ) fn from_resolve_error ( error : trust_dns_resolver:: error:: ResolveError ) -> Self {
@@ -325,7 +295,7 @@ where
325
295
fn from ( err : E ) -> Self {
326
296
Self {
327
297
kind : Box :: new ( err. into ( ) ) ,
328
- labels : Vec :: new ( ) ,
298
+ labels : Default :: default ( ) ,
329
299
}
330
300
}
331
301
}
@@ -447,10 +417,6 @@ pub struct CommandError {
447
417
/// A description of the error that occurred.
448
418
#[ serde( rename = "errmsg" ) ]
449
419
pub message : String ,
450
-
451
- /// The error labels that the server returned.
452
- #[ serde( rename = "errorLabels" , default ) ]
453
- pub labels : Vec < String > ,
454
420
}
455
421
456
422
impl fmt:: Display for CommandError {
@@ -477,10 +443,6 @@ pub struct WriteConcernError {
477
443
/// A document identifying the write concern setting related to the error.
478
444
#[ serde( rename = "errInfo" ) ]
479
445
pub details : Option < Document > ,
480
-
481
- /// The error labels that the server returned.
482
- #[ serde( rename = "errorLabels" , default ) ]
483
- pub labels : Vec < String > ,
484
446
}
485
447
486
448
/// An error that occurred during a write operation that wasn't due to being unable to satisfy a
@@ -584,9 +546,9 @@ impl WriteFailure {
584
546
/// untouched.
585
547
pub ( crate ) fn convert_bulk_errors ( error : Error ) -> Error {
586
548
match * error. kind {
587
- ErrorKind :: BulkWrite ( ref bulk_failure) => {
588
- match WriteFailure :: from_bulk_failure ( bulk_failure. clone ( ) ) {
589
- Ok ( failure) => ErrorKind :: Write ( failure) . into ( ) ,
549
+ ErrorKind :: BulkWrite ( bulk_failure) => {
550
+ match WriteFailure :: from_bulk_failure ( bulk_failure) {
551
+ Ok ( failure) => Error :: new ( ErrorKind :: Write ( failure) , Some ( error . labels ) ) ,
590
552
Err ( e) => e,
591
553
}
592
554
}
0 commit comments