1 //! [![github]](https://github.com/dtolnay/anyhow) [![crates-io]](https://crates.io/crates/anyhow) [![docs-rs]](https://docs.rs/anyhow)
2 //!
3 //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
4 //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
5 //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
6 //!
7 //! <br>
8 //!
9 //! This library provides [`anyhow::Error`][Error], a trait object based error
10 //! type for easy idiomatic error handling in Rust applications.
11 //!
12 //! <br>
13 //!
14 //! # Details
15 //!
16 //! - Use `Result<T, anyhow::Error>`, or equivalently `anyhow::Result<T>`, as
17 //! the return type of any fallible function.
18 //!
19 //! Within the function, use `?` to easily propagate any error that implements
20 //! the `std::error::Error` trait.
21 //!
22 //! ```
23 //! # pub trait Deserialize {}
24 //! #
25 //! # mod serde_json {
26 //! # use super::Deserialize;
27 //! # use std::io;
28 //! #
29 //! # pub fn from_str<T: Deserialize>(json: &str) -> io::Result<T> {
30 //! # unimplemented!()
31 //! # }
32 //! # }
33 //! #
34 //! # struct ClusterMap;
35 //! #
36 //! # impl Deserialize for ClusterMap {}
37 //! #
38 //! use anyhow::Result;
39 //!
40 //! fn get_cluster_info() -> Result<ClusterMap> {
41 //! let config = std::fs::read_to_string("cluster.json")?;
42 //! let map: ClusterMap = serde_json::from_str(&config)?;
43 //! Ok(map)
44 //! }
45 //! #
46 //! # fn main() {}
47 //! ```
48 //!
49 //! - Attach context to help the person troubleshooting the error understand
50 //! where things went wrong. A low-level error like "No such file or
51 //! directory" can be annoying to debug without more context about what higher
52 //! level step the application was in the middle of.
53 //!
54 //! ```
55 //! # struct It;
56 //! #
57 //! # impl It {
58 //! # fn detach(&self) -> Result<()> {
59 //! # unimplemented!()
60 //! # }
61 //! # }
62 //! #
63 //! use anyhow::{Context, Result};
64 //!
65 //! fn main() -> Result<()> {
66 //! # return Ok(());
67 //! #
68 //! # const _: &str = stringify! {
69 //! ...
70 //! # };
71 //! #
72 //! # let it = It;
73 //! # let path = "./path/to/instrs.json";
74 //! #
75 //! it.detach().context("Failed to detach the important thing")?;
76 //!
77 //! let content = std::fs::read(path)
78 //! .with_context(|| format!("Failed to read instrs from {}", path))?;
79 //! #
80 //! # const _: &str = stringify! {
81 //! ...
82 //! # };
83 //! #
84 //! # Ok(())
85 //! }
86 //! ```
87 //!
88 //! ```console
89 //! Error: Failed to read instrs from ./path/to/instrs.json
90 //!
91 //! Caused by:
92 //! No such file or directory (os error 2)
93 //! ```
94 //!
95 //! - Downcasting is supported and can be by value, by shared reference, or by
96 //! mutable reference as needed.
97 //!
98 //! ```
99 //! # use anyhow::anyhow;
100 //! # use std::fmt::{self, Display};
101 //! # use std::task::Poll;
102 //! #
103 //! # #[derive(Debug)]
104 //! # enum DataStoreError {
105 //! # Censored(()),
106 //! # }
107 //! #
108 //! # impl Display for DataStoreError {
109 //! # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
110 //! # unimplemented!()
111 //! # }
112 //! # }
113 //! #
114 //! # impl std::error::Error for DataStoreError {}
115 //! #
116 //! # const REDACTED_CONTENT: () = ();
117 //! #
118 //! # let error = anyhow!("...");
119 //! # let root_cause = &error;
120 //! #
121 //! # let ret =
122 //! // If the error was caused by redaction, then return a
123 //! // tombstone instead of the content.
124 //! match root_cause.downcast_ref::<DataStoreError>() {
125 //! Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),
126 //! None => Err(error),
127 //! }
128 //! # ;
129 //! ```
130 //!
131 //! - If using Rust ≥ 1.65, a backtrace is captured and printed with the
132 //! error if the underlying error type does not already provide its own. In
133 //! order to see backtraces, they must be enabled through the environment
134 //! variables described in [`std::backtrace`]:
135 //!
136 //! - If you want panics and errors to both have backtraces, set
137 //! `RUST_BACKTRACE=1`;
138 //! - If you want only errors to have backtraces, set `RUST_LIB_BACKTRACE=1`;
139 //! - If you want only panics to have backtraces, set `RUST_BACKTRACE=1` and
140 //! `RUST_LIB_BACKTRACE=0`.
141 //!
142 //! [`std::backtrace`]: https://doc.rust-lang.org/std/backtrace/index.html#environment-variables
143 //!
144 //! - Anyhow works with any error type that has an impl of `std::error::Error`,
145 //! including ones defined in your crate. We do not bundle a `derive(Error)`
146 //! macro but you can write the impls yourself or use a standalone macro like
147 //! [thiserror].
148 //!
149 //! [thiserror]: https://github.com/dtolnay/thiserror
150 //!
151 //! ```
152 //! use thiserror::Error;
153 //!
154 //! #[derive(Error, Debug)]
155 //! pub enum FormatError {
156 //! #[error("Invalid header (expected {expected:?}, got {found:?})")]
157 //! InvalidHeader {
158 //! expected: String,
159 //! found: String,
160 //! },
161 //! #[error("Missing attribute: {0}")]
162 //! MissingAttribute(String),
163 //! }
164 //! ```
165 //!
166 //! - One-off error messages can be constructed using the `anyhow!` macro, which
167 //! supports string interpolation and produces an `anyhow::Error`.
168 //!
169 //! ```
170 //! # use anyhow::{anyhow, Result};
171 //! #
172 //! # fn demo() -> Result<()> {
173 //! # let missing = "...";
174 //! return Err(anyhow!("Missing attribute: {}", missing));
175 //! # Ok(())
176 //! # }
177 //! ```
178 //!
179 //! A `bail!` macro is provided as a shorthand for the same early return.
180 //!
181 //! ```
182 //! # use anyhow::{bail, Result};
183 //! #
184 //! # fn demo() -> Result<()> {
185 //! # let missing = "...";
186 //! bail!("Missing attribute: {}", missing);
187 //! # Ok(())
188 //! # }
189 //! ```
190 //!
191 //! <br>
192 //!
193 //! # No-std support
194 //!
195 //! In no_std mode, the same API is almost all available and works the same way.
196 //! To depend on Anyhow in no_std mode, disable our default enabled "std"
197 //! feature in Cargo.toml. A global allocator is required.
198 //!
199 //! ```toml
200 //! [dependencies]
201 //! anyhow = { version = "1.0", default-features = false }
202 //! ```
203 //!
204 //! Since the `?`-based error conversions would normally rely on the
205 //! `std::error::Error` trait which is only available through std, no_std mode
206 //! will require an explicit `.map_err(Error::msg)` when working with a
207 //! non-Anyhow error type inside a function that returns Anyhow's error type.
208
209 #![doc(html_root_url = "https://docs.rs/anyhow/1.0.79")]
210 #![cfg_attr(error_generic_member_access, feature(error_generic_member_access))]
211 #![cfg_attr(doc_cfg, feature(doc_cfg))]
212 #![cfg_attr(not(feature = "std"), no_std)]
213 #![deny(dead_code, unused_imports, unused_mut)]
214 #![cfg_attr(
215 not(anyhow_no_unsafe_op_in_unsafe_fn_lint),
216 deny(unsafe_op_in_unsafe_fn)
217 )]
218 #![cfg_attr(anyhow_no_unsafe_op_in_unsafe_fn_lint, allow(unused_unsafe))]
219 #![allow(
220 clippy::doc_markdown,
221 clippy::enum_glob_use,
222 clippy::explicit_auto_deref,
223 clippy::extra_unused_type_parameters,
224 clippy::let_underscore_untyped,
225 clippy::missing_errors_doc,
226 clippy::missing_panics_doc,
227 clippy::module_name_repetitions,
228 clippy::must_use_candidate,
229 clippy::needless_doctest_main,
230 clippy::new_ret_no_self,
231 clippy::redundant_else,
232 clippy::return_self_not_must_use,
233 clippy::struct_field_names,
234 clippy::unused_self,
235 clippy::used_underscore_binding,
236 clippy::wildcard_imports,
237 clippy::wrong_self_convention
238 )]
239
240 #[cfg(all(
241 anyhow_nightly_testing,
242 feature = "std",
243 not(error_generic_member_access)
244 ))]
245 compile_error!("Build script probe failed to compile.");
246
247 extern crate alloc;
248
249 #[macro_use]
250 mod backtrace;
251 mod chain;
252 mod context;
253 mod ensure;
254 mod error;
255 mod fmt;
256 mod kind;
257 mod macros;
258 mod ptr;
259 mod wrapper;
260
261 use crate::error::ErrorImpl;
262 use crate::ptr::Own;
263 use core::fmt::Display;
264
265 #[cfg(not(feature = "std"))]
266 use core::fmt::Debug;
267
268 #[cfg(feature = "std")]
269 use std::error::Error as StdError;
270
271 #[cfg(not(feature = "std"))]
272 trait StdError: Debug + Display {
source(&self) -> Option<&(dyn StdError + 'static)>273 fn source(&self) -> Option<&(dyn StdError + 'static)> {
274 None
275 }
276 }
277
278 #[doc(no_inline)]
279 pub use anyhow as format_err;
280
281 /// The `Error` type, a wrapper around a dynamic error type.
282 ///
283 /// `Error` works a lot like `Box<dyn std::error::Error>`, but with these
284 /// differences:
285 ///
286 /// - `Error` requires that the error is `Send`, `Sync`, and `'static`.
287 /// - `Error` guarantees that a backtrace is available, even if the underlying
288 /// error type does not provide one.
289 /// - `Error` is represented as a narrow pointer — exactly one word in
290 /// size instead of two.
291 ///
292 /// <br>
293 ///
294 /// # Display representations
295 ///
296 /// When you print an error object using "{}" or to_string(), only the outermost
297 /// underlying error or context is printed, not any of the lower level causes.
298 /// This is exactly as if you had called the Display impl of the error from
299 /// which you constructed your anyhow::Error.
300 ///
301 /// ```console
302 /// Failed to read instrs from ./path/to/instrs.json
303 /// ```
304 ///
305 /// To print causes as well using anyhow's default formatting of causes, use the
306 /// alternate selector "{:#}".
307 ///
308 /// ```console
309 /// Failed to read instrs from ./path/to/instrs.json: No such file or directory (os error 2)
310 /// ```
311 ///
312 /// The Debug format "{:?}" includes your backtrace if one was captured. Note
313 /// that this is the representation you get by default if you return an error
314 /// from `fn main` instead of printing it explicitly yourself.
315 ///
316 /// ```console
317 /// Error: Failed to read instrs from ./path/to/instrs.json
318 ///
319 /// Caused by:
320 /// No such file or directory (os error 2)
321 /// ```
322 ///
323 /// and if there is a backtrace available:
324 ///
325 /// ```console
326 /// Error: Failed to read instrs from ./path/to/instrs.json
327 ///
328 /// Caused by:
329 /// No such file or directory (os error 2)
330 ///
331 /// Stack backtrace:
332 /// 0: <E as anyhow::context::ext::StdError>::ext_context
333 /// at /git/anyhow/src/backtrace.rs:26
334 /// 1: core::result::Result<T,E>::map_err
335 /// at /git/rustc/src/libcore/result.rs:596
336 /// 2: anyhow::context::<impl anyhow::Context<T,E> for core::result::Result<T,E>>::with_context
337 /// at /git/anyhow/src/context.rs:58
338 /// 3: testing::main
339 /// at src/main.rs:5
340 /// 4: std::rt::lang_start
341 /// at /git/rustc/src/libstd/rt.rs:61
342 /// 5: main
343 /// 6: __libc_start_main
344 /// 7: _start
345 /// ```
346 ///
347 /// To see a conventional struct-style Debug representation, use "{:#?}".
348 ///
349 /// ```console
350 /// Error {
351 /// context: "Failed to read instrs from ./path/to/instrs.json",
352 /// source: Os {
353 /// code: 2,
354 /// kind: NotFound,
355 /// message: "No such file or directory",
356 /// },
357 /// }
358 /// ```
359 ///
360 /// If none of the built-in representations are appropriate and you would prefer
361 /// to render the error and its cause chain yourself, it can be done something
362 /// like this:
363 ///
364 /// ```
365 /// use anyhow::{Context, Result};
366 ///
367 /// fn main() {
368 /// if let Err(err) = try_main() {
369 /// eprintln!("ERROR: {}", err);
370 /// err.chain().skip(1).for_each(|cause| eprintln!("because: {}", cause));
371 /// std::process::exit(1);
372 /// }
373 /// }
374 ///
375 /// fn try_main() -> Result<()> {
376 /// # const IGNORE: &str = stringify! {
377 /// ...
378 /// # };
379 /// # Ok(())
380 /// }
381 /// ```
382 #[cfg_attr(not(doc), repr(transparent))]
383 pub struct Error {
384 inner: Own<ErrorImpl>,
385 }
386
387 /// Iterator of a chain of source errors.
388 ///
389 /// This type is the iterator returned by [`Error::chain`].
390 ///
391 /// # Example
392 ///
393 /// ```
394 /// use anyhow::Error;
395 /// use std::io;
396 ///
397 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {
398 /// for cause in error.chain() {
399 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {
400 /// return Some(io_error.kind());
401 /// }
402 /// }
403 /// None
404 /// }
405 /// ```
406 #[cfg(feature = "std")]
407 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
408 #[derive(Clone)]
409 pub struct Chain<'a> {
410 state: crate::chain::ChainState<'a>,
411 }
412
413 /// `Result<T, Error>`
414 ///
415 /// This is a reasonable return type to use throughout your application but also
416 /// for `fn main`; if you do, failures will be printed along with any
417 /// [context][Context] and a backtrace if one was captured.
418 ///
419 /// `anyhow::Result` may be used with one *or* two type parameters.
420 ///
421 /// ```rust
422 /// use anyhow::Result;
423 ///
424 /// # const IGNORE: &str = stringify! {
425 /// fn demo1() -> Result<T> {...}
426 /// // ^ equivalent to std::result::Result<T, anyhow::Error>
427 ///
428 /// fn demo2() -> Result<T, OtherError> {...}
429 /// // ^ equivalent to std::result::Result<T, OtherError>
430 /// # };
431 /// ```
432 ///
433 /// # Example
434 ///
435 /// ```
436 /// # pub trait Deserialize {}
437 /// #
438 /// # mod serde_json {
439 /// # use super::Deserialize;
440 /// # use std::io;
441 /// #
442 /// # pub fn from_str<T: Deserialize>(json: &str) -> io::Result<T> {
443 /// # unimplemented!()
444 /// # }
445 /// # }
446 /// #
447 /// # #[derive(Debug)]
448 /// # struct ClusterMap;
449 /// #
450 /// # impl Deserialize for ClusterMap {}
451 /// #
452 /// use anyhow::Result;
453 ///
454 /// fn main() -> Result<()> {
455 /// # return Ok(());
456 /// let config = std::fs::read_to_string("cluster.json")?;
457 /// let map: ClusterMap = serde_json::from_str(&config)?;
458 /// println!("cluster info: {:#?}", map);
459 /// Ok(())
460 /// }
461 /// ```
462 pub type Result<T, E = Error> = core::result::Result<T, E>;
463
464 /// Provides the `context` method for `Result`.
465 ///
466 /// This trait is sealed and cannot be implemented for types outside of
467 /// `anyhow`.
468 ///
469 /// <br>
470 ///
471 /// # Example
472 ///
473 /// ```
474 /// use anyhow::{Context, Result};
475 /// use std::fs;
476 /// use std::path::PathBuf;
477 ///
478 /// pub struct ImportantThing {
479 /// path: PathBuf,
480 /// }
481 ///
482 /// impl ImportantThing {
483 /// # const IGNORE: &'static str = stringify! {
484 /// pub fn detach(&mut self) -> Result<()> {...}
485 /// # };
486 /// # fn detach(&mut self) -> Result<()> {
487 /// # unimplemented!()
488 /// # }
489 /// }
490 ///
491 /// pub fn do_it(mut it: ImportantThing) -> Result<Vec<u8>> {
492 /// it.detach().context("Failed to detach the important thing")?;
493 ///
494 /// let path = &it.path;
495 /// let content = fs::read(path)
496 /// .with_context(|| format!("Failed to read instrs from {}", path.display()))?;
497 ///
498 /// Ok(content)
499 /// }
500 /// ```
501 ///
502 /// When printed, the outermost context would be printed first and the lower
503 /// level underlying causes would be enumerated below.
504 ///
505 /// ```console
506 /// Error: Failed to read instrs from ./path/to/instrs.json
507 ///
508 /// Caused by:
509 /// No such file or directory (os error 2)
510 /// ```
511 ///
512 /// Refer to the [Display representations] documentation for other forms in
513 /// which this context chain can be rendered.
514 ///
515 /// [Display representations]: Error#display-representations
516 ///
517 /// <br>
518 ///
519 /// # Effect on downcasting
520 ///
521 /// After attaching context of type `C` onto an error of type `E`, the resulting
522 /// `anyhow::Error` may be downcast to `C` **or** to `E`.
523 ///
524 /// That is, in codebases that rely on downcasting, Anyhow's context supports
525 /// both of the following use cases:
526 ///
527 /// - **Attaching context whose type is insignificant onto errors whose type
528 /// is used in downcasts.**
529 ///
530 /// In other error libraries whose context is not designed this way, it can
531 /// be risky to introduce context to existing code because new context might
532 /// break existing working downcasts. In Anyhow, any downcast that worked
533 /// before adding context will continue to work after you add a context, so
534 /// you should freely add human-readable context to errors wherever it would
535 /// be helpful.
536 ///
537 /// ```
538 /// # use anyhow::bail;
539 /// # use thiserror::Error;
540 /// #
541 /// # #[derive(Error, Debug)]
542 /// # #[error("???")]
543 /// # struct SuspiciousError;
544 /// #
545 /// # fn helper() -> Result<()> {
546 /// # bail!(SuspiciousError);
547 /// # }
548 /// #
549 /// use anyhow::{Context, Result};
550 ///
551 /// fn do_it() -> Result<()> {
552 /// helper().context("Failed to complete the work")?;
553 /// # const IGNORE: &str = stringify! {
554 /// ...
555 /// # };
556 /// # unreachable!()
557 /// }
558 ///
559 /// fn main() {
560 /// let err = do_it().unwrap_err();
561 /// if let Some(e) = err.downcast_ref::<SuspiciousError>() {
562 /// // If helper() returned SuspiciousError, this downcast will
563 /// // correctly succeed even with the context in between.
564 /// # return;
565 /// }
566 /// # panic!("expected downcast to succeed");
567 /// }
568 /// ```
569 ///
570 /// - **Attaching context whose type is used in downcasts onto errors whose
571 /// type is insignificant.**
572 ///
573 /// Some codebases prefer to use machine-readable context to categorize
574 /// lower level errors in a way that will be actionable to higher levels of
575 /// the application.
576 ///
577 /// ```
578 /// # use anyhow::bail;
579 /// # use thiserror::Error;
580 /// #
581 /// # #[derive(Error, Debug)]
582 /// # #[error("???")]
583 /// # struct HelperFailed;
584 /// #
585 /// # fn helper() -> Result<()> {
586 /// # bail!("no such file or directory");
587 /// # }
588 /// #
589 /// use anyhow::{Context, Result};
590 ///
591 /// fn do_it() -> Result<()> {
592 /// helper().context(HelperFailed)?;
593 /// # const IGNORE: &str = stringify! {
594 /// ...
595 /// # };
596 /// # unreachable!()
597 /// }
598 ///
599 /// fn main() {
600 /// let err = do_it().unwrap_err();
601 /// if let Some(e) = err.downcast_ref::<HelperFailed>() {
602 /// // If helper failed, this downcast will succeed because
603 /// // HelperFailed is the context that has been attached to
604 /// // that error.
605 /// # return;
606 /// }
607 /// # panic!("expected downcast to succeed");
608 /// }
609 /// ```
610 pub trait Context<T, E>: context::private::Sealed {
611 /// Wrap the error value with additional context.
context<C>(self, context: C) -> Result<T, Error> where C: Display + Send + Sync + 'static612 fn context<C>(self, context: C) -> Result<T, Error>
613 where
614 C: Display + Send + Sync + 'static;
615
616 /// Wrap the error value with additional context that is evaluated lazily
617 /// only once an error does occur.
with_context<C, F>(self, f: F) -> Result<T, Error> where C: Display + Send + Sync + 'static, F: FnOnce() -> C618 fn with_context<C, F>(self, f: F) -> Result<T, Error>
619 where
620 C: Display + Send + Sync + 'static,
621 F: FnOnce() -> C;
622 }
623
624 /// Equivalent to Ok::<_, anyhow::Error>(value).
625 ///
626 /// This simplifies creation of an anyhow::Result in places where type inference
627 /// cannot deduce the `E` type of the result — without needing to write
628 /// `Ok::<_, anyhow::Error>(value)`.
629 ///
630 /// One might think that `anyhow::Result::Ok(value)` would work in such cases
631 /// but it does not.
632 ///
633 /// ```console
634 /// error[E0282]: type annotations needed for `std::result::Result<i32, E>`
635 /// --> src/main.rs:11:13
636 /// |
637 /// 11 | let _ = anyhow::Result::Ok(1);
638 /// | - ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the enum `Result`
639 /// | |
640 /// | consider giving this pattern the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified
641 /// ```
642 #[allow(non_snake_case)]
Ok<T>(t: T) -> Result<T>643 pub fn Ok<T>(t: T) -> Result<T> {
644 Result::Ok(t)
645 }
646
647 // Not public API. Referenced by macro-generated code.
648 #[doc(hidden)]
649 pub mod __private {
650 use crate::Error;
651 use alloc::fmt;
652 use core::fmt::Arguments;
653
654 #[doc(hidden)]
655 pub use crate::ensure::{BothDebug, NotBothDebug};
656 #[doc(hidden)]
657 pub use alloc::format;
658 #[doc(hidden)]
659 pub use core::result::Result::Err;
660 #[doc(hidden)]
661 pub use core::{concat, format_args, stringify};
662
663 #[doc(hidden)]
664 pub mod kind {
665 #[doc(hidden)]
666 pub use crate::kind::{AdhocKind, TraitKind};
667
668 #[cfg(feature = "std")]
669 #[doc(hidden)]
670 pub use crate::kind::BoxedKind;
671 }
672
673 #[doc(hidden)]
674 #[inline]
675 #[cold]
format_err(args: Arguments) -> Error676 pub fn format_err(args: Arguments) -> Error {
677 #[cfg(anyhow_no_fmt_arguments_as_str)]
678 let fmt_arguments_as_str = None::<&str>;
679 #[cfg(not(anyhow_no_fmt_arguments_as_str))]
680 let fmt_arguments_as_str = args.as_str();
681
682 if let Some(message) = fmt_arguments_as_str {
683 // anyhow!("literal"), can downcast to &'static str
684 Error::msg(message)
685 } else {
686 // anyhow!("interpolate {var}"), can downcast to String
687 Error::msg(fmt::format(args))
688 }
689 }
690
691 #[doc(hidden)]
692 #[inline]
693 #[cold]
694 #[must_use]
must_use(error: Error) -> Error695 pub fn must_use(error: Error) -> Error {
696 error
697 }
698 }
699