mirror of
https://github.com/haraldh/chainerror.git
synced 2025-05-29 21:18:07 +02:00
lots of documentation
This commit is contained in:
parent
5145b950b5
commit
8ad6eaceac
24 changed files with 892 additions and 89 deletions
71
examples/example.rs
Normal file
71
examples/example.rs
Normal file
|
@ -0,0 +1,71 @@
|
|||
use chainerror::*;
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::result::Result;
|
||||
|
||||
fn do_some_io() -> Result<(), Box<Error>> {
|
||||
Err(io::Error::from(io::ErrorKind::NotFound))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn func3() -> Result<(), Box<Error>> {
|
||||
let filename = "foo.txt";
|
||||
do_some_io().map_err(mstrerr!("Error reading '{}'", filename))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
derive_str_cherr!(Func2Error);
|
||||
|
||||
fn func2() -> ChainResult<(), Func2Error> {
|
||||
func3().map_err(mstrerr!(Func2Error, "func2 error: calling func3"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
enum Func1Error {
|
||||
Func2,
|
||||
IO(String),
|
||||
}
|
||||
|
||||
impl ::std::fmt::Display for Func1Error {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
match self {
|
||||
Func1Error::Func2 => write!(f, "func1 error calling func2"),
|
||||
Func1Error::IO(filename) => write!(f, "Error reading '{}'", filename),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for Func1Error {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
write!(f, "{}", self)
|
||||
}
|
||||
}
|
||||
|
||||
fn func1() -> ChainResult<(), Func1Error> {
|
||||
func2().map_err(|e| cherr!(e, Func1Error::Func2))?;
|
||||
let filename = String::from("bar.txt");
|
||||
do_some_io().map_err(|e| cherr!(e, Func1Error::IO(filename)))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if let Err(e) = func1() {
|
||||
match e.kind() {
|
||||
Func1Error::Func2 => eprintln!("Main Error Report: func1 error calling func2"),
|
||||
Func1Error::IO(filename) => {
|
||||
eprintln!("Main Error Report: func1 error reading '{}'", filename)
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(e) = e.find_chain_cause::<Func2Error>() {
|
||||
eprintln!("\nError reported by Func2Error: {}", e)
|
||||
}
|
||||
|
||||
if let Some(e) = e.root_cause() {
|
||||
let ioerror = e.downcast_ref::<io::Error>().unwrap();
|
||||
eprintln!("\nThe root cause was: std::io::Error: {:#?}", ioerror);
|
||||
}
|
||||
|
||||
eprintln!("\nDebug Error:\n{:?}", e);
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::result::Result;
|
||||
|
||||
fn do_some_io() -> Result<(), Box<Error>> {
|
||||
Err("do_some_io error")?;
|
||||
Err(io::Error::from(io::ErrorKind::NotFound))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -17,38 +17,38 @@ fn func2() -> Result<(), Box<Error>> {
|
|||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Func1Error {
|
||||
enum Func1ErrorKind {
|
||||
Func2,
|
||||
IO(String),
|
||||
}
|
||||
|
||||
impl ::std::fmt::Display for Func1Error {
|
||||
impl ::std::fmt::Display for Func1ErrorKind {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
match self {
|
||||
Func1Error::Func2 => write!(f, "func1 error calling func2"),
|
||||
Func1Error::IO(filename) => write!(f, "Error reading '{}'", filename),
|
||||
Func1ErrorKind::Func2 => write!(f, "func1 error calling func2"),
|
||||
Func1ErrorKind::IO(filename) => write!(f, "Error reading '{}'", filename),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn func1() -> ChainResult<(), Func1Error> {
|
||||
func2().map_err(|e| cherr!(e, Func1Error::Func2))?;
|
||||
fn func1() -> ChainResult<(), Func1ErrorKind> {
|
||||
func2().map_err(|e| cherr!(e, Func1ErrorKind::Func2))?;
|
||||
let filename = String::from("bar.txt");
|
||||
do_some_io().map_err(|e| cherr!(e, Func1Error::IO(filename)))?;
|
||||
do_some_io().map_err(|e| cherr!(e, Func1ErrorKind::IO(filename)))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<Error>> {
|
||||
if let Err(e) = func1() {
|
||||
match e.kind() {
|
||||
Func1Error::Func2 => eprintln!("Main Error Report: func1 error calling func2"),
|
||||
Func1Error::IO(filename) => {
|
||||
Func1ErrorKind::Func2 => eprintln!("Main Error Report: func1 error calling func2"),
|
||||
Func1ErrorKind::IO(filename) => {
|
||||
eprintln!("Main Error Report: func1 error reading '{}'", filename)
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(e) = e.find_chain_cause::<Func2Error>() {
|
||||
eprintln!("Error reported by Func2Error: {}", e)
|
||||
eprintln!("\nError reported by Func2Error: {}", e)
|
||||
}
|
||||
|
||||
eprintln!("\nDebug Error:\n{:?}", e);
|
||||
|
|
|
@ -16,44 +16,44 @@ fn func2() -> Result<(), Box<Error>> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
enum Func1Error {
|
||||
enum Func1ErrorKind {
|
||||
Func2,
|
||||
IO(String),
|
||||
}
|
||||
|
||||
impl ::std::fmt::Display for Func1Error {
|
||||
impl ::std::fmt::Display for Func1ErrorKind {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
match self {
|
||||
Func1Error::Func2 => write!(f, "func1 error calling func2"),
|
||||
Func1Error::IO(filename) => write!(f, "Error reading '{}'", filename),
|
||||
Func1ErrorKind::Func2 => write!(f, "func1 error calling func2"),
|
||||
Func1ErrorKind::IO(filename) => write!(f, "Error reading '{}'", filename),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for Func1Error {
|
||||
impl ::std::fmt::Debug for Func1ErrorKind {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
write!(f, "{}", self)
|
||||
}
|
||||
}
|
||||
|
||||
fn func1() -> ChainResult<(), Func1Error> {
|
||||
func2().map_err(|e| cherr!(e, Func1Error::Func2))?;
|
||||
fn func1() -> ChainResult<(), Func1ErrorKind> {
|
||||
func2().map_err(|e| cherr!(e, Func1ErrorKind::Func2))?;
|
||||
let filename = String::from("bar.txt");
|
||||
do_some_io().map_err(|e| cherr!(e, Func1Error::IO(filename)))?;
|
||||
do_some_io().map_err(|e| cherr!(e, Func1ErrorKind::IO(filename)))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<Error>> {
|
||||
if let Err(e) = func1() {
|
||||
match e.kind() {
|
||||
Func1Error::Func2 => eprintln!("Main Error Report: func1 error calling func2"),
|
||||
Func1Error::IO(filename) => {
|
||||
Func1ErrorKind::Func2 => eprintln!("Main Error Report: func1 error calling func2"),
|
||||
Func1ErrorKind::IO(filename) => {
|
||||
eprintln!("Main Error Report: func1 error reading '{}'", filename)
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(e) = e.find_chain_cause::<Func2Error>() {
|
||||
eprintln!("Error reported by Func2Error: {}", e)
|
||||
eprintln!("\nError reported by Func2Error: {}", e)
|
||||
}
|
||||
|
||||
eprintln!("\nDebug Error:\n{:?}", e);
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
use chainerror::*;
|
||||
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::result::Result;
|
||||
|
||||
fn do_some_io() -> Result<(), Box<Error>> {
|
||||
Err("do_some_io error")?;
|
||||
Err(io::Error::from(io::ErrorKind::NotFound))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
use chainerror::*;
|
||||
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::result::Result;
|
||||
|
||||
fn do_some_io() -> Result<(), Box<Error>> {
|
||||
Err("do_some_io error")?;
|
||||
Err(io::Error::from(io::ErrorKind::NotFound))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
use chainerror::*;
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::result::Result;
|
||||
|
||||
fn do_some_io() -> Result<(), Box<Error>> {
|
||||
Err("do_some_io error")?;
|
||||
Err(io::Error::from(io::ErrorKind::NotFound))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ fn main() -> Result<(), Box<Error>> {
|
|||
eprintln!("Func1ErrorIO:\n{:?}", s);
|
||||
}
|
||||
|
||||
if let Some(s) = try_cherr_ref!(e, Func1ErrorFunc2) {
|
||||
if let Some(s) = e.downcast_chain_ref::<Func1ErrorFunc2>() {
|
||||
eprintln!("Func1ErrorFunc2:\n{:?}", s);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue