]> git.proxmox.com Git - cargo.git/blob - vendor/anyhow/src/lib.rs
New upstream version 0.52.0
[cargo.git] / vendor / anyhow / src / lib.rs
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&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K
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 the nightly channel, 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 //! The tracking issue for this feature is [rust-lang/rust#53487].
143 //!
144 //! [`std::backtrace`]: https://doc.rust-lang.org/std/backtrace/index.html#environment-variables
145 //! [rust-lang/rust#53487]: https://github.com/rust-lang/rust/issues/53487
146 //!
147 //! - Anyhow works with any error type that has an impl of `std::error::Error`,
148 //! including ones defined in your crate. We do not bundle a `derive(Error)`
149 //! macro but you can write the impls yourself or use a standalone macro like
150 //! [thiserror].
151 //!
152 //! [thiserror]: https://github.com/dtolnay/thiserror
153 //!
154 //! ```
155 //! use thiserror::Error;
156 //!
157 //! #[derive(Error, Debug)]
158 //! pub enum FormatError {
159 //! #[error("Invalid header (expected {expected:?}, got {found:?})")]
160 //! InvalidHeader {
161 //! expected: String,
162 //! found: String,
163 //! },
164 //! #[error("Missing attribute: {0}")]
165 //! MissingAttribute(String),
166 //! }
167 //! ```
168 //!
169 //! - One-off error messages can be constructed using the `anyhow!` macro, which
170 //! supports string interpolation and produces an `anyhow::Error`.
171 //!
172 //! ```
173 //! # use anyhow::{anyhow, Result};
174 //! #
175 //! # fn demo() -> Result<()> {
176 //! # let missing = "...";
177 //! return Err(anyhow!("Missing attribute: {}", missing));
178 //! # Ok(())
179 //! # }
180 //! ```
181 //!
182 //! A `bail!` macro is provided as a shorthand for the same early return.
183 //!
184 //! ```
185 //! # use anyhow::{bail, Result};
186 //! #
187 //! # fn demo() -> Result<()> {
188 //! # let missing = "...";
189 //! bail!("Missing attribute: {}", missing);
190 //! # Ok(())
191 //! # }
192 //! ```
193 //!
194 //! <br>
195 //!
196 //! # No-std support
197 //!
198 //! In no_std mode, the same API is almost all available and works the same way.
199 //! To depend on Anyhow in no_std mode, disable our default enabled "std"
200 //! feature in Cargo.toml. A global allocator is required.
201 //!
202 //! ```toml
203 //! [dependencies]
204 //! anyhow = { version = "1.0", default-features = false }
205 //! ```
206 //!
207 //! Since the `?`-based error conversions would normally rely on the
208 //! `std::error::Error` trait which is only available through std, no_std mode
209 //! will require an explicit `.map_err(Error::msg)` when working with a
210 //! non-Anyhow error type inside a function that returns Anyhow's error type.
211
212 #![doc(html_root_url = "https://docs.rs/anyhow/1.0.40")]
213 #![cfg_attr(backtrace, feature(backtrace))]
214 #![cfg_attr(doc_cfg, feature(doc_cfg))]
215 #![cfg_attr(not(feature = "std"), no_std)]
216 #![deny(dead_code, unused_imports, unused_mut)]
217 #![allow(
218 clippy::doc_markdown,
219 clippy::enum_glob_use,
220 clippy::missing_errors_doc,
221 clippy::missing_panics_doc,
222 clippy::module_name_repetitions,
223 clippy::must_use_candidate,
224 clippy::needless_doctest_main,
225 clippy::new_ret_no_self,
226 clippy::redundant_else,
227 clippy::unused_self,
228 clippy::used_underscore_binding,
229 clippy::wildcard_imports,
230 clippy::wrong_self_convention
231 )]
232
233 mod alloc {
234 #[cfg(not(feature = "std"))]
235 extern crate alloc;
236
237 #[cfg(not(feature = "std"))]
238 pub use alloc::boxed::Box;
239
240 #[cfg(feature = "std")]
241 pub use std::boxed::Box;
242 }
243
244 #[macro_use]
245 mod backtrace;
246 mod chain;
247 mod context;
248 mod error;
249 mod fmt;
250 mod kind;
251 mod macros;
252 mod ptr;
253 mod wrapper;
254
255 use crate::error::ErrorImpl;
256 use crate::ptr::Own;
257 use core::fmt::Display;
258
259 #[cfg(not(feature = "std"))]
260 use core::fmt::Debug;
261
262 #[cfg(feature = "std")]
263 use std::error::Error as StdError;
264
265 #[cfg(not(feature = "std"))]
266 trait StdError: Debug + Display {
267 fn source(&self) -> Option<&(dyn StdError + 'static)> {
268 None
269 }
270 }
271
272 pub use anyhow as format_err;
273
274 /// The `Error` type, a wrapper around a dynamic error type.
275 ///
276 /// `Error` works a lot like `Box<dyn std::error::Error>`, but with these
277 /// differences:
278 ///
279 /// - `Error` requires that the error is `Send`, `Sync`, and `'static`.
280 /// - `Error` guarantees that a backtrace is available, even if the underlying
281 /// error type does not provide one.
282 /// - `Error` is represented as a narrow pointer &mdash; exactly one word in
283 /// size instead of two.
284 ///
285 /// <br>
286 ///
287 /// # Display representations
288 ///
289 /// When you print an error object using "{}" or to_string(), only the outermost
290 /// underlying error or context is printed, not any of the lower level causes.
291 /// This is exactly as if you had called the Display impl of the error from
292 /// which you constructed your anyhow::Error.
293 ///
294 /// ```console
295 /// Failed to read instrs from ./path/to/instrs.json
296 /// ```
297 ///
298 /// To print causes as well using anyhow's default formatting of causes, use the
299 /// alternate selector "{:#}".
300 ///
301 /// ```console
302 /// Failed to read instrs from ./path/to/instrs.json: No such file or directory (os error 2)
303 /// ```
304 ///
305 /// The Debug format "{:?}" includes your backtrace if one was captured. Note
306 /// that this is the representation you get by default if you return an error
307 /// from `fn main` instead of printing it explicitly yourself.
308 ///
309 /// ```console
310 /// Error: Failed to read instrs from ./path/to/instrs.json
311 ///
312 /// Caused by:
313 /// No such file or directory (os error 2)
314 /// ```
315 ///
316 /// and if there is a backtrace available:
317 ///
318 /// ```console
319 /// Error: Failed to read instrs from ./path/to/instrs.json
320 ///
321 /// Caused by:
322 /// No such file or directory (os error 2)
323 ///
324 /// Stack backtrace:
325 /// 0: <E as anyhow::context::ext::StdError>::ext_context
326 /// at /git/anyhow/src/backtrace.rs:26
327 /// 1: core::result::Result<T,E>::map_err
328 /// at /git/rustc/src/libcore/result.rs:596
329 /// 2: anyhow::context::<impl anyhow::Context<T,E> for core::result::Result<T,E>>::with_context
330 /// at /git/anyhow/src/context.rs:58
331 /// 3: testing::main
332 /// at src/main.rs:5
333 /// 4: std::rt::lang_start
334 /// at /git/rustc/src/libstd/rt.rs:61
335 /// 5: main
336 /// 6: __libc_start_main
337 /// 7: _start
338 /// ```
339 ///
340 /// To see a conventional struct-style Debug representation, use "{:#?}".
341 ///
342 /// ```console
343 /// Error {
344 /// context: "Failed to read instrs from ./path/to/instrs.json",
345 /// source: Os {
346 /// code: 2,
347 /// kind: NotFound,
348 /// message: "No such file or directory",
349 /// },
350 /// }
351 /// ```
352 ///
353 /// If none of the built-in representations are appropriate and you would prefer
354 /// to render the error and its cause chain yourself, it can be done something
355 /// like this:
356 ///
357 /// ```
358 /// use anyhow::{Context, Result};
359 ///
360 /// fn main() {
361 /// if let Err(err) = try_main() {
362 /// eprintln!("ERROR: {}", err);
363 /// err.chain().skip(1).for_each(|cause| eprintln!("because: {}", cause));
364 /// std::process::exit(1);
365 /// }
366 /// }
367 ///
368 /// fn try_main() -> Result<()> {
369 /// # const IGNORE: &str = stringify! {
370 /// ...
371 /// # };
372 /// # Ok(())
373 /// }
374 /// ```
375 #[repr(transparent)]
376 pub struct Error {
377 inner: Own<ErrorImpl>,
378 }
379
380 /// Iterator of a chain of source errors.
381 ///
382 /// This type is the iterator returned by [`Error::chain`].
383 ///
384 /// # Example
385 ///
386 /// ```
387 /// use anyhow::Error;
388 /// use std::io;
389 ///
390 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {
391 /// for cause in error.chain() {
392 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {
393 /// return Some(io_error.kind());
394 /// }
395 /// }
396 /// None
397 /// }
398 /// ```
399 #[cfg(feature = "std")]
400 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
401 #[derive(Clone)]
402 pub struct Chain<'a> {
403 state: crate::chain::ChainState<'a>,
404 }
405
406 /// `Result<T, Error>`
407 ///
408 /// This is a reasonable return type to use throughout your application but also
409 /// for `fn main`; if you do, failures will be printed along with any
410 /// [context][Context] and a backtrace if one was captured.
411 ///
412 /// `anyhow::Result` may be used with one *or* two type parameters.
413 ///
414 /// ```rust
415 /// use anyhow::Result;
416 ///
417 /// # const IGNORE: &str = stringify! {
418 /// fn demo1() -> Result<T> {...}
419 /// // ^ equivalent to std::result::Result<T, anyhow::Error>
420 ///
421 /// fn demo2() -> Result<T, OtherError> {...}
422 /// // ^ equivalent to std::result::Result<T, OtherError>
423 /// # };
424 /// ```
425 ///
426 /// # Example
427 ///
428 /// ```
429 /// # pub trait Deserialize {}
430 /// #
431 /// # mod serde_json {
432 /// # use super::Deserialize;
433 /// # use std::io;
434 /// #
435 /// # pub fn from_str<T: Deserialize>(json: &str) -> io::Result<T> {
436 /// # unimplemented!()
437 /// # }
438 /// # }
439 /// #
440 /// # #[derive(Debug)]
441 /// # struct ClusterMap;
442 /// #
443 /// # impl Deserialize for ClusterMap {}
444 /// #
445 /// use anyhow::Result;
446 ///
447 /// fn main() -> Result<()> {
448 /// # return Ok(());
449 /// let config = std::fs::read_to_string("cluster.json")?;
450 /// let map: ClusterMap = serde_json::from_str(&config)?;
451 /// println!("cluster info: {:#?}", map);
452 /// Ok(())
453 /// }
454 /// ```
455 pub type Result<T, E = Error> = core::result::Result<T, E>;
456
457 /// Provides the `context` method for `Result`.
458 ///
459 /// This trait is sealed and cannot be implemented for types outside of
460 /// `anyhow`.
461 ///
462 /// <br>
463 ///
464 /// # Example
465 ///
466 /// ```
467 /// use anyhow::{Context, Result};
468 /// use std::fs;
469 /// use std::path::PathBuf;
470 ///
471 /// pub struct ImportantThing {
472 /// path: PathBuf,
473 /// }
474 ///
475 /// impl ImportantThing {
476 /// # const IGNORE: &'static str = stringify! {
477 /// pub fn detach(&mut self) -> Result<()> {...}
478 /// # };
479 /// # fn detach(&mut self) -> Result<()> {
480 /// # unimplemented!()
481 /// # }
482 /// }
483 ///
484 /// pub fn do_it(mut it: ImportantThing) -> Result<Vec<u8>> {
485 /// it.detach().context("Failed to detach the important thing")?;
486 ///
487 /// let path = &it.path;
488 /// let content = fs::read(path)
489 /// .with_context(|| format!("Failed to read instrs from {}", path.display()))?;
490 ///
491 /// Ok(content)
492 /// }
493 /// ```
494 ///
495 /// When printed, the outermost context would be printed first and the lower
496 /// level underlying causes would be enumerated below.
497 ///
498 /// ```console
499 /// Error: Failed to read instrs from ./path/to/instrs.json
500 ///
501 /// Caused by:
502 /// No such file or directory (os error 2)
503 /// ```
504 ///
505 /// <br>
506 ///
507 /// # Effect on downcasting
508 ///
509 /// After attaching context of type `C` onto an error of type `E`, the resulting
510 /// `anyhow::Error` may be downcast to `C` **or** to `E`.
511 ///
512 /// That is, in codebases that rely on downcasting, Anyhow's context supports
513 /// both of the following use cases:
514 ///
515 /// - **Attaching context whose type is insignificant onto errors whose type
516 /// is used in downcasts.**
517 ///
518 /// In other error libraries whose context is not designed this way, it can
519 /// be risky to introduce context to existing code because new context might
520 /// break existing working downcasts. In Anyhow, any downcast that worked
521 /// before adding context will continue to work after you add a context, so
522 /// you should freely add human-readable context to errors wherever it would
523 /// be helpful.
524 ///
525 /// ```
526 /// # use anyhow::bail;
527 /// # use thiserror::Error;
528 /// #
529 /// # #[derive(Error, Debug)]
530 /// # #[error("???")]
531 /// # struct SuspiciousError;
532 /// #
533 /// # fn helper() -> Result<()> {
534 /// # bail!(SuspiciousError);
535 /// # }
536 /// #
537 /// use anyhow::{Context, Result};
538 ///
539 /// fn do_it() -> Result<()> {
540 /// helper().context("Failed to complete the work")?;
541 /// # const IGNORE: &str = stringify! {
542 /// ...
543 /// # };
544 /// # unreachable!()
545 /// }
546 ///
547 /// fn main() {
548 /// let err = do_it().unwrap_err();
549 /// if let Some(e) = err.downcast_ref::<SuspiciousError>() {
550 /// // If helper() returned SuspiciousError, this downcast will
551 /// // correctly succeed even with the context in between.
552 /// # return;
553 /// }
554 /// # panic!("expected downcast to succeed");
555 /// }
556 /// ```
557 ///
558 /// - **Attaching context whose type is used in downcasts onto errors whose
559 /// type is insignificant.**
560 ///
561 /// Some codebases prefer to use machine-readable context to categorize
562 /// lower level errors in a way that will be actionable to higher levels of
563 /// the application.
564 ///
565 /// ```
566 /// # use anyhow::bail;
567 /// # use thiserror::Error;
568 /// #
569 /// # #[derive(Error, Debug)]
570 /// # #[error("???")]
571 /// # struct HelperFailed;
572 /// #
573 /// # fn helper() -> Result<()> {
574 /// # bail!("no such file or directory");
575 /// # }
576 /// #
577 /// use anyhow::{Context, Result};
578 ///
579 /// fn do_it() -> Result<()> {
580 /// helper().context(HelperFailed)?;
581 /// # const IGNORE: &str = stringify! {
582 /// ...
583 /// # };
584 /// # unreachable!()
585 /// }
586 ///
587 /// fn main() {
588 /// let err = do_it().unwrap_err();
589 /// if let Some(e) = err.downcast_ref::<HelperFailed>() {
590 /// // If helper failed, this downcast will succeed because
591 /// // HelperFailed is the context that has been attached to
592 /// // that error.
593 /// # return;
594 /// }
595 /// # panic!("expected downcast to succeed");
596 /// }
597 /// ```
598 pub trait Context<T, E>: context::private::Sealed {
599 /// Wrap the error value with additional context.
600 fn context<C>(self, context: C) -> Result<T, Error>
601 where
602 C: Display + Send + Sync + 'static;
603
604 /// Wrap the error value with additional context that is evaluated lazily
605 /// only once an error does occur.
606 fn with_context<C, F>(self, f: F) -> Result<T, Error>
607 where
608 C: Display + Send + Sync + 'static,
609 F: FnOnce() -> C;
610 }
611
612 // Not public API. Referenced by macro-generated code.
613 #[doc(hidden)]
614 pub mod private {
615 use crate::Error;
616 use core::fmt::{Debug, Display};
617
618 pub use core::result::Result::Err;
619
620 #[doc(hidden)]
621 pub mod kind {
622 pub use crate::kind::{AdhocKind, TraitKind};
623
624 #[cfg(feature = "std")]
625 pub use crate::kind::BoxedKind;
626 }
627
628 pub fn new_adhoc<M>(message: M) -> Error
629 where
630 M: Display + Debug + Send + Sync + 'static,
631 {
632 Error::from_adhoc(message, backtrace!())
633 }
634
635 #[cfg(anyhow_no_macro_reexport)]
636 pub use crate::{__anyhow_concat as concat, __anyhow_stringify as stringify};
637 #[cfg(not(anyhow_no_macro_reexport))]
638 pub use core::{concat, stringify};
639
640 #[cfg(anyhow_no_macro_reexport)]
641 #[doc(hidden)]
642 #[macro_export]
643 macro_rules! __anyhow_concat {
644 ($($tt:tt)*) => {
645 concat!($($tt)*)
646 };
647 }
648
649 #[cfg(anyhow_no_macro_reexport)]
650 #[doc(hidden)]
651 #[macro_export]
652 macro_rules! __anyhow_stringify {
653 ($($tt:tt)*) => {
654 stringify!($($tt)*)
655 };
656 }
657 }