1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
//! This library provides [`anyhow::Error`][Error], a trait object based error //! type for easy idiomatic error handling in Rust applications. //! //! <br> //! //! # Details //! //! - Use `Result<T, anyhow::Error>`, or equivalently `anyhow::Result<T>`, as //! the return type of any fallible function. //! //! Within the function, use `?` to easily propagate any error that implements //! the `std::error::Error` trait. //! //! ``` //! # pub trait Deserialize {} //! # //! # mod serde_json { //! # use super::Deserialize; //! # use std::io; //! # //! # pub fn from_str<T: Deserialize>(json: &str) -> io::Result<T> { //! # unimplemented!() //! # } //! # } //! # //! # struct ClusterMap; //! # //! # impl Deserialize for ClusterMap {} //! # //! use anyhow::Result; //! //! fn get_cluster_info() -> Result<ClusterMap> { //! let config = std::fs::read_to_string("cluster.json")?; //! let map: ClusterMap = serde_json::from_str(&config)?; //! Ok(map) //! } //! # //! # fn main() {} //! ``` //! //! - Attach context to help the person troubleshooting the error understand //! where things went wrong. A low-level error like "No such file or //! directory" can be annoying to debug without more context about what higher //! level step the application was in the middle of. //! //! ``` //! # struct It; //! # //! # impl It { //! # fn detach(&self) -> Result<()> { //! # unimplemented!() //! # } //! # } //! # //! use anyhow::{Context, Result}; //! //! fn main() -> Result<()> { //! # return Ok(()); //! # //! # const _: &str = stringify! { //! ... //! # }; //! # //! # let it = It; //! # let path = "./path/to/instrs.jsox"; //! # //! it.detach().context("failed to detach the important thing")?; //! //! let content = std::fs::read(path) //! .with_context(|| format!("failed to read instrs from {}", path))?; //! # //! # const _: &str = stringify! { //! ... //! # }; //! # //! # Ok(()) //! } //! ``` //! //! ```console //! Error: failed to read instrs from ./path/to/instrs.jsox //! //! Caused by: //! No such file or directory (os error 2) //! ``` //! //! - Downcasting is supported and can be by value, by shared reference, or by //! mutable reference as needed. //! //! ``` //! # use anyhow::anyhow; //! # use std::fmt::{self, Display}; //! # use std::task::Poll; //! # //! # #[derive(Debug)] //! # enum DataStoreError { //! # Censored(()), //! # } //! # //! # impl Display for DataStoreError { //! # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { //! # unimplemented!() //! # } //! # } //! # //! # impl std::error::Error for DataStoreError {} //! # //! # const REDACTED_CONTENT: () = (); //! # //! # let error = anyhow!("..."); //! # let root_cause = &error; //! # //! # let ret = //! // If the error was caused by redaction, then return a //! // tombstone instead of the content. //! match root_cause.downcast_ref::<DataStoreError>() { //! Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)), //! None => Err(error), //! } //! # ; //! ``` //! //! - A backtrace is captured and printed with the error if the underlying error //! type does not already provide its own. In order to see backtraces, the //! `RUST_LIB_BACKTRACE=1` environment variable must be defined. //! //! - Anyhow works with any error type that has an impl of `std::error::Error`, //! including ones defined in your crate. We do not bundle a `derive(Error)` //! macro but you can write the impls yourself or use a standalone macro like //! [thiserror]. //! //! [thiserror]: https://github.com/dtolnay/thiserror //! //! ``` //! use thiserror::Error; //! //! #[derive(Error, Debug)] //! pub enum FormatError { //! #[error("invalid header (expected {expected:?}, got {found:?})")] //! InvalidHeader { //! expected: String, //! found: String, //! }, //! #[error("missing attribute: {0}")] //! MissingAttribute(String), //! } //! ``` //! //! - One-off error messages can be constructed using the `anyhow!` macro, which //! supports string interpolation and produces an `anyhow::Error`. //! //! ``` //! # use anyhow::{anyhow, Result}; //! # //! # fn demo() -> Result<()> { //! # let missing = "..."; //! return Err(anyhow!("missing attribute: {}", missing)); //! # Ok(()) //! # } //! ``` //! //! <br> //! //! # Acknowledgements //! //! The implementation of the `anyhow::Error` type is originally forked from //! `fehler::Exception` ([https://github.com/withoutboats/fehler][fehler]). This //! library exposes it under the more standard `Error` / `Result` terminology //! rather than the `throw!` / `#[throws]` / `Exception` language of exceptions. //! //! [fehler]: https://github.com/withoutboats/fehler #![doc(html_root_url = "https://docs.rs/anyhow/1.0.15")] #![cfg_attr(backtrace, feature(backtrace))] #![allow(clippy::new_ret_no_self, clippy::wrong_self_convention)] #[macro_use] mod backtrace; mod context; mod error; mod kind; #[cfg(not(feature = "std"))] compile_error!("no_std support is not implemented yet"); pub use crate::context::Context; pub use crate::error::{Chain, Error}; /// `Result<T, Error>` /// /// This is a reasonable return type to use throughout your application but also /// for `fn main`; if you do, failures will be printed along with any /// [context][Context] and a backtrace if one was captured. /// /// `anyhow::Result` may be used with one *or* two type parameters. /// /// ```rust /// use anyhow::Result; /// /// # const IGNORE: &str = stringify! { /// fn demo1() -> Result<T> {...} /// // ^ equivalent to std::result::Result<T, anyhow::Error> /// /// fn demo2() -> Result<T, OtherError> {...} /// // ^ equivalent to std::result::Result<T, OtherError> /// # }; /// ``` /// /// # Example /// /// ``` /// # pub trait Deserialize {} /// # /// # mod serde_json { /// # use super::Deserialize; /// # use std::io; /// # /// # pub fn from_str<T: Deserialize>(json: &str) -> io::Result<T> { /// # unimplemented!() /// # } /// # } /// # /// # #[derive(Debug)] /// # struct ClusterMap; /// # /// # impl Deserialize for ClusterMap {} /// # /// use anyhow::Result; /// /// fn main() -> Result<()> { /// # return Ok(()); /// let config = std::fs::read_to_string("cluster.json")?; /// let map: ClusterMap = serde_json::from_str(&config)?; /// println!("cluster info: {:#?}", map); /// Ok(()) /// } /// ``` pub type Result<T, E = Error> = std::result::Result<T, E>; /// Return early with an error. /// /// This macro is equivalent to `return Err(From::from($err))`. /// /// # Example /// /// ``` /// # use anyhow::{bail, Result}; /// # /// # fn has_permission(user: usize, resource: usize) -> bool { /// # true /// # } /// # /// # fn main() -> Result<()> { /// # let user = 0; /// # let resource = 0; /// # /// if !has_permission(user, resource) { /// bail!("permission denied for accessing {}", resource); /// } /// # Ok(()) /// # } /// ``` /// /// ``` /// # use anyhow::{bail, Result}; /// # use std::fmt::{self, Display}; /// # /// # #[derive(Debug)] /// # enum ScienceError { /// # RecursionLimitExceeded, /// # } /// # /// # impl std::error::Error for ScienceError {} /// # /// # impl Display for ScienceError { /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// # unimplemented!() /// # } /// # } /// # /// # const MAX_DEPTH: usize = 1; /// # /// # const IGNORE: &str = stringify! { /// #[derive(Error, Debug)] /// enum ScienceError { /// #[error(display = "recursion limit exceeded")] /// RecursionLimitExceeded, /// ... /// } /// # }; /// /// # fn main() -> Result<()> { /// # let depth = 0; /// # /// if depth > MAX_DEPTH { /// bail!(ScienceError::RecursionLimitExceeded); /// } /// # Ok(()) /// # } /// ``` #[macro_export] macro_rules! bail { ($msg:literal $(,)?) => { return std::result::Result::Err($crate::anyhow!($msg)); }; ($err:expr $(,)?) => { return std::result::Result::Err($crate::anyhow!($err)); }; ($fmt:expr, $($arg:tt)*) => { return std::result::Result::Err($crate::anyhow!($fmt, $($arg)*)); }; } /// Return early with an error if a condition is not satisfied. /// /// This macro is equivalent to `if !$cond { return Err(From::from($err)); }`. /// /// Analogously to `assert!`, `ensure!` takes a condition and exits the function /// if the condition fails. Unlike `assert!`, `ensure!` returns an `Error` /// rather than panicking. /// /// # Example /// /// ``` /// # use anyhow::{ensure, Result}; /// # /// # fn main() -> Result<()> { /// # let user = 0; /// # /// ensure!(user == 0, "only user 0 is allowed"); /// # Ok(()) /// # } /// ``` /// /// ``` /// # use anyhow::{ensure, Result}; /// # use std::fmt::{self, Display}; /// # /// # #[derive(Debug)] /// # enum ScienceError { /// # RecursionLimitExceeded, /// # } /// # /// # impl std::error::Error for ScienceError {} /// # /// # impl Display for ScienceError { /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { /// # unimplemented!() /// # } /// # } /// # /// # const MAX_DEPTH: usize = 1; /// # /// # const IGNORE: &str = stringify! { /// #[derive(Error, Debug)] /// enum ScienceError { /// #[error(display = "recursion limit exceeded")] /// RecursionLimitExceeded, /// ... /// } /// # }; /// /// # fn main() -> Result<()> { /// # let depth = 0; /// # /// ensure!(depth <= MAX_DEPTH, ScienceError::RecursionLimitExceeded); /// # Ok(()) /// # } /// ``` #[macro_export] macro_rules! ensure { ($cond:expr, $msg:literal $(,)?) => { if !$cond { return std::result::Result::Err($crate::anyhow!($msg)); } }; ($cond:expr, $err:expr $(,)?) => { if !$cond { return std::result::Result::Err($crate::anyhow!($err)); } }; ($cond:expr, $fmt:expr, $($arg:tt)*) => { if !$cond { return std::result::Result::Err($crate::anyhow!($fmt, $($arg)*)); } }; } /// Construct an ad-hoc error from a string. /// /// This evaluates to an `Error`. It can take either just a string, or a format /// string with arguments. It also can take any custom type which implements /// `Debug` and `Display`. /// /// # Example /// /// ``` /// # type V = (); /// # /// use anyhow::{anyhow, Result}; /// /// fn lookup(key: &str) -> Result<V> { /// if key.len() != 16 { /// return Err(anyhow!("key length must be 16 characters, got {:?}", key)); /// } /// /// // ... /// # Ok(()) /// } /// ``` #[macro_export] macro_rules! anyhow { ($msg:literal $(,)?) => { // Handle $:literal as a special case to make cargo-expanded code more // concise in the common case. $crate::private::new_adhoc($msg) }; ($err:expr $(,)?) => ({ #[allow(unused_imports)] use $crate::private::{AdhocKind, TraitKind}; let error = $err; (&error).anyhow_kind().new(error) }); ($fmt:expr, $($arg:tt)*) => { $crate::private::new_adhoc(format!($fmt, $($arg)*)) }; } // Not public API. #[doc(hidden)] pub mod private { use crate::Error; use std::fmt::{Debug, Display}; #[cfg(backtrace)] use std::backtrace::Backtrace; pub use crate::kind::{AdhocKind, TraitKind}; pub fn new_adhoc<M>(message: M) -> Error where M: Display + Debug + Send + Sync + 'static, { Error::from_adhoc(message, backtrace!()) } }