error handling

This commit is contained in:
Jaroslaw Konik 2025-05-26 07:00:00 +02:00
parent db573426da
commit b339283901
6 changed files with 14 additions and 12 deletions

View file

@ -1,3 +1,3 @@
def test_func
print('abc' + 5)
raise
end

View file

@ -1,2 +1,2 @@
mark_called
# puts("foo" + 1)
raise

View file

@ -1,5 +1,5 @@
def test_func
rust_func.and_then do |x|
print('abc' + 5)
raise
end
end

View file

@ -6,12 +6,12 @@ use std::{
};
use ::magnus::value::Opaque;
use anyhow::anyhow;
use bevy::{
asset::Asset,
ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel},
math::Vec3,
reflect::TypePath,
tasks::futures_lite::io,
};
use magnus::{
DataType, DataTypeFunctions, IntoValue, Object, RClass, RModule, Ruby, TryConvert, TypedData,
@ -223,7 +223,7 @@ impl TryConvert for BevyVec3 {
impl From<magnus::Error> for ScriptingError {
fn from(value: magnus::Error) -> Self {
ScriptingError::RuntimeError(Box::new(io::Error::other(value.to_string())))
ScriptingError::RuntimeError(anyhow!(value.to_string()).into())
}
}
@ -373,13 +373,15 @@ impl Runtime for RubyRuntime {
var.ivar_set("_current", BevyEntity(entity))
.expect("Failed to set current entity handle");
let value = ruby.eval::<magnus::value::Value>(&script).unwrap();
ruby.eval::<magnus::value::Value>(&script)
.map_err(|e| ScriptingError::RuntimeError(anyhow!(e.to_string()).into()))?;
var.ivar_set("_current", ruby.qnil().as_value())
.expect("Failed to unset current entity handle");
RubyValue::new(value)
}));
Ok(RubyScriptData)
Ok::<Self::ScriptData, ScriptingError>(RubyScriptData)
}))
}
fn register_fn(

View file

@ -1,13 +1,13 @@
use bevy::{prelude::*, log::tracing};
use bevy::{log::tracing, prelude::*};
use std::{
fmt::Display,
sync::{Arc, Mutex},
};
use crate::{
Callback, Callbacks, Runtime, ScriptingError,
callback::FunctionCallEvent,
promise::{Promise, PromiseInner},
Callback, Callbacks, Runtime, ScriptingError,
};
use super::components::Script;

View file

@ -266,7 +266,7 @@ macro_rules! scripting_tests {
|| {},
);
assert_n_times_called!(app, 1);
assert_n_times_called!(app, 2);
}
#[test]