This repository was archived by the owner on Aug 16, 2021. It is now read-only.
File tree 3 files changed +43
-108
lines changed
3 files changed +43
-108
lines changed Original file line number Diff line number Diff line change @@ -19,55 +19,7 @@ provides a few unique features:
19
19
20
20
## Quick start
21
21
22
- Add this to Cargo.toml, under ` [dependencies] ` :
23
-
24
- ``` toml
25
- error-chain = " 0.6"
26
- ```
27
-
28
- Write this at the top of your crate:
29
-
30
- ``` rust
31
- #![recursion_limit = " 1024" ]
32
- ```
33
-
34
- Again near the top of your crate, import the ` error_chain ` crate and its macros:
35
-
36
- ``` rust
37
- #[macro_use]
38
- extern crate error_chain;
39
- ```
40
-
41
- Add an ` errors ` module to your crate:
42
-
43
- ``` rust
44
- mod errors ;
45
- ```
46
-
47
- Add a file for that module called ` errors.rs ` and put this inside:
48
-
49
- ``` rust
50
- error_chain! { }
51
- ```
52
-
53
- That's the setup. Now when writing modules for your crate,
54
- import everything from the ` errors ` module:
55
-
56
- ``` rust
57
- use errors :: * ;
58
- ```
59
-
60
- Create functions that return ` Result ` , which is defined by
61
- the ` error_chain! ` macro, and start chaining errors!
62
-
63
- ``` rust
64
- fn do_error_prone_work () -> Result <()> {
65
- let file = try ! (File :: open (" foo" ). chain_err (|| " couldn't open file" ));
66
- try ! (file . write_all (" important" . as_bytes ()). chain_err (|| " couldn't write file" ));
67
-
68
- Ok (())
69
- }
70
- ```
22
+ See https://github.com/brson/error-chain/blob/master/examples/quickstart.rs .
71
23
72
24
## License
73
25
Original file line number Diff line number Diff line change
1
+ // This macro use recursion a lot.
2
+ #![ recursion_limit = "1024" ]
3
+
4
+ // Import of the macro. Don't forget to add `error-chain = "*"` in your
5
+ // `Cargo.toml`!
6
+ #[ macro_use]
7
+ extern crate error_chain;
8
+
9
+ // Generations of the `Error` type, the `ErrorKind` enum and the `Result`
10
+ // wrapper. See the documentation for more details.
11
+ //
12
+ // You can also generate the types in a dedicated modules, like `errors.rs`.
13
+ error_chain ! {
14
+ foreign_links {
15
+ // An IO error can occur.
16
+ Io ( :: std:: io:: Error ) ;
17
+ }
18
+ }
19
+
20
+ // With the `Result` wrapper, we don't have to specify `Error` each time.
21
+ fn do_some_work ( ) -> Result < ( ) > {
22
+ use std:: fs:: File ;
23
+ // Dummy file, this operation should fail on your system. The result of
24
+ // `File::open` is automatically converted to our `Error` type.
25
+ File :: open ( "tretrete" ) ?;
26
+ Ok ( ( ) )
27
+ }
28
+
29
+ fn main ( ) {
30
+ match do_some_work ( ) {
31
+ Ok ( ( ) ) => println ! ( "No errors" ) ,
32
+ Err ( e) => {
33
+ println ! ( "An error occured: {}" , e) ;
34
+ // The backtrace is not always generated. Try to run this example
35
+ // with `RUST_BACKTRACE=1`.
36
+ if let Some ( backtrace) = e. backtrace ( ) {
37
+ println ! ( "Backtrace: {:?}" , backtrace) ;
38
+ }
39
+ }
40
+ }
41
+ }
Original file line number Diff line number Diff line change 58
58
//!
59
59
//! ## Quick start
60
60
//!
61
- //! Add this to Cargo.toml, under `[dependencies]`:
62
- //!
63
- //! ```toml
64
- //! error-chain = "0.5"
65
- //! ```
66
- //!
67
- //! Write this at the top of your crate:
68
- //!
69
- //! ```
70
- //! #![recursion_limit = "1024"]
71
- //! # fn main() {}
72
- //! ```
73
- //!
74
- //! Again near the top of your crate, import the `error_chain` crate and its macros:
75
- //!
76
- //! ```
77
- //! #[macro_use]
78
- //! extern crate error_chain;
79
- //! # fn main() {}
80
- //! ```
81
- //!
82
- //! Add an `errors` module to your crate:
83
- //!
84
- //! ```ignore
85
- //! mod errors;
86
- //! ```
87
- //!
88
- //! Add a file for that module called `errors.rs` and put this inside:
89
- //!
90
- //! ```
91
- //! # #[macro_use] extern crate error_chain;
92
- //! # fn main() {}
93
- //! error_chain! { }
94
- //! ```
95
- //!
96
- //! That's the setup. Now when writing modules for your crate,
97
- //! import everything from the `errors` module:
98
- //!
99
- //! ```ignore
100
- //! use errors::*;
101
- //! ```
102
- //!
103
- //! Create functions that return `Result`, which is defined by
104
- //! the `error_chain!` macro, and start chaining errors!
105
- //!
106
- //! ```ignore
107
- //! # #[macro_use] extern crate error_chain;
108
- //! # use std::fs::File;
109
- //! # use std::io::Write;
110
- //! # use error_chain::ResultExt;
111
- //! # fn main() {}
112
- //! # error_chain! {}
113
- //! fn do_error_prone_work() -> Result<()> {
114
- //! let file = try!(File::open("foo").chain_err(|| "couldn't open file"));
115
- //! try!(file.write_all("important".as_bytes()).chain_err(|| "couldn't write file"));
116
- //!
117
- //! Ok(())
118
- //! }
119
- //! ```
61
+ //! See https://github.com/brson/error-chain/blob/master/examples/quickstart.rs.
120
62
//!
121
63
//! ## Declaring error types
122
64
//!
You can’t perform that action at this time.
0 commit comments