Skip to main content

dig_message/
error.rs

1//! The dig-message error taxonomy — stable, catalogued variants a scripted client can key off (§6.2).
2//! WU1 owns the encoding/framing/compression failures; the seal/signature/replay variants arrive with
3//! WU2/WU4.
4
5use thiserror::Error;
6
7/// Every fail-cleanly outcome of encoding, framing, or (de)compressing a dig-message (SPEC §1, §1.1,
8/// §2). A receiver MUST reject — never panic — on any of these.
9#[derive(Debug, Error, PartialEq, Eq)]
10pub enum MessageError {
11    /// The on-wire frame exceeds [`crate::MAX_ENVELOPE_BYTES`] (SPEC §1).
12    #[error("envelope is {size} bytes, exceeds the {max}-byte cap")]
13    EnvelopeTooLarge { size: usize, max: usize },
14
15    /// The frame ended before a complete envelope could be decoded (SPEC §1).
16    #[error("truncated envelope: {0}")]
17    Truncated(String),
18
19    /// The envelope declares a version this reader does not support (SPEC §2 field 1).
20    #[error("unsupported envelope version {0}")]
21    UnsupportedVersion(u8),
22
23    /// The compression algorithm id is not recognized (SPEC §1.1). Never mis-decode with a wrong codec.
24    #[error("unsupported compression id {0}")]
25    UnsupportedCompression(u8),
26
27    /// The declared uncompressed length exceeds the bomb-guard cap (SPEC §1.1).
28    #[error("declared uncompressed length {declared} exceeds the {max}-byte cap")]
29    DecompressionBomb { declared: usize, max: usize },
30
31    /// The decoded output length did not match the declared `uncompressed_len` (SPEC §1.1).
32    #[error("decompressed length mismatch: expected {expected}, got {actual}")]
33    DecompressedLengthMismatch { expected: usize, actual: usize },
34
35    /// A payload's uncompressed length does not fit the `u32` wire field (SPEC §5.2).
36    #[error("payload is {0} bytes, exceeds the u32 uncompressed_len field")]
37    PayloadTooLarge(usize),
38
39    /// A Streamable (de)serialization or codec-level failure with the underlying detail (SPEC §1).
40    #[error("codec error: {0}")]
41    Codec(String),
42
43    /// The `message_type` is not registered (SPEC §4). Returned when the dispatch shape is a request or
44    /// stream frame so the caller can reply UNSUPPORTED_TYPE; an unknown one-shot/response is instead
45    /// silently dropped (never surfaced as an error — the forward-compat property).
46    #[error("unsupported message type {0:#010x}")]
47    UnsupportedType(u32),
48
49    /// A [`crate::MessageType`] was registered twice (SPEC §4 additive-only: an id, once assigned, is
50    /// never renumbered or repurposed). A duplicate registration is a caller bug, reported — never
51    /// silently overwriting the existing handler.
52    #[error("message type {0:#010x} is already registered")]
53    DuplicateType(u32),
54}
55
56/// The crate result alias.
57pub type Result<T> = std::result::Result<T, MessageError>;