-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Panic when formatting Value outside of virtual machines #747
Comments
Result
wrapped in rune Value
Result
wrapped in rune Value
Before I switched from log to tracing for my project I would get this when attempting to unwrap the returned result in my user code. I came up with this monstrosity as a workaround. But this doesn't help now that it crashes internally in tracing. /// Attempt to format the error in the best way possible.
///
/// Unfortunately this is awkward with dynamic Rune values.
fn try_format_error(phase: Phase, e: &rune::Value) -> anyhow::Result<()> {
match e.clone().into_any() {
rune::runtime::VmResult::Ok(any) => {
match any.clone().downcast_into_ref::<anyhow::Error>() {
Ok(err) => {
anyhow::bail!("Got error result from {phase}: {:?}", *err);
}
Err(err) => {
let ty = try_get_type_info(e, "error");
anyhow::bail!(
"Got error result from {phase}, but it was not an anyhow error: {err:?}, type info: {ty}: {any:?}",
);
}
}
}
rune::runtime::VmResult::Err(not_any) => {
tracing::error!("Got error result from {phase}, it was not an Any: {not_any:?}. Trying other approches at printing the error.");
}
}
// Attempt to format the error
let formatted = catch_unwind(AssertUnwindSafe(|| {
format!("Got error result from {phase}: {e:?}")
}));
match formatted {
Ok(str) => anyhow::bail!(str),
Err(_) => {
let ty = try_get_type_info(e, "error");
anyhow::bail!(
"Got error result from {phase}, but got a panic while attempting to format said error for printing, {ty}",
);
}
}
}
/// Best effort attempt at gettint the type info and printing it
fn try_get_type_info(e: &rune::Value, what: &str) -> String {
match e.type_info() {
rune::runtime::VmResult::Ok(ty) => format!("type info for {what}: {ty:?}"),
rune::runtime::VmResult::Err(err) => {
format!("failed getting type info for {what}: {err:?}")
}
}
} |
Well, For some reason we've also opted to instrument the return value here. This is not correct and should be removed since formatting might fail. It is convenient for |
My understanding is that std::fmt::Error is for when the formatting itself fails. And the Rust standard library will in fact panic in So if I understand you correctly and it error when printing an error (as opposed when to the formatting fails) this seems incorrect and like a misunderstanding. |
Yeah, we should fix it. |
I'll make a PR later today |
This ensures that we don't get an error on formatting a value without a debug protocol. (fixes rune-rs#747)
This ensures that we don't get an error on formatting a value without a debug protocol. Instead attempt to print *something* (fixes #747)
Result
wrapped in rune Value
Given the following example code (full reproducer project attached as tar.gz below):
reproduce
we get the following panic while running it
Example project triggering this: rune_fmt_panic.tar.gz
The text was updated successfully, but these errors were encountered: