From 18c5c3fd34ceb8a1631f7766b69e407cf92e1a09 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 15 May 2026 21:54:11 +0800 Subject: Add panic catch for program execution --- mingling_core/src/program/hook.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'mingling_core/src/program/hook.rs') diff --git a/mingling_core/src/program/hook.rs b/mingling_core/src/program/hook.rs index afc253c..080c00f 100644 --- a/mingling_core/src/program/hook.rs +++ b/mingling_core/src/program/hook.rs @@ -1,6 +1,6 @@ use std::any::Any; -use crate::{AnyOutput, Program, ProgramCollect, RenderResult}; +use crate::{AnyOutput, Program, ProgramCollect, RenderResult, error::ProgramPanic}; #[derive(Default)] pub struct ProgramHook @@ -30,6 +30,9 @@ where /// Executes before the program ends pub finish: Option i32>, + + /// Executes when the program panics + pub exec_panic: Option, } impl Program @@ -126,6 +129,18 @@ where } } + pub(crate) fn run_hook_exec_panic(&self, panic_info: &ProgramPanic) { + if !self.user_context.run_hook { + return; + } + + for hook in &self.hooks { + if let Some(exec_panic) = hook.exec_panic { + exec_panic(panic_info) + } + } + } + pub(crate) fn run_hook_finish(&self) -> i32 { if !self.user_context.run_hook { return 0; @@ -159,6 +174,7 @@ where pre_render: None, post_render: None, finish: None, + exec_panic: None, } } @@ -209,4 +225,10 @@ where let _ = self.finish.insert(handler); self } + + /// Sets the handler for the `exec_panic` event. + pub fn on_exec_panic(mut self, handler: fn(&ProgramPanic)) -> Self { + let _ = self.exec_panic.insert(handler); + self + } } -- cgit