11//! Contains the `Error` and `Result` types that `mongodb` uses.
22
3- use std:: {
4- fmt:: { self , Debug } ,
5- sync:: Arc ,
6- } ;
3+ use std:: { collections:: HashSet , fmt:: { self , Debug } , sync:: Arc } ;
74
85use serde:: Deserialize ;
96use thiserror:: Error ;
@@ -43,10 +40,17 @@ pub type Result<T> = std::result::Result<T, Error>;
4340pub struct Error {
4441 /// The type of error that occurred.
4542 pub kind : Box < ErrorKind > ,
46- labels : Vec < String > ,
43+ pub ( crate ) labels : HashSet < String > ,
4744}
4845
4946impl 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+
5054 pub ( crate ) fn pool_cleared_error ( address : & ServerAddress ) -> Self {
5155 ErrorKind :: ConnectionPoolCleared {
5256 message : format ! (
@@ -159,19 +163,8 @@ impl Error {
159163 }
160164
161165 /// 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
175168 }
176169
177170 /// Whether this error contains the specified label.
@@ -184,30 +177,7 @@ impl Error {
184177 /// Adds the given label to this error.
185178 pub ( crate ) fn add_label < T : AsRef < str > > ( & mut self , label : T ) {
186179 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) ;
211181 }
212182
213183 pub ( crate ) fn from_resolve_error ( error : trust_dns_resolver:: error:: ResolveError ) -> Self {
@@ -325,7 +295,7 @@ where
325295 fn from ( err : E ) -> Self {
326296 Self {
327297 kind : Box :: new ( err. into ( ) ) ,
328- labels : Vec :: new ( ) ,
298+ labels : Default :: default ( ) ,
329299 }
330300 }
331301}
@@ -447,10 +417,6 @@ pub struct CommandError {
447417 /// A description of the error that occurred.
448418 #[ serde( rename = "errmsg" ) ]
449419 pub message : String ,
450-
451- /// The error labels that the server returned.
452- #[ serde( rename = "errorLabels" , default ) ]
453- pub labels : Vec < String > ,
454420}
455421
456422impl fmt:: Display for CommandError {
@@ -477,10 +443,6 @@ pub struct WriteConcernError {
477443 /// A document identifying the write concern setting related to the error.
478444 #[ serde( rename = "errInfo" ) ]
479445 pub details : Option < Document > ,
480-
481- /// The error labels that the server returned.
482- #[ serde( rename = "errorLabels" , default ) ]
483- pub labels : Vec < String > ,
484446}
485447
486448/// An error that occurred during a write operation that wasn't due to being unable to satisfy a
@@ -584,9 +546,9 @@ impl WriteFailure {
584546/// untouched.
585547pub ( crate ) fn convert_bulk_errors ( error : Error ) -> Error {
586548 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 ) ) ,
590552 Err ( e) => e,
591553 }
592554 }
0 commit comments