Skip to content

Commit 09b7422

Browse files
authored
Deprecate wasmtime_internal_error::anyhow; prefer wasmtime_internal_error::format_err (#12252)
* Deprecate `wasmtime_internal_error::anyhow`; prefer `wasmtime_internal_error::format_err` * use `format_err!` instead of `anyhow!` in OOM test
1 parent 96e1970 commit 09b7422

4 files changed

Lines changed: 45 additions & 39 deletions

File tree

crates/error/src/error.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,14 @@ impl BoxedDynError {
260260
/// ```
261261
///
262262
/// If you do not want to early-return, just to create the `Error`, then the
263-
/// [`anyhow!`][crate::anyhow] macro is preferred:
263+
/// [`format_err!`][crate::format_err] macro is preferred:
264264
///
265265
/// ```
266266
/// # use wasmtime_internal_error as wasmtime;
267-
/// use wasmtime::{anyhow, Error};
267+
/// use wasmtime::{format_err, Error};
268268
///
269269
/// let x = 42;
270-
/// let my_error: Error = anyhow!("whoops! {x}");
270+
/// let my_error: Error = format_err!("whoops! {x}");
271271
/// ```
272272
///
273273
/// If, however, you happen to require a constructor function instead of a
@@ -511,7 +511,7 @@ impl From<Error> for Box<dyn core::error::Error + 'static> {
511511
}
512512
}
513513

514-
/// Convert a [`Error`] into an [`anyhow::Error`].
514+
/// Convert a [`wasmtime::Error`][crate::Error] into an [`anyhow::Error`].
515515
///
516516
/// # Example
517517
///
@@ -642,8 +642,11 @@ impl Error {
642642
/// # use wasmtime_internal_error as wasmtime;
643643
/// use std::error::Error;
644644
///
645-
/// let anyhow_error = anyhow::Error::msg("whoops");
646-
/// let boxed_error: Box<dyn Error + Send + Sync + 'static> = anyhow_error.into_boxed_dyn_error();
645+
/// // You happen to have a boxed error trait object.
646+
/// let orig_error = std::fs::read("XXX: some file that doesn't exist").unwrap_err();
647+
/// let boxed_error: Box<dyn Error + Send + Sync + 'static> = Box::new(orig_error) as _;
648+
///
649+
/// // You can turn it into a `wasmtime::Error` via `from_boxed`.
647650
/// let wasmtime_error = wasmtime::Error::from_boxed(boxed_error);
648651
/// # }
649652
/// ```

crates/error/src/macros.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use core::fmt::{self, write};
1111

1212
/// Construct an [`Error`](crate::Error) via string formatting or another error.
1313
///
14-
/// Like `anyhow::anyhow!` but for [`wasmtime::Error`][crate::Error].
14+
/// Like `anyhow::format_err!` or `anyhow::anyhow!` but for
15+
/// [`wasmtime::Error`][crate::Error].
1516
///
1617
/// # String Formatting
1718
///
@@ -20,16 +21,16 @@ use core::fmt::{self, write};
2021
///
2122
/// ```
2223
/// # use wasmtime_internal_error as wasmtime;
23-
/// use wasmtime::{anyhow, Error};
24+
/// use wasmtime::{format_err, Error};
2425
///
2526
/// let x = 42;
26-
/// let error: Error = anyhow!("x is {x}");
27+
/// let error: Error = format_err!("x is {x}");
2728
/// assert_eq!(error.to_string(), "x is 42");
2829
///
29-
/// let error: Error = anyhow!("x / 2 is {}", x / 2);
30+
/// let error: Error = format_err!("x / 2 is {}", x / 2);
3031
/// assert_eq!(error.to_string(), "x / 2 is 21");
3132
///
32-
/// let error: Error = anyhow!("x + 1 is {y}", y = x + 1);
33+
/// let error: Error = format_err!("x + 1 is {y}", y = x + 1);
3334
/// assert_eq!(error.to_string(), "x + 1 is 43");
3435
/// ```
3536
///
@@ -45,7 +46,7 @@ use core::fmt::{self, write};
4546
/// #![cfg(feature = "std")]
4647
/// # use wasmtime_internal_error as wasmtime;
4748
/// use std::fmt;
48-
/// use wasmtime::{anyhow, Error};
49+
/// use wasmtime::{format_err, Error};
4950
///
5051
/// #[derive(Debug)]
5152
/// struct SomeOtherError(u32);
@@ -58,13 +59,13 @@ use core::fmt::{self, write};
5859
///
5960
/// impl std::error::Error for SomeOtherError {}
6061
///
61-
/// let error: Error = anyhow!(SomeOtherError(36));
62+
/// let error: Error = format_err!(SomeOtherError(36));
6263
/// assert!(error.is::<SomeOtherError>());
6364
/// assert_eq!(error.to_string(), "some other error (code 36)");
6465
/// # }
6566
/// ```
6667
#[macro_export]
67-
macro_rules! anyhow {
68+
macro_rules! format_err {
6869
// Format-style invocation without explicit format arguments.
6970
( $message:literal $(,)? ) => {
7071
$crate::Error::from_format_args($crate::macros::format_args!($message))
@@ -84,22 +85,24 @@ macro_rules! anyhow {
8485
}};
8586
}
8687

87-
/// Identical to the [`anyhow!`][crate::anyhow] macro.
88+
/// Identical to the [`format_err!`][crate::format_err] macro.
8889
///
89-
/// Provided for compatibility.
90+
/// This is provided for API compatibility with the `anyhow` crate, but you
91+
/// should prefer using `format_err!` instead.
9092
#[macro_export]
91-
macro_rules! format_err {
93+
#[deprecated = "Use `format_err!(...)` instead"]
94+
macro_rules! anyhow {
9295
( $( $args:tt )* ) => {
93-
anyhow!( $( $args )* )
96+
$crate::format_err!( $( $args )* )
9497
};
9598
}
9699

97100
/// Early exit from the current function with an error.
98101
///
99-
/// This helper is equivalent to `return Err(anyhow!(...))`.
102+
/// This helper is equivalent to `return Err(format_err!(...))`.
100103
///
101-
/// See the docs for the [`anyhow!`][crate::anyhow] macro for details on the
102-
/// kinds of errors that can be constructed.
104+
/// See the docs for the [`format_err!`][crate::format_err] macro for details on
105+
/// the kinds of errors that can be constructed.
103106
///
104107
/// Like `anyhow::bail!` but for [`wasmtime::Error`][crate::Error].
105108
///
@@ -128,7 +131,7 @@ macro_rules! format_err {
128131
#[macro_export]
129132
macro_rules! bail {
130133
( $($args:tt)* ) => {{
131-
return $crate::macros::Err($crate::anyhow!( $( $args )* ));
134+
return $crate::macros::Err($crate::format_err!( $( $args )* ));
132135
}};
133136
}
134137

@@ -139,7 +142,7 @@ macro_rules! bail {
139142
///
140143
/// ```ignore
141144
/// if !condition {
142-
/// return Err(anyhow!(...));
145+
/// return Err(format_err!(...));
143146
/// }
144147
/// ```
145148
///
@@ -291,7 +294,7 @@ pub mod formatting {
291294
impl Error {
292295
/// Construct an `Error` from format arguments.
293296
///
294-
/// Only for use by the `anyhow!` macro.
297+
/// Only for use by the `format_err!` macro.
295298
#[doc(hidden)]
296299
pub fn from_format_args(args: fmt::Arguments<'_>) -> Self {
297300
if let Some(s) = args.as_str() {

crates/error/tests/tests.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use core::{
1313
};
1414
#[cfg(feature = "std")]
1515
use std::backtrace::BacktraceStatus;
16-
use wasmtime_internal_error::{Context, Error, OutOfMemory, Result, anyhow, bail, ensure};
16+
use wasmtime_internal_error::{Context, Error, OutOfMemory, Result, bail, ensure, format_err};
1717

1818
#[derive(Debug)]
1919
struct TestError(u32);
@@ -244,41 +244,41 @@ fn backtrace() {
244244
}
245245

246246
#[test]
247-
fn anyhow_macro_string_literal() {
248-
let error = anyhow!("literal");
247+
fn format_err_macro_string_literal() {
248+
let error = format_err!("literal");
249249
assert_eq!(error.to_string(), "literal");
250250
}
251251

252252
#[test]
253-
fn anyhow_macro_format_implicit_args() {
253+
fn format_err_macro_format_implicit_args() {
254254
let x = 42;
255255
let y = 36;
256-
let error = anyhow!("implicit args {x} {y}");
256+
let error = format_err!("implicit args {x} {y}");
257257
assert_eq!(error.to_string(), "implicit args 42 36");
258258
}
259259

260260
#[test]
261-
fn anyhow_macro_format_explicit_args() {
261+
fn format_err_macro_format_explicit_args() {
262262
let a = 84;
263263
let b = 72;
264-
let error = anyhow!("explicit args {x} {y}", x = a / 2, y = b / 2);
264+
let error = format_err!("explicit args {x} {y}", x = a / 2, y = b / 2);
265265
assert_eq!(error.to_string(), "explicit args 42 36");
266266
}
267267

268268
#[test]
269-
fn anyhow_macro_core_error() {
269+
fn format_err_macro_core_error() {
270270
let error = TestError(42);
271-
let error = anyhow!(error);
271+
let error = format_err!(error);
272272
assert!(error.is::<TestError>());
273273
assert_eq!(error.to_string(), "TestError(42)");
274274
}
275275

276276
#[test]
277-
fn anyhow_macro_core_error_chain() {
277+
fn format_err_macro_core_error_chain() {
278278
let error = ChainError::new("ouch", None);
279279
let error = ChainError::new("yikes", Some(Box::new(error)));
280280
let error = ChainError::new("whoops", Some(Box::new(error)));
281-
let error = anyhow!(error);
281+
let error = format_err!(error);
282282

283283
let mut chain = error.chain();
284284

@@ -295,9 +295,9 @@ fn anyhow_macro_core_error_chain() {
295295
}
296296

297297
#[test]
298-
fn anyhow_macro_msg() {
298+
fn format_err_macro_msg() {
299299
let error = 42;
300-
let error = anyhow!(error);
300+
let error = format_err!(error);
301301
assert_eq!(error.to_string(), "42");
302302
}
303303

crates/fuzzing/tests/oom.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
sync::atomic::{AtomicU32, Ordering::SeqCst},
55
};
66
use wasmtime::Config;
7-
use wasmtime_error::{Error, OutOfMemory, Result, anyhow};
7+
use wasmtime_error::{Error, OutOfMemory, Result, format_err};
88
use wasmtime_fuzzing::oom::{OomTest, OomTestAllocator};
99

1010
#[global_allocator]
@@ -70,7 +70,7 @@ static X: AtomicU32 = AtomicU32::new(42);
7070
fn error_fmt() -> Result<()> {
7171
OomTest::new().test(|| {
7272
let x = X.load(SeqCst);
73-
let error = anyhow!("ouch: {x}");
73+
let error = format_err!("ouch: {x}");
7474
ok_if_not_oom(error)
7575
})
7676
}

0 commit comments

Comments
 (0)