Skip to main content

try_register_error_hook

Function try_register_error_hook 

Source
pub fn try_register_error_hook<F>(callback: F) -> Result<(), &'static str>
where F: Fn(ErrorContext<'_>) + Send + Sync + 'static,
Expand description

Attempt to register a callback to be called when errors are created.

The callback may be a function pointer or a closure capturing thread-safe state. Only one hook can be registered per process; subsequent calls return Err("Error hook already registered").

§Example

use error_forge::macros::try_register_error_hook;
use std::sync::{Arc, Mutex};

// Closures that capture state work too — not just function pointers.
let log: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
let log_for_hook = Arc::clone(&log);
let _ = try_register_error_hook(move |ctx| {
    log_for_hook
        .lock()
        .unwrap()
        .push(format!("{}: {}", ctx.kind, ctx.caption));
});