Skip to main content

collect_diagnostic

Function collect_diagnostic 

pub fn collect_diagnostic<R>(f: impl FnOnce() -> R) -> Result<R, Diagnostic>
Expand description

Runs f with a diagnostic sink installed.

An error that would normally print a report and exit/panic instead records a Diagnostic and unwinds back here. Nested calls are supported (the previous state is restored), and panics that signal bugs are not swallowed; they propagate through unchanged.

§Requires

An unwinding panic strategy (panic = "unwind"). This function catches the internal unwind used to carry a diagnostic out of the error funnels via std::panic::catch_unwind. Under panic = "abort", which is candela’s own [profile.release] default, that unwind cannot be caught: the process aborts on the first error instead of this returning Err. Embedders that rely on collect_diagnostic therefore must build with panic = "unwind" (see the embed profile in candela’s Cargo.toml). The CLI never calls this, so its behavior is unaffected either way.

While a collection is active this installs a process-global panic hook that silences only the internal diagnostic unwind and forwards every other panic to the host’s own hook. The host’s hook is captured on entry and restored once the outermost collection finishes, so it is never permanently replaced.

§Errors

Returns the Diagnostic recorded by the first error funnel that fired while f was running (parser, compiler or runtime).