|
66 | 66 | //! |
67 | 67 | //! Write this at the top of your crate: |
68 | 68 | //! |
69 | | -//! ```ignore |
| 69 | +//! ``` |
70 | 70 | //! #![recursion_limit = "1024"] |
| 71 | +//! # fn main() {} |
71 | 72 | //! ``` |
72 | 73 | //! |
73 | 74 | //! Again near the top of your crate, import the `error_chain` crate and its macros: |
74 | 75 | //! |
75 | | -//! ```ignore |
| 76 | +//! ``` |
76 | 77 | //! #[macro_use] |
77 | 78 | //! extern crate error_chain; |
| 79 | +//! # fn main() {} |
78 | 80 | //! ``` |
79 | 81 | //! |
80 | 82 | //! Add an `errors` module to your crate: |
|
85 | 87 | //! |
86 | 88 | //! Add a file for that module called `errors.rs` and put this inside: |
87 | 89 | //! |
88 | | -//! ```ignore |
| 90 | +//! ``` |
| 91 | +//! # #[macro_use] extern crate error_chain; |
| 92 | +//! # fn main() {} |
89 | 93 | //! error_chain! { } |
90 | 94 | //! ``` |
91 | 95 | //! |
|
100 | 104 | //! the `error_chain!` macro, and start chaining errors! |
101 | 105 | //! |
102 | 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! {} |
103 | 113 | //! fn do_error_prone_work() -> Result<()> { |
104 | 114 | //! let file = try!(File::open("foo").chain_err(|| "couldn't open file")); |
105 | 115 | //! try!(file.write_all("important".as_bytes()).chain_err(|| "couldn't write file")); |
|
192 | 202 | //! use std::sync::Arc; |
193 | 203 | //! |
194 | 204 | //! #[derive(Debug)] |
195 | | -//! pub struct Error(pub ErrorKind, |
196 | | -//! pub Option<Box<StdError + Send>>, |
197 | | -//! pub Option<Arc<error_chain::Backtrace>>); |
| 205 | +//! pub struct Error { |
| 206 | +//! pub kind: ErrorKind, |
| 207 | +//! pub state: ::error_chain::State, |
| 208 | +//! } |
198 | 209 | //! |
199 | 210 | //! impl Error { |
200 | 211 | //! pub fn kind(&self) -> &ErrorKind { ... } |
|
238 | 249 | //! |
239 | 250 | //! Introducing new error chains, with a string message: |
240 | 251 | //! |
241 | | -//! ```ignore |
| 252 | +//! ``` |
| 253 | +//! # #[macro_use] extern crate error_chain; |
| 254 | +//! # fn main() {} |
| 255 | +//! # error_chain! {} |
242 | 256 | //! fn foo() -> Result<()> { |
243 | 257 | //! Err("foo error!".into()) |
244 | 258 | //! } |
245 | 259 | //! ``` |
246 | 260 | //! |
247 | 261 | //! Introducing new error chains, with an `ErrorKind`: |
248 | 262 | //! |
249 | | -//! ```ignore |
| 263 | +//! ``` |
| 264 | +//! # #[macro_use] extern crate error_chain; |
| 265 | +//! # fn main() {} |
| 266 | +//! error_chain! { |
| 267 | +//! errors { FooError } |
| 268 | +//! } |
| 269 | +//! |
250 | 270 | //! fn foo() -> Result<()> { |
251 | 271 | //! Err(ErrorKind::FooError.into()) |
252 | 272 | //! } |
|
264 | 284 | //! automatically convert `Err(ErrorKind)` to `Err(Error)`. So the |
265 | 285 | //! below is equivalent to the previous: |
266 | 286 | //! |
267 | | -//! ```ignore |
| 287 | +//! ``` |
| 288 | +//! # #[macro_use] extern crate error_chain; |
| 289 | +//! # fn main() {} |
| 290 | +//! # error_chain! { errors { FooError } } |
268 | 291 | //! fn foo() -> Result<()> { |
269 | 292 | //! Ok(try!(Err(ErrorKind::FooError))) |
270 | 293 | //! } |
|
278 | 301 | //! |
279 | 302 | //! To extend the error chain: |
280 | 303 | //! |
281 | | -//! ```ignore |
| 304 | +//! ``` |
| 305 | +//! # #[macro_use] extern crate error_chain; |
| 306 | +//! # fn main() {} |
| 307 | +//! # error_chain! {} |
| 308 | +//! # fn do_something() -> Result<()> { unimplemented!() } |
| 309 | +//! # fn test() -> Result<()> { |
282 | 310 | //! use error_chain::ResultExt; |
283 | | -//! try!(do_something().chain_err(|| "something went wrong")); |
| 311 | +//! let res: Result<()> = do_something().chain_err(|| "something went wrong"); |
| 312 | +//! # Ok(()) |
| 313 | +//! # } |
284 | 314 | //! ``` |
285 | 315 | //! |
286 | 316 | //! `chain_err` can be called on any `Result` type where the contained |
|
0 commit comments