pcapsql_core/
error.rs

1//! Error types for pcapsql-core.
2//!
3//! This module provides structured error types for all pcapsql-core operations:
4//!
5//! - [`enum@Error`] - Main error enum that wraps all error types
6//! - [`PcapError`] - Errors from PCAP file reading
7//! - [`ProtocolError`] - Errors from protocol parsing
8//!
9//! All errors implement `std::error::Error` and can be converted to `anyhow::Error`.
10
11use thiserror::Error;
12
13/// Main error type for pcapsql-core operations.
14#[derive(Error, Debug)]
15pub enum Error {
16    /// Error reading or parsing PCAP file
17    #[error("PCAP error: {0}")]
18    Pcap(#[from] PcapError),
19
20    /// Error during protocol parsing
21    #[error("Protocol parse error: {0}")]
22    Protocol(#[from] ProtocolError),
23
24    /// I/O error
25    #[error("I/O error: {0}")]
26    Io(#[from] std::io::Error),
27}
28
29/// Errors related to PCAP file reading.
30#[derive(Error, Debug)]
31pub enum PcapError {
32    /// File not found
33    #[error("File not found: {path}")]
34    FileNotFound { path: String },
35
36    /// Invalid PCAP format
37    #[error("Invalid PCAP format: {reason}")]
38    InvalidFormat { reason: String },
39
40    /// Unsupported link type
41    #[error("Unsupported link type: {link_type}")]
42    UnsupportedLinkType { link_type: u16 },
43
44    /// Truncated packet
45    #[error("Truncated packet at frame {frame}: expected {expected} bytes, got {actual}")]
46    TruncatedPacket {
47        frame: u64,
48        expected: usize,
49        actual: usize,
50    },
51}
52
53/// Errors related to protocol parsing.
54#[derive(Error, Debug)]
55pub enum ProtocolError {
56    /// Packet too short for protocol header
57    #[error("{protocol}: packet too short (need {needed} bytes, have {have})")]
58    PacketTooShort {
59        protocol: &'static str,
60        needed: usize,
61        have: usize,
62    },
63
64    /// Invalid header field value
65    #[error("{protocol}: invalid {field}: {reason}")]
66    InvalidField {
67        protocol: &'static str,
68        field: &'static str,
69        reason: String,
70    },
71
72    /// Checksum mismatch
73    #[error("{protocol}: checksum mismatch (expected {expected:#x}, got {actual:#x})")]
74    ChecksumMismatch {
75        protocol: &'static str,
76        expected: u16,
77        actual: u16,
78    },
79}
80
81/// Result type alias using our Error type.
82pub type Result<T> = std::result::Result<T, Error>;