Skip to main content

lean_rs/error/
mod.rs

1//! Typed error boundary for the safe `lean-rs` surface.
2//!
3//! Every fallible public function returns [`LeanResult<T>`]. [`LeanError`]
4//! is the single error type that crosses the boundary; it has three
5//! variants:
6//!
7//! - [`LeanError::LeanException`] when Lean threw through its `IO` error
8//!   channel. The payload reports the `IO.Error` constructor as
9//!   [`LeanExceptionKind`] and a bounded `message()` that callers may
10//!   surface to end users.
11//! - [`LeanError::Host`] when our stack failed (init, ABI conversion,
12//!   contained callback panic, future link/load, internal invariant). The
13//!   payload reports the [`HostStage`] and a bounded developer-facing
14//!   `message()`.
15//! - [`LeanError::Cancelled`] when a `lean-rs-host` cooperative
16//!   cancellation token was observed before an FFI dispatch or between
17//!   bulk dispatches.
18//!
19//! Bounded messages are a *structural* invariant: [`LeanException`] and
20//! [`HostFailure`] have private fields, and the only constructors are
21//! `pub(crate)`; both run a shared helper that truncates the message at
22//! [`LEAN_ERROR_MESSAGE_LIMIT`] on a UTF-8 char boundary. [`LeanCancelled`]
23//! also has private fields and carries a fixed host-authored message.
24//! External callers receive `LeanError` values but cannot mint one with
25//! an unbounded message.
26//!
27//! The rule callers learn: **runtime / host failures are [`LeanError`];
28//! application semantics are values.** A Lean function returning
29//! `IO (Except E T)` decodes as `LeanResult<Result<T, E>>` — outer `IO`
30//! failure becomes a [`LeanError::LeanException`], inner `Except`
31//! becomes a Rust [`Result`].
32//!
33//! ## Diagnostic codes
34//!
35//! Every error-bearing type on the public surface projects to a stable
36//! [`LeanDiagnosticCode`] via `.code()`. The code names the failure
37//! family a downstream caller must react to — `Linking`, `Elaboration`,
38//! `Unsupported`, and so on — independent of the internal [`HostStage`]
39//! tag. The `as_str()` form of each code is the identifier used by the
40//! tracing spans and the published `docs/diagnostics.md` catalogue;
41//! variant names and string ids are stable across patch releases.
42
43use std::any::Any;
44use std::fmt;
45
46pub(crate) mod capture;
47pub(crate) mod io;
48pub(crate) mod panic;
49pub(crate) mod redact;
50
51#[cfg(test)]
52mod tests;
53
54pub use self::capture::{CapturedEvent, DIAGNOSTIC_CAPTURE_DEFAULT_CAPACITY, DiagnosticCapture};
55
56/// Hard cap on the byte length of any [`LeanError`] message.
57///
58/// Enforced at construction time by the `pub(crate)` constructors in this
59/// module. The truncation respects UTF-8 char boundaries, so the cap is
60/// an upper bound rather than an exact length.
61pub const LEAN_ERROR_MESSAGE_LIMIT: usize = 4096;
62
63/// Result alias used by every fallible public API in `lean-rs`.
64pub type LeanResult<T> = Result<T, LeanError>;
65
66/// Errors reported across the safe `lean-rs` boundary.
67#[derive(Clone, Debug)]
68pub enum LeanError {
69    /// Lean threw through its `IO` error channel; see [`LeanException`].
70    LeanException(LeanException),
71    /// The host stack failed at a particular stage; see [`HostFailure`].
72    Host(HostFailure),
73    /// A cooperative cancellation token was observed before dispatch.
74    Cancelled(LeanCancelled),
75}
76
77impl LeanError {
78    /// Project to the stable [`LeanDiagnosticCode`] taxonomy.
79    ///
80    /// `LeanException` always maps to [`LeanDiagnosticCode::LeanException`];
81    /// `Host` returns the code recorded by the construction site.
82    #[must_use]
83    pub fn code(&self) -> LeanDiagnosticCode {
84        match self {
85            Self::LeanException(_) => LeanDiagnosticCode::LeanException,
86            Self::Host(failure) => failure.code,
87            Self::Cancelled(_) => LeanDiagnosticCode::Cancelled,
88        }
89    }
90
91    /// Build a `RuntimeInit` host failure.
92    pub(crate) fn runtime_init(message: impl Into<String>) -> Self {
93        Self::host(HostStage::RuntimeInit, LeanDiagnosticCode::RuntimeInit, message)
94    }
95
96    /// Build a `RuntimeInit` host failure from a caught panic payload.
97    pub(crate) fn runtime_init_panic(payload: &(dyn Any + Send)) -> Self {
98        Self::runtime_init(render_panic_payload(payload))
99    }
100
101    /// Build a `RuntimeInit` host failure for an active Lean toolchain that
102    /// is not in [`lean_rs_sys::SUPPORTED_TOOLCHAINS`]. The build script
103    /// already filters at compile time; this constructor exists for the
104    /// runtime backstop in [`crate::runtime::LeanRuntime::init`].
105    pub(crate) fn runtime_init_unsupported_toolchain(active_version: &str) -> Self {
106        use std::fmt::Write as _;
107        let mut window = String::new();
108        for (i, t) in lean_rs_sys::SUPPORTED_TOOLCHAINS.iter().enumerate() {
109            if i > 0 {
110                window.push_str(", ");
111            }
112            let _ = write!(window, "{:?}", t.versions);
113        }
114        Self::runtime_init(format!(
115            "active Lean toolchain {active_version} is not in the supported window: {window}",
116        ))
117    }
118
119    /// Build a `Linking` host failure (missing/invalid Lake names,
120    /// missing initializer symbol, header-digest mismatch).
121    pub(crate) fn linking(message: impl Into<String>) -> Self {
122        Self::host(HostStage::Link, LeanDiagnosticCode::Linking, message)
123    }
124
125    /// Build a `ModuleInit` host failure (dylib could not be opened,
126    /// initializer raised, Lake project path bad).
127    pub(crate) fn module_init(message: impl Into<String>) -> Self {
128        Self::host(HostStage::Load, LeanDiagnosticCode::ModuleInit, message)
129    }
130
131    /// Build a `ModuleInit` host failure from a caught panic payload.
132    pub(crate) fn module_init_panic(payload: &(dyn Any + Send)) -> Self {
133        Self::module_init(render_panic_payload(payload))
134    }
135
136    /// Build a `SymbolLookup` host failure (dlsym miss, signature
137    /// mismatch — function symbol expected but global found, etc.).
138    pub(crate) fn symbol_lookup(message: impl Into<String>) -> Self {
139        Self::host(HostStage::Link, LeanDiagnosticCode::SymbolLookup, message)
140    }
141
142    /// Build an `AbiConversion` host failure (wrong Lean kind, integer
143    /// out of range, invalid UTF-8, missing declaration).
144    pub(crate) fn abi_conversion(message: impl Into<String>) -> Self {
145        Self::host(HostStage::Conversion, LeanDiagnosticCode::AbiConversion, message)
146    }
147
148    /// Build a Lean-thrown-exception report with a bounded message.
149    pub(crate) fn lean_exception(kind: LeanExceptionKind, message: impl Into<String>) -> Self {
150        Self::LeanException(LeanException {
151            kind,
152            message: bound_message(message.into()),
153        })
154    }
155
156    /// Build a host-stack failure from a caught `std::panic::catch_unwind`
157    /// payload at a Lean → Rust callback boundary. The payload is
158    /// rendered into a string before bounding so the panic value never
159    /// escapes the error boundary.
160    pub(crate) fn callback_panic(payload: &(dyn Any + Send)) -> Self {
161        Self::host(
162            HostStage::CallbackPanic,
163            LeanDiagnosticCode::Internal,
164            render_panic_payload(payload),
165        )
166    }
167
168    /// Build an internal host failure for a `pub(crate)` invariant breach.
169    pub(crate) fn internal(message: impl Into<String>) -> Self {
170        Self::host(HostStage::Internal, LeanDiagnosticCode::Internal, message)
171    }
172
173    /// Build a cooperative cancellation report.
174    pub(crate) fn cancelled() -> Self {
175        Self::Cancelled(LeanCancelled {
176            message: "operation cancelled by LeanCancellationToken".to_owned(),
177        })
178    }
179
180    /// Shared host-failure constructor. Private — every call site uses
181    /// the typed wrappers above so the [`HostStage`] / [`LeanDiagnosticCode`]
182    /// pair cannot drift.
183    fn host(stage: HostStage, code: LeanDiagnosticCode, message: impl Into<String>) -> Self {
184        Self::Host(HostFailure {
185            stage,
186            code,
187            message: bound_message(message.into()),
188        })
189    }
190}
191
192impl fmt::Display for LeanError {
193    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194        match self {
195            Self::LeanException(e) => write!(f, "lean-rs: {e}"),
196            Self::Host(e) => write!(f, "lean-rs: {e}"),
197            Self::Cancelled(e) => write!(f, "lean-rs: {e}"),
198        }
199    }
200}
201
202impl std::error::Error for LeanError {}
203
204/// A cooperative cancellation observed by `lean-rs-host`.
205///
206/// Constructed only by the crate through the hidden host boundary helper.
207/// The payload is intentionally small and stable: cancellation is a caller
208/// decision, not a Lean diagnostic.
209#[derive(Clone, Debug)]
210pub struct LeanCancelled {
211    message: String,
212}
213
214impl LeanCancelled {
215    /// Caller-facing cancellation message.
216    #[must_use]
217    pub fn message(&self) -> &str {
218        &self.message
219    }
220}
221
222impl fmt::Display for LeanCancelled {
223    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
224        f.write_str(&self.message)
225    }
226}
227
228/// A Lean exception thrown through the `IO` error channel.
229///
230/// Constructed only by the crate; the `kind` and `message` fields are
231/// private so the bounded-message invariant survives downstream
232/// pattern-matching.
233#[derive(Clone, Debug)]
234pub struct LeanException {
235    kind: LeanExceptionKind,
236    message: String,
237}
238
239impl LeanException {
240    /// The `IO.Error` constructor Lean reported.
241    ///
242    /// Use this to classify a Lean-thrown exception when the
243    /// caller-facing taxonomy in [`LeanDiagnosticCode`] is not specific
244    /// enough — for example, distinguishing `FileNotFound` from
245    /// `PermissionDenied` to drive different recovery paths.
246    #[must_use]
247    pub fn kind(&self) -> LeanExceptionKind {
248        self.kind
249    }
250
251    /// What Lean said about the failure, truncated to at most
252    /// [`LEAN_ERROR_MESSAGE_LIMIT`] bytes on a UTF-8 char boundary.
253    #[must_use]
254    pub fn message(&self) -> &str {
255        &self.message
256    }
257}
258
259impl fmt::Display for LeanException {
260    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
261        write!(f, "Lean threw {:?}: {}", self.kind, self.message)
262    }
263}
264
265/// A host-stack failure observed inside `lean-rs`.
266///
267/// Constructed only by the crate; fields are private so the bounded-message
268/// invariant survives downstream pattern-matching.
269#[derive(Clone, Debug)]
270pub struct HostFailure {
271    stage: HostStage,
272    code: LeanDiagnosticCode,
273    message: String,
274}
275
276impl HostFailure {
277    /// The host-stack stage that observed the failure.
278    ///
279    /// This is the *internal* classification; reach for
280    /// [`Self::code`] when displaying a stable identifier or routing on
281    /// the caller-facing failure family. The [`HostStage`] tag may grow
282    /// new variants alongside new internal paths.
283    #[must_use]
284    pub fn stage(&self) -> HostStage {
285        self.stage
286    }
287
288    /// The stable diagnostic code matching this failure.
289    ///
290    /// Recorded at the construction site rather than projected from
291    /// [`Self::stage`], so the code identity does not drift if the
292    /// internal stage tag is later refined. Use this for stable
293    /// logging, metrics, or downstream error routing.
294    #[must_use]
295    pub fn code(&self) -> LeanDiagnosticCode {
296        self.code
297    }
298
299    /// Developer-facing diagnostic, truncated to at most
300    /// [`LEAN_ERROR_MESSAGE_LIMIT`] bytes on a UTF-8 char boundary.
301    #[must_use]
302    pub fn message(&self) -> &str {
303        &self.message
304    }
305}
306
307impl fmt::Display for HostFailure {
308    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
309        write!(
310            f,
311            "host stage {:?} [{}]: {}",
312            self.stage,
313            self.code.as_str(),
314            self.message
315        )
316    }
317}
318
319/// What the host stack was doing when it failed.
320///
321/// A flat tag enum; callers rarely match on it and read
322/// [`HostFailure::message`] instead. Use [`LeanDiagnosticCode`] for the
323/// stable, caller-facing failure taxonomy — `HostStage` is the
324/// host-stack's internal classification and may grow new variants when
325/// new internal paths are added.
326#[derive(Copy, Clone, Debug, Eq, PartialEq)]
327pub enum HostStage {
328    /// `OnceLock` + `lean_initialize_*` panic-or-failure.
329    RuntimeInit,
330    /// First-order ABI value malformed (wrong kind, out of range,
331    /// invalid UTF-8, non-scalar `char`).
332    Conversion,
333    /// A Rust panic was contained at a Lean → Rust callback boundary.
334    CallbackPanic,
335    /// Link-time failure (missing symbol, header-digest mismatch).
336    Link,
337    /// Load-time failure (`dlopen`, per-module initializer).
338    Load,
339    /// A `pub(crate)` invariant tripped. Indicates a bug in `lean-rs`.
340    Internal,
341}
342
343/// Stable, caller-facing classification of a `lean-rs` failure.
344///
345/// Every error-bearing public type projects to one of these via
346/// `.code()`:
347///
348/// - [`LeanError::code`] — `LeanException` → [`LeanDiagnosticCode::LeanException`],
349///   `Host` → the code recorded by the construction site.
350/// - `lean_rs_host::LeanElabFailure::code` — always [`LeanDiagnosticCode::Elaboration`].
351/// - `lean_rs_host::meta::LeanMetaResponse::code` — `Ok` → `None`,
352///   `Failed` / `TimeoutOrHeartbeat` → `Elaboration`, `Unsupported` →
353///   `Unsupported`.
354///
355/// Use `match err.code() { Linking => ..., ModuleInit => ..., _ => ... }`
356/// to react by family; reach for [`HostStage`] only when you need the
357/// host-stack's internal classification (and accept that it may grow new
358/// variants). The string form returned by [`Self::as_str`] is also the
359/// identifier emitted in tracing fields and listed in
360/// `docs/diagnostics.md`. Variant names and `as_str()` ids are stable
361/// across patch releases.
362#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
363pub enum LeanDiagnosticCode {
364    /// Lean runtime initialization failed (panic in `lean_initialize`,
365    /// thread-attach floor failure, task-manager init failure).
366    RuntimeInit,
367    /// A linkable artefact was missing or did not match: a Lake
368    /// package/module name was invalid, the initializer symbol was
369    /// absent, or the header digest did not match the active toolchain.
370    Linking,
371    /// A capability dylib could not be opened, parsed, or its root
372    /// module initializer raised. Also: the Lake project root did not
373    /// exist or was not a directory.
374    ModuleInit,
375    /// A function or global symbol was not present in the loaded dylib
376    /// when a session call tried to resolve it.
377    SymbolLookup,
378    /// An ABI conversion failed: wrong Lean kind for the requested
379    /// Rust type, integer out of range, invalid UTF-8, or a queried
380    /// declaration was missing from the imported environment.
381    AbiConversion,
382    /// Lean raised through its `IO` error channel. Inspect
383    /// [`LeanException::kind`] for the `IO.Error` constructor.
384    LeanException,
385    /// Term parsing or elaboration produced one or more diagnostics.
386    /// The payload is a `lean_rs_host::LeanElabFailure` with the typed
387    /// diagnostic list.
388    Elaboration,
389    /// The loaded capability does not expose the requested service —
390    /// either the Lean shim returned `unsupported` for the request
391    /// shape, or the optional capability symbol was absent at load
392    /// time.
393    Unsupported,
394    /// A cooperative cancellation token was observed before dispatch.
395    Cancelled,
396    /// A `pub(crate)` invariant tripped, or a callback panicked inside
397    /// the safe boundary. Indicates a bug in `lean-rs`.
398    Internal,
399}
400
401impl LeanDiagnosticCode {
402    /// Stable identifier used in tracing fields and `docs/diagnostics.md`.
403    ///
404    /// The returned string is part of the published API; the value for
405    /// an existing variant is fixed across patch releases. New variants
406    /// may add new ids.
407    #[must_use]
408    pub const fn as_str(self) -> &'static str {
409        match self {
410            Self::RuntimeInit => "lean_rs.runtime_init",
411            Self::Linking => "lean_rs.linking",
412            Self::ModuleInit => "lean_rs.module_init",
413            Self::SymbolLookup => "lean_rs.symbol_lookup",
414            Self::AbiConversion => "lean_rs.abi_conversion",
415            Self::LeanException => "lean_rs.lean_exception",
416            Self::Elaboration => "lean_rs.elaboration",
417            Self::Unsupported => "lean_rs.unsupported",
418            Self::Cancelled => "lean_rs.cancelled",
419            Self::Internal => "lean_rs.internal",
420        }
421    }
422
423    /// One-line prose description used in `docs/diagnostics.md` and as
424    /// the default fallback when a span needs human-readable context.
425    #[must_use]
426    pub const fn description(self) -> &'static str {
427        match self {
428            Self::RuntimeInit => "Lean runtime initialization failed",
429            Self::Linking => "a linkable artefact was missing or mismatched",
430            Self::ModuleInit => "a capability dylib could not be opened or initialized",
431            Self::SymbolLookup => "a symbol was not present in the loaded dylib",
432            Self::AbiConversion => "an ABI conversion failed",
433            Self::LeanException => "Lean raised through its IO error channel",
434            Self::Elaboration => "term parsing or elaboration produced diagnostics",
435            Self::Unsupported => "the loaded capability does not expose the requested service",
436            Self::Cancelled => "a cooperative cancellation token was observed before dispatch",
437            Self::Internal => "a pub(crate) invariant tripped — likely a bug in lean-rs",
438        }
439    }
440}
441
442impl fmt::Display for LeanDiagnosticCode {
443    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
444        f.write_str(self.as_str())
445    }
446}
447
448/// Constructor tag for Lean's `IO.Error`.
449///
450/// 1:1 with `IO.Error` at the active Lean toolchain version (4.29.1 —
451/// see `src/lean/Init/System/IOError.lean`), plus an [`Other`] catch-all
452/// for tag values not in the declaration. The crate-internal decoder
453/// maps the raw `lean_obj_tag` to a variant via a `const` table; a unit
454/// test anchors the `userError` mapping against the live Lean runtime
455/// and will fail if the constructor index drifts.
456///
457/// [`Other`]: Self::Other
458#[derive(Copy, Clone, Debug, Eq, PartialEq)]
459pub enum LeanExceptionKind {
460    /// `IO.Error.alreadyExists`
461    AlreadyExists,
462    /// `IO.Error.otherError`
463    OtherError,
464    /// `IO.Error.resourceBusy`
465    ResourceBusy,
466    /// `IO.Error.resourceVanished`
467    ResourceVanished,
468    /// `IO.Error.unsupportedOperation`
469    UnsupportedOperation,
470    /// `IO.Error.hardwareFault`
471    HardwareFault,
472    /// `IO.Error.unsatisfiedConstraints`
473    UnsatisfiedConstraints,
474    /// `IO.Error.illegalOperation`
475    IllegalOperation,
476    /// `IO.Error.protocolError`
477    ProtocolError,
478    /// `IO.Error.timeExpired`
479    TimeExpired,
480    /// `IO.Error.interrupted`
481    Interrupted,
482    /// `IO.Error.noFileOrDirectory`
483    NoFileOrDirectory,
484    /// `IO.Error.invalidArgument`
485    InvalidArgument,
486    /// `IO.Error.permissionDenied`
487    PermissionDenied,
488    /// `IO.Error.resourceExhausted`
489    ResourceExhausted,
490    /// `IO.Error.inappropriateType`
491    InappropriateType,
492    /// `IO.Error.noSuchThing`
493    NoSuchThing,
494    /// `IO.Error.unexpectedEof`
495    UnexpectedEof,
496    /// `IO.Error.userError`
497    UserError,
498    /// Tag did not match any known `IO.Error` constructor at the active
499    /// Lean toolchain version.
500    Other,
501}
502
503/// Truncate `s` to at most [`LEAN_ERROR_MESSAGE_LIMIT`] bytes on a UTF-8
504/// char boundary. The single place every constructor enforces the bound.
505///
506/// `#[doc(hidden)] pub` so the sibling `lean-rs-host` crate can apply the
507/// same bound when it constructs `LeanError::Host(HostFailure)` values
508/// through the `__host_internals` helpers. Re-exported through
509/// [`crate::__host_internals`]; not part of the public semver promise.
510#[doc(hidden)]
511pub fn bound_message(mut s: String) -> String {
512    if s.len() <= LEAN_ERROR_MESSAGE_LIMIT {
513        return s;
514    }
515    // Walk to the largest char boundary at or below the limit, then
516    // truncate. `floor_char_boundary` is unstable; do it manually with a
517    // single backward scan from the limit. `saturating_sub` keeps clippy's
518    // arithmetic-side-effects lint quiet without changing semantics — the
519    // loop guard already prevents `cut == 0` from underflowing.
520    let mut cut = LEAN_ERROR_MESSAGE_LIMIT;
521    while cut > 0 && !s.is_char_boundary(cut) {
522        cut = cut.saturating_sub(1);
523    }
524    s.truncate(cut);
525    s
526}
527
528// -- L1 → L2 boundary helpers -----------------------------------------
529//
530// `LeanError`'s constructors are `pub(crate)` to preserve the structural
531// bounding invariant: external crates cannot mint `LeanError` values
532// directly. The sibling `lean-rs-host` crate needs to construct host
533// failures and cooperative cancellation reports when it dispatches
534// capability shims; it reaches the constructors it actually uses through
535// this `#[doc(hidden)] pub fn` wrapper, re-exported at
536// [`crate::__host_internals`].
537//
538// The only wrappers live here are those a real call site needs. Add another the same way
539// (single-call wrapper + re-export in `crate::__host_internals`) only
540// when a future call site needs one.
541
542/// Construct a `ModuleInit` host failure. See [`LeanError::module_init`].
543#[doc(hidden)]
544pub fn host_module_init(message: impl Into<String>) -> LeanError {
545    LeanError::module_init(message)
546}
547
548/// Construct a cooperative cancellation report. See [`LeanError::cancelled`].
549#[doc(hidden)]
550pub fn host_cancelled() -> LeanError {
551    LeanError::cancelled()
552}
553
554/// Construct a host-internal failure. See [`LeanError::internal`].
555#[doc(hidden)]
556pub fn host_internal(message: impl Into<String>) -> LeanError {
557    LeanError::internal(message)
558}
559
560/// Construct a callback-panic host failure. See [`LeanError::callback_panic`].
561#[doc(hidden)]
562pub fn host_callback_panic(payload: &(dyn Any + Send)) -> LeanError {
563    LeanError::callback_panic(payload)
564}
565
566/// Render an arbitrary panic payload as a human-readable string. Strings
567/// (both `&'static str` and `String`) come through verbatim; other types
568/// collapse to a generic placeholder. Centralised so the
569/// `HostStage::RuntimeInit` and `HostStage::CallbackPanic` sites stay
570/// consistent.
571fn render_panic_payload(payload: &(dyn Any + Send)) -> String {
572    if let Some(s) = payload.downcast_ref::<&'static str>() {
573        (*s).to_owned()
574    } else if let Some(s) = payload.downcast_ref::<String>() {
575        s.clone()
576    } else {
577        "panic payload was not a string".to_owned()
578    }
579}