Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit bac8d49

Browse files
AndyGaugeYamakaky
authored andcommitted
example for Backtrace::frames (#211)
* example for Backtrace::frames * added conditional compilation to filter out main when backtrace feature disabled
1 parent 527bea6 commit bac8d49

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,47 @@
461461
//!
462462
//! Backtrace generation can be disabled by turning off the `backtrace` feature.
463463
//!
464+
//! The Backtrace contains a Vec of [`BacktraceFrame`]s that can be operated
465+
//! on directly. For example, to only see the files and line numbers of code
466+
//! within your own project.
467+
//!
468+
//! ```
469+
//! # #[macro_use]
470+
//! # extern crate error_chain;
471+
//! # mod errors {
472+
//! # error_chain! {
473+
//! # foreign_links {
474+
//! # Io(::std::io::Error);
475+
//! # }
476+
//! # }
477+
//! # }
478+
//! # use errors::*;
479+
//! # #[cfg(feature="backtrace")]
480+
//! # fn main() {
481+
//! if let Err(ref e) = open_file() {
482+
//! if let Some(backtrace) = e.backtrace() {
483+
//! let frames = backtrace.frames();
484+
//! for frame in frames.iter() {
485+
//! for symbol in frame.symbols().iter() {
486+
//! if let (Some(file), Some(lineno)) = (symbol.filename(), symbol.lineno()) {
487+
//! if file.display().to_string()[0..3] == "src".to_string(){
488+
//! println!("{}:{}", file.display().to_string(), lineno);
489+
//! }
490+
//! }
491+
//! }
492+
//! }
493+
//! }
494+
//! };
495+
//! # }
496+
//! # #[cfg(not(feature="backtrace"))]
497+
//! # fn main() { }
498+
//!
499+
//! fn open_file() -> Result<()> {
500+
//! std::fs::File::open("does_not_exist")?;
501+
//! Ok(())
502+
//! }
503+
//! ```
504+
//!
464505
//! ## Iteration
465506
//!
466507
//! The [`iter`] method returns an iterator over the chain of error boxes.
@@ -493,6 +534,8 @@
493534
//! [`std::fmt::Error`]: https://doc.rust-lang.org/std/fmt/struct.Error.html
494535
//! [`.into()`]: https://doc.rust-lang.org/std/convert/trait.Into.html#tymethod.into
495536
//! [`map_err`]: https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err
537+
//! [`BacktraceFrame`]: https://docs.rs/backtrace/0.3.2/backtrace/struct.BacktraceFrame.html
538+
496539

497540
#[cfg(feature = "backtrace")]
498541
extern crate backtrace;

0 commit comments

Comments
 (0)