1 /// Return early with an error.
3 /// This macro is equivalent to `return Err(`[`anyhow!($args...)`][anyhow!]`)`.
5 /// The surrounding function's or closure's return value is required to be
6 /// `Result<_,`[`anyhow::Error`][crate::Error]`>`.
8 /// [anyhow!]: crate::anyhow
13 /// # use anyhow::{bail, Result};
15 /// # fn has_permission(user: usize, resource: usize) -> bool {
19 /// # fn main() -> Result<()> {
21 /// # let resource = 0;
23 /// if !has_permission(user, resource) {
24 /// bail!("permission denied for accessing {}", resource);
31 /// # use anyhow::{bail, Result};
32 /// # use thiserror::Error;
34 /// # const MAX_DEPTH: usize = 1;
36 /// #[derive(Error, Debug)]
37 /// enum ScienceError {
38 /// #[error("recursion limit exceeded")]
39 /// RecursionLimitExceeded,
41 /// # More = (stringify! {
46 /// # fn main() -> Result<()> {
49 /// if depth > MAX_DEPTH {
50 /// bail!(ScienceError::RecursionLimitExceeded);
57 ($msg:literal $(,)?) => {
58 return $crate::__private::Err($crate::__anyhow!($msg))
60 ($err:expr $(,)?) => {
61 return $crate::__private::Err($crate::__anyhow!($err))
63 ($fmt:expr, $($arg:tt)*) => {
64 return $crate::__private::Err($crate::__anyhow!($fmt, $($arg)*))
68 /// Return early with an error if a condition is not satisfied.
70 /// This macro is equivalent to `if !$cond { return
71 /// Err(`[`anyhow!($args...)`][anyhow!]`); }`.
73 /// The surrounding function's or closure's return value is required to be
74 /// `Result<_,`[`anyhow::Error`][crate::Error]`>`.
76 /// Analogously to `assert!`, `ensure!` takes a condition and exits the function
77 /// if the condition fails. Unlike `assert!`, `ensure!` returns an `Error`
78 /// rather than panicking.
80 /// [anyhow!]: crate::anyhow
85 /// # use anyhow::{ensure, Result};
87 /// # fn main() -> Result<()> {
90 /// ensure!(user == 0, "only user 0 is allowed");
96 /// # use anyhow::{ensure, Result};
97 /// # use thiserror::Error;
99 /// # const MAX_DEPTH: usize = 1;
101 /// #[derive(Error, Debug)]
102 /// enum ScienceError {
103 /// #[error("recursion limit exceeded")]
104 /// RecursionLimitExceeded,
105 /// # #[error("...")]
106 /// # More = (stringify! {
111 /// # fn main() -> Result<()> {
114 /// ensure!(depth <= MAX_DEPTH, ScienceError::RecursionLimitExceeded);
120 macro_rules! ensure {
121 ($cond:expr $(,)?) => {
123 return $crate::__private::Err($crate::Error::msg(
124 $crate::__private::concat!("Condition failed: `", $crate::__private::stringify!($cond), "`")
128 ($cond:expr, $msg:literal $(,)?) => {
130 return $crate::__private::Err($crate::__anyhow!($msg));
133 ($cond:expr, $err:expr $(,)?) => {
135 return $crate::__private::Err($crate::__anyhow!($err));
138 ($cond:expr, $fmt:expr, $($arg:tt)*) => {
140 return $crate::__private::Err($crate::__anyhow!($fmt, $($arg)*));
147 macro_rules! ensure {
149 $crate::__parse_ensure!(
153 /* fuel */ (~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~)
161 /// Construct an ad-hoc error from a string or existing non-`anyhow` error
164 /// This evaluates to an [`Error`][crate::Error]. It can take either just a
165 /// string, or a format string with arguments. It also can take any custom type
166 /// which implements `Debug` and `Display`.
168 /// If called with a single argument whose type implements `std::error::Error`
169 /// (in addition to `Debug` and `Display`, which are always required), then that
170 /// Error impl's `source` is preserved as the `source` of the resulting
178 /// use anyhow::{anyhow, Result};
180 /// fn lookup(key: &str) -> Result<V> {
181 /// if key.len() != 16 {
182 /// return Err(anyhow!("key length must be 16 characters, got {:?}", key));
190 macro_rules! anyhow {
191 ($msg:literal $(,)?) => {
192 $crate::__private::must_use({
193 let error = $crate::__private::format_err($crate::__private::format_args!($msg));
197 ($err:expr $(,)?) => {
198 $crate::__private::must_use({
199 use $crate::__private::kind::*;
200 let error = match $err {
201 error => (&error).anyhow_kind().new(error),
206 ($fmt:expr, $($arg:tt)*) => {
207 $crate::Error::msg($crate::__private::format!($fmt, $($arg)*))
211 // Not public API. This is used in the implementation of some of the other
212 // macros, in which the must_use call is not needed because the value is known
216 macro_rules! __anyhow {
217 ($msg:literal $(,)?) => ({
218 let error = $crate::__private::format_err($crate::__private::format_args!($msg));
221 ($err:expr $(,)?) => ({
222 use $crate::__private::kind::*;
223 let error = match $err {
224 error => (&error).anyhow_kind().new(error),
228 ($fmt:expr, $($arg:tt)*) => {
229 $crate::Error::msg($crate::__private::format!($fmt, $($arg)*))