1
+ #![ warn( missing_docs) ]
2
+
1
3
//! A library for consistent and reliable error handling
2
4
//!
3
5
//! This crate defines an opinionated strategy for error handling in Rust,
317
319
extern crate backtrace;
318
320
319
321
use std:: error;
322
+ use std:: iter:: Iterator ;
323
+ use std:: sync:: Arc ;
320
324
321
325
#[ cfg( feature = "backtrace" ) ]
322
326
pub use backtrace:: Backtrace ;
@@ -348,27 +352,28 @@ macro_rules! error_chain {
348
352
) => {
349
353
350
354
351
- // The Error type
352
- // --------------
353
-
354
- // This has a simple structure to support pattern matching
355
- // during error handling. The second field is internal state
356
- // that is mostly irrelevant for error handling purposes.
355
+ /// The Error type
356
+ ///
357
+ /// This has a simple structure to support pattern matching
358
+ /// during error handling. The second field is internal state
359
+ /// that is mostly irrelevant for error handling purposes.
357
360
#[ derive( Debug ) ]
358
361
pub struct $error_name( pub $error_kind_name,
359
362
pub ( Option <Box <:: std:: error:: Error + Send >>,
360
363
Option <:: std:: sync:: Arc <$crate:: Backtrace >>) ) ;
361
364
362
- #[ allow( unused) ]
363
365
impl $error_name {
366
+ /// Returns the kind of the error.
364
367
pub fn kind( & self ) -> & $error_kind_name {
365
368
& self . 0
366
369
}
367
370
371
+ /// Iterates over the error chain.
368
372
pub fn iter( & self ) -> $crate:: ErrorChainIter {
369
373
$crate:: ErrorChainIter ( Some ( self ) )
370
374
}
371
375
376
+ /// Returns the backtrace associated with this error.
372
377
pub fn backtrace( & self ) -> Option <& $crate:: Backtrace > {
373
378
( self . 1 ) . 1 . as_ref( ) . map( |v| & * * v)
374
379
}
@@ -504,10 +509,6 @@ macro_rules! error_chain {
504
509
$error_name( kind, backtrace)
505
510
}
506
511
507
- // Use downcasts to extract the backtrace from types we know,
508
- // to avoid generating a new one. It would be better to not
509
- // define this in the macro, but types need some additional
510
- // machinery to make it work.
511
512
fn backtrace_from_box( mut e: Box <:: std:: error:: Error + Send + ' static >)
512
513
-> ( Box <:: std:: error:: Error + Send + ' static >,
513
514
Option <Option <:: std:: sync:: Arc <$crate:: Backtrace >>>) {
@@ -651,17 +652,13 @@ macro_rules! error_chain {
651
652
) ;
652
653
}
653
654
654
-
655
- use std:: error:: Error as StdError ;
656
- use std:: iter:: Iterator ;
657
- use std:: sync:: Arc ;
658
-
659
- pub struct ErrorChainIter < ' a > ( pub Option < & ' a StdError > ) ;
655
+ /// Iterator over the error chain.
656
+ pub struct ErrorChainIter < ' a > ( pub Option < & ' a error:: Error > ) ;
660
657
661
658
impl < ' a > Iterator for ErrorChainIter < ' a > {
662
- type Item = & ' a StdError ;
659
+ type Item = & ' a error :: Error ;
663
660
664
- fn next < ' b > ( & ' b mut self ) -> Option < & ' a StdError > {
661
+ fn next < ' b > ( & ' b mut self ) -> Option < & ' a error :: Error > {
665
662
match self . 0 . take ( ) {
666
663
Some ( e) => {
667
664
self . 0 = e. cause ( ) ;
@@ -688,13 +685,21 @@ pub fn make_backtrace() -> Option<Arc<Backtrace>> {
688
685
None
689
686
}
690
687
688
+ /// This trait is an implementation detail, you should have to implement or
689
+ /// use it.
691
690
pub trait Error : error:: Error + Send + ' static {
691
+ /// Associated kind type.
692
692
type ErrorKind ;
693
- fn new ( kind : Self :: ErrorKind , backtrace : ( Option < Box < error:: Error + Send + ' static > > ,
694
- Option < Arc < Backtrace > > ) ) -> Self ;
693
+ /// Creates an error from it's parts.
694
+ fn new ( kind : Self :: ErrorKind ,
695
+ state : ( Option < Box < error:: Error + Send + ' static > > ,
696
+ Option < Arc < Backtrace > > ) ) -> Self ;
697
+ /// Use downcasts to extract the backtrace from types we know,
698
+ /// to avoid generating a new one. It would be better to not
699
+ /// define this in the macro, but types need some additional
700
+ /// machinery to make it work.
695
701
fn backtrace_from_box ( e : Box < error:: Error + Send + ' static > )
696
- -> ( Box < error:: Error + Send + ' static > ,
697
- Option < Option < Arc < Backtrace > > > ) ;
702
+ -> ( Box < error:: Error + Send + ' static > , Option < Option < Arc < Backtrace > > > ) ;
698
703
}
699
704
700
705
/// Additionnal methods for `Result`, for easy interaction with this crate.
0 commit comments