Skip to main content

vlo_kernel/
error.rs

1//! Kernel error codes and constructors.
2//!
3//! The kernel never panics and never returns a bare string: every failure is a
4//! [`KernelError`] with one of the codes in [`code`], so the host can branch on it.
5
6use vlo_types::KernelError;
7
8/// The closed set of kernel error codes.
9pub mod code {
10    /// An export input, an inline payload, a reducer state or a kernel output is not valid JSON.
11    pub const MALFORMED_JSON: &str = "malformed_json";
12    /// The envelope fails a field check (empty id, bad `random`, both payload forms, bad
13    /// `payload_ref` prefix, unroutable kind/status), belongs to another run, omits
14    /// `kernel_state`, still carries an unresolved `payload_ref`, has a bad `delay_ms`, or is
15    /// lifecycle-only but was sent to `reduce_step`. Also `plan_retry` with a bad `random`.
16    pub const INVALID_EVENT: &str = "invalid_event";
17    /// An inline `payload` exceeds [`INLINE_LIMIT_BYTES`](vlo_types::INLINE_LIMIT_BYTES).
18    pub const PAYLOAD_TOO_LARGE: &str = "payload_too_large";
19    /// The run is already closed (completed, failed, cancelled or continued); no further
20    /// events are accepted.
21    pub const RUN_CLOSED: &str = "run_closed";
22    /// `started` arrived on a run whose `last_seq` is not `0`.
23    pub const BAD_SEQ: &str = "bad_seq";
24    /// The event names a step that is not open: never opened, or already pruned as terminal.
25    pub const UNKNOWN_STEP: &str = "unknown_step";
26    /// The step edge is not in the state machine, the step's kind differs from the event's,
27    /// the event is for a future attempt, a retry skips an attempt, a signal lands on a step
28    /// not awaiting one, or `started` arrives on a run that is not `created`.
29    pub const ILLEGAL_TRANSITION: &str = "illegal_transition";
30    /// The event is for an attempt the step has already moved past (a stale duplicate).
31    pub const ALREADY_TERMINAL: &str = "already_terminal";
32    /// A command is malformed: empty activity, workflow, signal name, channel or explicit id,
33    /// or a timer with neither `fire_at_ms` nor `fire_in_ms`.
34    pub const INVALID_COMMAND: &str = "invalid_command";
35    /// A command names a step id that is already open.
36    pub const DUPLICATE_STEP_ID: &str = "duplicate_step_id";
37    /// A reduction issues `complete` / `continue_as_new` more than once.
38    pub const MULTIPLE_COMPLETIONS: &str = "multiple_completions";
39    /// A command follows `complete` / `continue_as_new` in the same reduction.
40    pub const COMMANDS_AFTER_COMPLETION: &str = "commands_after_completion";
41}
42
43/// Result alias every kernel function returns; the error is always a [`KernelError`].
44pub type KResult<T> = Result<T, KernelError>;
45
46/// Build a [`KernelError`] with neither `seq` nor `step_id`.
47pub fn err(code: &str, message: impl Into<String>) -> KernelError {
48    KernelError {
49        code: code.to_string(),
50        message: message.into(),
51        seq: None,
52        step_id: None,
53    }
54}
55
56/// Build a [`KernelError`] tagged with the step it concerns.
57pub fn err_step(code: &str, message: impl Into<String>, step_id: &str) -> KernelError {
58    KernelError {
59        step_id: Some(step_id.to_string()),
60        ..err(code, message)
61    }
62}
63
64/// Build a [`KernelError`] tagged with the seq it concerns.
65pub fn err_seq(code: &str, message: impl Into<String>, seq: u64) -> KernelError {
66    KernelError {
67        seq: Some(seq),
68        ..err(code, message)
69    }
70}