I have been using the anyhow to provide some context to my error Results, which works fine.
But now I find I can't create my own errors, even the simple examples from the anyhow docs do not work for me:
use anyhow::Result;
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),
}
fn main() -> Result<()> {
let missing = 0;
let attr = String::new();
return Err(FormatError::MissingAttribute(attr));
return Err(anyhow!("Missing attribute: {}", missing));
}
Errors out with:
$ cargo run
Compiling junk v0.1.0 (/mnt/c/Users/zicog/conveqs/junk)
error: cannot find macro `anyhow` in this scope
--> src/main.rs:21:16
|
21 | return Err(anyhow!("Missing attribute: {}", missing));
| ^^^^^^
error[E0308]: mismatched types
--> src/main.rs:19:16
|
19 | return Err(FormatError::MissingAttribute(attr));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `anyhow::Error`, found enum `FormatError`
Where is that pesky anyhow! macro hiding?
What do I have to do to get the "FormatError" example working?
I have this in Cargo.toml
[dependencies]
anyhow = "1.0"
thiserror = "1.0"