chainerror/searchindex.json

1 line
62 KiB
JSON
Raw Normal View History

{"doc_urls":["tutorial1.html#simple-string-errors","tutorial2.html#simple-chained-string-errors"],"index":{"documentStore":{"docInfo":{"0":{"body":57,"breadcrumbs":3,"title":3},"1":{"body":556,"breadcrumbs":4,"title":4}},"docs":{"0":{"body":"The most simplest of doing error handling in rust is by returning String as a Box<Error> . As you can see by running the example, this only prints out the last Error . If the rust main function returns an Err(), this Err() will be displayed with std::fmt::Debug . use std::error::Error;\nuse std::result::Result; fn do_some_io() -> Result<(), Box<Error>> { Err(\"do_some_io error\")?; Ok(())\n} fn func2() -> Result<(), Box<Error>> { if let Err(_) = do_some_io() { Err(\"func2 error\")?; } Ok(())\n} fn func1() -> Result<(), Box<Error>> { if let Err(_) = func2() { Err(\"func1 error\")?; } Ok(())\n} fn main() -> Result<(), Box<Error>> { func1()\n}","breadcrumbs":"Simple String Errors","id":"0","title":"Simple String Errors"},"1":{"body":"Now with the help of the chainerror crate, we can have a nicer output. Press the play button in the upper right corner and see the nice debug output. use crate::chainerror::*;\nuse std::error::Error;\nuse std::result::Result; fn do_some_io() -> Result<(), Box<Error>> { Err(\"do_some_io error\")?; Ok(())\n} fn func2() -> Result<(), Box<Error>> { if let Err(e) = do_some_io() { Err(cherr!(e, \"func2 error\"))?; } Ok(())\n} fn func1() -> Result<(), Box<Error>> { if let Err(e) = func2() { Err(cherr!(e, \"func1 error\"))?; } Ok(())\n} fn main() -> Result<(), Box<Error>> { func1()\n}\n# #[allow(dead_code)]\n# mod chainerror {\n# use std::error::Error;\n# use std::fmt::{Debug, Display, Formatter, Result};\n# use std::result::Result as StdResult;\n# # pub struct ChainError<T> {\n# #[cfg(not(feature = \"no-fileline\"))]\n# occurrence: Option<(u32, &'static str)>,\n# kind: T,\n# error_cause: Option<Box<dyn Error + 'static>>,\n# }\n# # pub type ChainResult<O, E> = StdResult<O, ChainError<E>>;\n# # impl<T: 'static + Display + Debug> ChainError<T> {\n# #[cfg(not(feature = \"no-fileline\"))]\n# pub fn new(\n# kind: T,\n# error_cause: Option<Box<dyn Error + 'static>>,\n# occurrence: Option<(u32, &'static str)>,\n# ) -> Self {\n# Self {\n# occurrence,\n# kind,\n# error_cause,\n# }\n# }\n# # #[cfg(feature = \"no-fileline\")]\n# pub fn new(\n# kind: T,\n# error_cause: Option<Box<dyn Error + 'static>>,\n# _occurrence: Option<(u32, &'static str)>,\n# ) -> Self {\n# Self { kind, error_cause }\n# }\n# # pub fn root_cause(&self) -> Option<&(dyn Error + 'static)> {\n# let mut cause = self as &(dyn Error + 'static);\n# while let Some(c) = cause.source() {\n# cause = c;\n# }\n# Some(cause)\n# }\n# # pub fn find_cause<U: Error + 'static>(&self) -> Option<&U> {\n# let mut cause = self as &(dyn Error + 'static);\n# loop {\n# if cause.is::<U>() {\n# return cause.downcast_ref::<U>();\n# }\n# # match cause.source() {\n# Some(c) => cause = c,\n# None => return None,\n# }\n# }\n# }\n# # pub fn find_chain_cause<U: Error + 'static>(&self) -> Option<&ChainError<U>> {\n# let mut cause = self as &(dyn Error + 'static);\n# loop {\n# if cause.is::<ChainError<U>>() {\n# return cause.downcast_ref::<ChainError<U>>();\n# }\n# # match cause.source() {\n# Some(c) => cause = c,\n# None => return None,\n# }\n# }\n# }\n# # pub fn kind<'a>(&'a self) -> &'a T {\n# &self.kind\n# }\n# }\n# # pub trait ChainErrorDown {\n# fn is_chain<T: 'static + Display + Debug>(&self) -> bool;\n# fn downcast_chain_ref<T: 'static + Display + Debug>(&self) -> Option<&ChainError<T>>;\n# fn downcast_chain_mut<T: 'static + Display + Debug>(&mut self) -> Option<&mut ChainError<T>>;\n# }\n# # use std::any::TypeId;\n# # impl<U: 'static + Display + Debug> ChainErrorDown for ChainError<U> {\n# fn is_chain<T: 'static + Display + Debug>(&self) -> bool {\n# TypeId::of::<T>() == TypeId::of::<U>()\n# }\n# # fn downcast_chain_ref<T: 'static + Display + Debug>(&self) -> Option<&ChainError<T>> {\n# if self.is_chain::<T>() {\n# unsafe { Some(&*(self as *const dyn Error as *const &ChainError<T>)) }\n# } else {\n# None\n# }\n# }\n# # fn downcast_