Skip to main content

logit_loom/
error.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Runtime callback and pipeline failures.
4
5use logit_loom_core::{CallbackFailure, CoreError};
6use thiserror::Error;
7
8/// Error returned by a user-defined logit transform.
9#[derive(Clone, Debug, PartialEq, Eq, Error)]
10#[error("{message}")]
11pub struct TransformError {
12    message: String,
13}
14
15impl TransformError {
16    /// Creates an implementation error.
17    pub fn new(message: impl Into<String>) -> Self {
18        Self {
19            message: message.into(),
20        }
21    }
22}
23
24/// Error returned by a generated-token observer.
25#[derive(Clone, Debug, PartialEq, Eq, Error)]
26#[error("{message}")]
27pub struct ObserverError {
28    message: String,
29}
30
31impl ObserverError {
32    /// Creates an observer error.
33    pub fn new(message: impl Into<String>) -> Self {
34        Self {
35            message: message.into(),
36        }
37    }
38}
39
40/// Error returned by a prefill observer.
41#[derive(Clone, Debug, PartialEq, Eq, Error)]
42#[error("{message}")]
43pub struct PrefillObserverError {
44    message: String,
45}
46
47impl PrefillObserverError {
48    /// Creates a prefill observer error.
49    pub fn new(message: impl Into<String>) -> Self {
50        Self {
51            message: message.into(),
52        }
53    }
54}
55
56/// Failure while validating or executing an ordered pipeline.
57#[non_exhaustive]
58#[derive(Debug, Error)]
59pub enum PipelineError {
60    /// A public contract was invalid.
61    #[error(transparent)]
62    Contract(#[from] CoreError),
63    /// A pipeline was used before a successful [`Pipeline::begin`](crate::Pipeline::begin).
64    #[error("pipeline has not begun")]
65    NotBegun,
66    /// A failed pipeline was reused without beginning a fresh call.
67    #[error("pipeline is failed; call begin before reuse")]
68    Failed,
69    /// The caller supplied a sampling step out of sequence.
70    #[error("invalid transform step {actual}; expected {expected}")]
71    InvalidStep {
72        /// Next zero-based step expected by the pipeline.
73        expected: u32,
74        /// Step supplied by the caller.
75        actual: u32,
76    },
77    /// A token was reported without a successful unmatched transform step.
78    #[error("token admission has no unmatched successful transform invocation")]
79    UnexpectedAdmission,
80    /// A new step began before the prior selected token was reported.
81    #[error("previous transform invocation is still awaiting causal admission")]
82    AdmissionPending,
83    /// Candidate arrays disagreed or exceeded a supported bound.
84    #[error("invalid candidate view: {0}")]
85    InvalidCandidates(String),
86    /// Exact mechanical accounting exceeded its integer representation.
87    #[error("pipeline accounting overflowed: {0}")]
88    AccountingOverflow(&'static str),
89    /// One user callback returned an error or panicked.
90    #[error("transform stage {stage} failed: {failure:?}")]
91    Callback {
92        /// Zero-based stage index.
93        stage: usize,
94        /// Contained failure.
95        failure: CallbackFailure,
96    },
97}