You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have some code that tries to be generic over the error type. It works fine with a "specific" error type, e.g. one derived with thiserror, but it doesn't work with anyhow because anyhow::Error does not implement std::error::Error. I saw a similar issue here: #25, but I think my use case is a bit different so I'm hoping you could provide some guidance about how best to proceed. This example is a bit long but hopefully communicates what I'm trying to do:
use std::error::ErrorasErrorTrait;use std::io::ErrorKind;typeCallback<E> = dynFn() -> Result<String,E>;typeErrorHandler<E> = dynFn(E);fndefault_error_handler<E:ErrorTrait>(error:E){println!("an error occurred: {}, source: {:?}", error, error.source());}fnio_error_handler(error: std::io::Error){match error.kind(){ErrorKind::NotFound => println!("io error: not found"),
_ => println!("some other kind of IO error"),}}structThing<'a,E:ErrorTrait>{callback:&'a Callback<E>,error_handler:&'a ErrorHandler<E>,}impl<'a,E:ErrorTrait>Thing<'a,E>{fnrun(&self){match(self.callback)(){Ok(val) => {println!("callback returned: {}", val);}Err(err) => {(self.error_handler)(err);}}}}fncallback1() -> Result<String, std::io::Error>{Ok("hello from callback1".into())}fncallback2() -> Result<String, anyhow::Error>{Ok("hello from callback2".into())}fnmain(){let thing1 = Thing{callback:&callback1,error_handler:&io_error_handler,};
thing1.run();let thing2 = Thing{callback:&callback2,error_handler:&default_error_handler,};
thing2.run();}
The basic idea is that I want to have a callback that can return any type of error. There is also a customizable error handling function to handle errors returned by the callback. The default error handler works for anything that implements std::error::Error, but can also be replaced by a more specific error handler that operates on a particular error type.
Of course, the code above does not compile because thing2 tries to use anyhow::Error as the error type, which does not implement std::error::Error. I'd be interested to hear any suggestion about how to make this API work better. Do I just need to have explicit support for anyhow::Error, separate from other error types? Or is there some more general way to make this work? Thanks :)
The text was updated successfully, but these errors were encountered:
dtolnay
changed the title
Question about anyhow::Error not implementing std::error::Error
Generic handler code that relies on a std::error::Error implementation
Jul 12, 2020
I have some code that tries to be generic over the error type. It works fine with a "specific" error type, e.g. one derived with
thiserror
, but it doesn't work withanyhow
becauseanyhow::Error
does not implementstd::error::Error
. I saw a similar issue here: #25, but I think my use case is a bit different so I'm hoping you could provide some guidance about how best to proceed. This example is a bit long but hopefully communicates what I'm trying to do:The basic idea is that I want to have a callback that can return any type of error. There is also a customizable error handling function to handle errors returned by the callback. The default error handler works for anything that implements
std::error::Error
, but can also be replaced by a more specific error handler that operates on a particular error type.Of course, the code above does not compile because
thing2
tries to useanyhow::Error
as the error type, which does not implementstd::error::Error
. I'd be interested to hear any suggestion about how to make this API work better. Do I just need to have explicit support foranyhow::Error
, separate from other error types? Or is there some more general way to make this work? Thanks :)The text was updated successfully, but these errors were encountered: