From d5a744b0909c4b2bec397ae4dcd43b56aba355c6 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 19 May 2026 00:12:11 +0200 Subject: feat(ecs): add error handling --- ecs/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'ecs/src/lib.rs') diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 32ade13..667aa0e 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -20,6 +20,12 @@ use crate::component::{ Sequence as ComponentSequence, }; use crate::entity::{Declaration as EntityDeclaration, Handle as EntityHandle}; +use crate::error::{ + ErrorHandler, + Metadata as ErrorMetadata, + SourceKind as ErrorSourceKind, + err_handler_panic, +}; use crate::event::component::Added; use crate::event::{Emitted as EmittedEvent, NewEvents, Submitter as EventSubmitter}; use crate::extension::{Collector as ExtensionCollector, Extension}; @@ -50,6 +56,7 @@ use crate::uid::{Kind as UidKind, Uid}; pub mod actions; pub mod component; pub mod entity; +pub mod error; pub mod event; pub mod extension; pub mod pair; @@ -74,6 +81,7 @@ pub struct World data: WorldData, stop: AtomicBool, is_first_tick: AtomicBool, + error_handler: ErrorHandler, } impl World @@ -85,6 +93,7 @@ impl World data: WorldData::default(), stop: AtomicBool::new(false), is_first_tick: AtomicBool::new(false), + error_handler: err_handler_panic, }; crate::phase::spawn_entities(&mut world); @@ -94,6 +103,11 @@ impl World world } + pub fn set_err_handler(&mut self, err_handler: ErrorHandler) + { + self.error_handler = err_handler; + } + /// Creates a entity with the given components. A new unique [`Uid`] will be generated /// for this entity. pub fn create_entity(&mut self, components: Comps) -> Uid @@ -371,10 +385,20 @@ impl World }; // SAFETY: The world lives long enough - unsafe { + if let Err(err) = unsafe { system .system - .run(self, SystemMetadata { ent_id: system_entity.uid() }); + .run(self, SystemMetadata { ent_id: system_entity.uid() }) + } { + cold_path(); + + (self.error_handler)( + err, + ErrorMetadata { + source_name: system.system.name(), + source_kind: ErrorSourceKind::System, + }, + ) } } } @@ -570,12 +594,22 @@ impl World ); for (observer_ent_id, (observer,)) in query.iter_with_euids() { - unsafe { + if let Err(err) = unsafe { observer.run( self, SystemMetadata { ent_id: observer_ent_id }, emitted_event.clone(), - ); + ) + } { + cold_path(); + + (self.error_handler)( + err, + ErrorMetadata { + source_name: observer.name(), + source_kind: ErrorSourceKind::Observer, + }, + ) } } } -- cgit v1.2.3-18-g5258