Skip to main content

pcap_processor/
error.rs

1use std::path::PathBuf;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum PcapError {
6    #[error("tshark not found. Install Wireshark or set PCAP_PROCESSOR_TSHARK")]
7    TsharkNotFound,
8
9    #[error("failed to execute tshark: {0}")]
10    TsharkExecution(#[from] std::io::Error),
11
12    #[error("tshark failed: {message}")]
13    TsharkFailed { message: String },
14
15    #[error("invalid display filter: {filter}\n{message}")]
16    InvalidFilter { filter: String, message: String },
17
18    #[error("invalid field: {field}")]
19    InvalidField { field: String },
20
21    #[error("file not found: {path}")]
22    FileNotFound { path: PathBuf },
23
24    #[error("no files matched pattern: {pattern}")]
25    NoFilesMatched { pattern: String },
26
27    #[error("failed to read file: {path}: {message}")]
28    FileRead { path: PathBuf, message: String },
29
30    #[error("output format error: {0}")]
31    OutputFormat(String),
32}
33
34impl PcapError {
35    pub fn exit_code(&self) -> i32 {
36        match self {
37            PcapError::TsharkNotFound => 2,
38            PcapError::TsharkExecution(_) => 5,
39            PcapError::TsharkFailed { .. } => 5,
40            PcapError::InvalidFilter { .. } => 4,
41            PcapError::InvalidField { .. } => 4,
42            PcapError::FileNotFound { .. } => 3,
43            PcapError::NoFilesMatched { .. } => 3,
44            PcapError::FileRead { .. } => 3,
45            PcapError::OutputFormat(_) => 1,
46        }
47    }
48}
49
50pub type Result<T> = std::result::Result<T, PcapError>;