Skip to main content

openbutler_protocol/
error.rs

1//! Error types for framing and envelope validation.
2
3/// Frame decoder failure. The message mirrors the TypeScript decoder exactly
4/// so both sides report the same text when a peer breaks the cap.
5#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
6pub enum FrameError {
7    /// A frame (across all its chunks) exceeded the configured limit.
8    #[error("IPC frame exceeded the 32 MB limit.")]
9    ExceededLimit,
10}
11
12/// Envelope validation failure (`isDaemonRequest` / `isBasicResponse`).
13#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
14pub enum ValidationError {
15    /// Frame carried a version this host does not speak.
16    #[error("Unsupported protocol version {actual}; this host speaks {expected}.")]
17    WrongVersion { expected: u32, actual: u32 },
18    /// `id` is empty or longer than 64 JS string length units.
19    #[error("Request id must be 1-64 characters, got {actual}.")]
20    InvalidIdLength { actual: usize },
21    /// `action` is empty or longer than 64 JS string length units.
22    #[error("Action must be 1-64 characters, got {actual}.")]
23    InvalidActionLength { actual: usize },
24    /// A response did not echo the requested version.
25    #[error("Response version {actual} does not echo the requested version {expected}.")]
26    ResponseVersionMismatch { expected: u32, actual: u32 },
27    /// A response carried neither the success nor the failure shape.
28    #[error("Response carries neither a result nor an error.")]
29    MalformedResponse,
30    /// A frame exceeded the JSON node or nesting budget before parsing.
31    #[error("IPC frame exceeded the JSON structure budget.")]
32    JsonBudget,
33    /// A payload string failed `requireString`: missing, empty, not a string,
34    /// or over the byte cap — one fixed message for all four.
35    #[error("{name} must be a non-empty string under {max_bytes} bytes.")]
36    PayloadString {
37        name: &'static str,
38        max_bytes: usize,
39    },
40    /// A payload number failed `requireInteger`: not a JS integer or outside
41    /// the inclusive range — one fixed message for every failure mode.
42    #[error("{name} must be an integer between {min} and {max}.")]
43    PayloadInteger {
44        name: &'static str,
45        min: u64,
46        max: u64,
47    },
48}