Best practice of error handling? #716
-
Hi, I'm trying to embed rune in my project as script engine, I found that it is difficult to handle errors. I have several modules that contain functions returning different type of Result, such as #[rune::function]
pub fn func_a() -> Result<(), AError> {...}
#[rune::function]
pub fn func_b() -> Result<(), BError> {...} and use them in the script: pub fn main() {
func_a()?;
func_b()?;
return sth;
} let output = vm.async_call(["main"], (args)).await?;
let object: Object = rune::from_value(output)?; I found that if I also tried change the convert type from What is the best practices of error handling when integrating rune? thanks for any response! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You have two options on the edge, either everything needs to be a The former is what's encouraged. Try to keep your types coherent, at least at the edge of your function. In your case that would mean returning The latter is possible, but won't really be easy to work with since calculating a type hash is a bit tricky right now. In git we actually have a macro that can do it which will be in the next release. |
Beta Was this translation helpful? Give feedback.
You have two options on the edge, either everything needs to be a
Result
so you can convert it into astd::result::Result<T, E>
, or you'll have to typecheck by comparing the value ofValue::type_hash
with the corresponding type hash and only perform the conversion if they match.The former is what's encouraged. Try to keep your types coherent, at least at the edge of your function. In your case that would mean returning
Ok(sth)
instead of juststh
.The latter is possible, but won't really be easy to work with since calculating a type hash is a bit tricky right now. In git we actually have a macro that can do it which will be in the next release.