diff options
Diffstat (limited to 'third_party/rust/error-chain/examples')
-rw-r--r-- | third_party/rust/error-chain/examples/all.rs | 36 | ||||
-rw-r--r-- | third_party/rust/error-chain/examples/chain_err.rs | 67 | ||||
-rw-r--r-- | third_party/rust/error-chain/examples/doc.rs | 28 | ||||
-rw-r--r-- | third_party/rust/error-chain/examples/has_backtrace.rs | 18 | ||||
-rw-r--r-- | third_party/rust/error-chain/examples/quickstart.rs | 78 | ||||
-rw-r--r-- | third_party/rust/error-chain/examples/size.rs | 30 |
6 files changed, 257 insertions, 0 deletions
diff --git a/third_party/rust/error-chain/examples/all.rs b/third_party/rust/error-chain/examples/all.rs new file mode 100644 index 0000000000..840ca95e27 --- /dev/null +++ b/third_party/rust/error-chain/examples/all.rs @@ -0,0 +1,36 @@ +#[macro_use] +extern crate error_chain; + +pub mod inner { + error_chain! {} +} + +#[cfg(feature = "a_feature")] +pub mod feature { + error_chain! {} +} + +error_chain! { + // Types generated by the macro. If empty or absent, it defaults to + // Error, ErrorKind, Result; + types { + // With custom names: + MyError, MyErrorKind, MyResult; + // Without the `Result` wrapper: + // Error, ErrorKind; + } + + // Automatic bindings to other error types generated by `error_chain!`. + links { + Inner(inner::Error, inner::ErrorKind); + // Attributes can be added at the end of the declaration. + Feature(feature::Error, feature::ErrorKind) #[cfg(feature = "a_feature")]; + } + + // Bindings to types implementing std::error::Error. + foreign_links { + Io(::std::io::Error); + } +} + +fn main() {} diff --git a/third_party/rust/error-chain/examples/chain_err.rs b/third_party/rust/error-chain/examples/chain_err.rs new file mode 100644 index 0000000000..c70a25dd60 --- /dev/null +++ b/third_party/rust/error-chain/examples/chain_err.rs @@ -0,0 +1,67 @@ +//! Demonstrates usage of `Error::caused` method. This method enables chaining errors +//! like `ResultExt::chain_err` but doesn't require the presence of a `Result` wrapper. + +#[macro_use] +extern crate error_chain; + +use std::fs::File; + +mod errors { + use super::LaunchStage; + use std::io; + + error_chain! { + foreign_links { + Io(io::Error) #[doc = "Error during IO"]; + } + + errors { + Launch(phase: LaunchStage) { + description("An error occurred during startup") + display("Startup aborted: {:?} did not complete successfully", phase) + } + + ConfigLoad(path: String) { + description("Config file not found") + display("Unable to read file `{}`", path) + } + } + } + + impl From<LaunchStage> for ErrorKind { + fn from(v: LaunchStage) -> Self { + ErrorKind::Launch(v) + } + } +} + +pub use errors::*; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum LaunchStage { + ConfigLoad, + ConfigParse, + ConfigResolve, +} + +/// Read the service config from the file specified. +fn load_config(rel_path: &str) -> Result<()> { + File::open(rel_path) + .map(|_| ()) + .chain_err(|| ErrorKind::ConfigLoad(rel_path.to_string())) +} + +/// Launch the service. +fn launch(rel_path: &str) -> Result<()> { + load_config(rel_path).map_err(|e| match e { + e @ Error(ErrorKind::ConfigLoad(_), _) => e.chain_err(|| LaunchStage::ConfigLoad), + e => e.chain_err(|| "Unknown failure"), + }) +} + +fn main() { + let chain = launch("does_not_exist.json").unwrap_err(); + for err in chain.iter() { + println!("{}", err); + } +} diff --git a/third_party/rust/error-chain/examples/doc.rs b/third_party/rust/error-chain/examples/doc.rs new file mode 100644 index 0000000000..68f57d7884 --- /dev/null +++ b/third_party/rust/error-chain/examples/doc.rs @@ -0,0 +1,28 @@ +#![deny(missing_docs)] + +//! This module is used to check that all generated items are documented. + +#[macro_use] +extern crate error_chain; + +/// Inner module. +pub mod inner { + error_chain! {} +} + +error_chain! { + links { + Inner(inner::Error, inner::ErrorKind) #[doc = "Doc"]; + } + foreign_links { + Io(::std::io::Error) #[doc = "Io"]; + } + errors { + /// Doc + Test2 { + + } + } +} + +fn main() {} diff --git a/third_party/rust/error-chain/examples/has_backtrace.rs b/third_party/rust/error-chain/examples/has_backtrace.rs new file mode 100644 index 0000000000..c5dac058a8 --- /dev/null +++ b/third_party/rust/error-chain/examples/has_backtrace.rs @@ -0,0 +1,18 @@ +//! Exits with exit code 0 if backtraces are disabled and 1 if they are enabled. +//! Used by tests to make sure backtraces are available when they should be. Should not be used +//! outside of the tests. + +#[macro_use] +extern crate error_chain; + +error_chain! { + errors { + MyError + } +} + +fn main() { + let err = Error::from(ErrorKind::MyError); + let has_backtrace = err.backtrace().is_some(); + ::std::process::exit(has_backtrace as i32); +} diff --git a/third_party/rust/error-chain/examples/quickstart.rs b/third_party/rust/error-chain/examples/quickstart.rs new file mode 100644 index 0000000000..e410a62b8a --- /dev/null +++ b/third_party/rust/error-chain/examples/quickstart.rs @@ -0,0 +1,78 @@ +// Simple and robust error handling with error-chain! +// Use this as a template for new projects. + +// `error_chain!` can recurse deeply +#![recursion_limit = "1024"] + +// Import the macro. Don't forget to add `error-chain` in your +// `Cargo.toml`! +#[macro_use] +extern crate error_chain; + +// We'll put our errors in an `errors` module, and other modules in +// this crate will `use errors::*;` to get access to everything +// `error_chain!` creates. +mod errors { + // Create the Error, ErrorKind, ResultExt, and Result types + error_chain! {} +} + +// This only gives access within this module. Make this `pub use errors::*;` +// instead if the types must be accessible from other modules (e.g., within +// a `links` section). +use errors::*; + +fn main() { + if let Err(ref e) = run() { + use std::io::Write; + let stderr = &mut ::std::io::stderr(); + let errmsg = "Error writing to stderr"; + + writeln!(stderr, "error: {}", e).expect(errmsg); + + for e in e.iter().skip(1) { + writeln!(stderr, "caused by: {}", e).expect(errmsg); + } + + // The backtrace is not always generated. Try to run this example + // with `RUST_BACKTRACE=1`. + if let Some(backtrace) = e.backtrace() { + writeln!(stderr, "backtrace: {:?}", backtrace).expect(errmsg); + } + + ::std::process::exit(1); + } +} + +// The above main gives you maximum control over how the error is +// formatted. If you don't care (i.e. you want to display the full +// error during an assert) you can just call the `display_chain` method +// on the error object +#[allow(dead_code)] +fn alternative_main() { + if let Err(ref e) = run() { + use error_chain::ChainedError; + use std::io::Write; // trait which holds `display_chain` + let stderr = &mut ::std::io::stderr(); + let errmsg = "Error writing to stderr"; + + writeln!(stderr, "{}", e.display_chain()).expect(errmsg); + ::std::process::exit(1); + } +} + +// Use this macro to auto-generate the main above. You may want to +// set the `RUST_BACKTRACE` env variable to see a backtrace. +// quick_main!(run); + +// Most functions will return the `Result` type, imported from the +// `errors` module. It is a typedef of the standard `Result` type +// for which the error type is always our own `Error`. +fn run() -> Result<()> { + use std::fs::File; + + // This operation will fail + File::open("tretrete").chain_err(|| "unable to open tretrete file")?; + + Ok(()) +} diff --git a/third_party/rust/error-chain/examples/size.rs b/third_party/rust/error-chain/examples/size.rs new file mode 100644 index 0000000000..3e180684e8 --- /dev/null +++ b/third_party/rust/error-chain/examples/size.rs @@ -0,0 +1,30 @@ +#[macro_use] +extern crate error_chain; + +use std::mem::{size_of, size_of_val}; + +error_chain! { + errors { + AVariant + Another + } +} + +fn main() { + println!("Memory usage in bytes"); + println!("---------------------"); + println!("Result<()>: {}", size_of::<Result<()>>()); + println!(" (): {}", size_of::<()>()); + println!(" Error: {}", size_of::<Error>()); + println!(" ErrorKind: {}", size_of::<ErrorKind>()); + let msg = ErrorKind::Msg("test".into()); + println!(" ErrorKind::Msg: {}", size_of_val(&msg)); + println!(" String: {}", size_of::<String>()); + println!(" State: {}", size_of::<error_chain::State>()); + let state = error_chain::State { + next_error: None, + backtrace: error_chain::InternalBacktrace::new(), + }; + println!(" State.next_error: {}", size_of_val(&state.next_error)); + println!(" State.backtrace: {}", size_of_val(&state.backtrace)); +} |