{"record":{"id":"386e72cfad031c4d","repo":"GyulyVGC/sniffnet","slug":"e-to-string","errorCode":null,"errorMessage":"e.to_string()","messagePattern":"e\\.to_string\\(\\)","errorType":"exception","errorClass":"CaptureContext::Error","httpStatus":null,"severity":"critical","filePath":"src/networking/types/capture_context.rs","lineNumber":24,"sourceCode":"use crate::translations::translations::network_adapter_translation;\nuse crate::translations::translations_4::capture_file_translation;\nuse crate::translations::types::language::Language;\nuse crate::utils::error_logger::{ErrorLogger, Location};\nuse pcap::{Active, Address, Capture, Device, Error, Packet, Savefile, Stat};\nuse serde::{Deserialize, Serialize};\n\npub enum CaptureContext {\n    Live(Live),\n    LiveWithSavefile(LiveWithSavefile),\n    Offline(Offline),\n    Error(String),\n}\n\nimpl CaptureContext {\n    pub fn new(source: &CaptureSource, pcap_out_path: Option<&String>, filters: &Filters) -> Self {\n        let mut cap_type = match CaptureType::from_source(source, pcap_out_path) {\n            Ok(c) => c,\n            Err(e) => return Self::Error(e.to_string()),\n        };\n\n        // only apply BPF filter if it is active, and return an error if it fails to apply\n        if filters.is_some_filter_active()\n            && let Err(e) = cap_type.set_bpf(filters.bpf())\n        {\n            return Self::Error(e.to_string());\n        }\n\n        let cap = match cap_type {\n            CaptureType::Live(cap) => cap,\n            CaptureType::Offline(cap) => return Self::new_offline(cap),\n        };\n\n        if let Some(out_path) = pcap_out_path {\n            let savefile_res = cap.savefile(out_path);\n            match savefile_res {\n                Ok(s) => Self::new_live_with_savefile(cap, s),","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/GyulyVGC/sniffnet/blob/48b0575dc0d3c7e503a466a3cb2c1466ba600f8e/src/networking/types/capture_context.rs#L6-L42","documentation":"In CaptureContext::new (src/networking/types/capture_context.rs:24), the first fallible step is CaptureType::from_source(source, pcap_out_path). On Err the enum degrades to CaptureContext::Error(e.to_string()), storing the raw error text for later display (it becomes sniffer.pcap_error shown under the 'An error occurred!' banner). This covers all failures of opening the capture itself: Capture::from_device(...).open()? for live devices and Capture::from_file(...)? for offline pcaps.","triggerScenarios":"Live: opening the selected device with pcap fails — no such device (adapter renamed/removed since the list was built), permission denied opening the capture handle (non-root without CAP_NET_RAW, Windows without Npcap or without admin), device busy. Offline: Capture::from_file fails — file path doesn't exist, isn't a valid pcap/pcapng, or is unreadable. Result: no Capture variant is built; the GUI waiting page shows the stored string.","commonSituations":"Running Sniffnet without privileges on Linux/macOS; missing Npcap on Windows; adapter unplugged between listing and selection; double-clicking a corrupted/truncated .pcapng as capture source; file with wrong extension content (renamed text file); path with characters the pcap open rejects; stale settings remembering a device name that no longer exists (config_device.rs falls back to a synthetic 'default' device when Device::lookup() fails).","solutions":["Read the stored error string on the waiting page — pcap messages distinguish 'no such device', 'permissions problem', and invalid file.","For permission errors: run with sudo / grant CAP_NET_RAW (`setcap cap_net_raw+ep $(which sniffnet)`), on Windows run as admin and ensure Npcap with WinPcap-compatible API is installed.","For device errors: reopen the adapter picker and select a currently present adapter; clear the remembered device in settings.","For file errors: verify the path exists and is a real pcap/pcapng (`capinfos file.pcap` or open in Wireshark); fix the path/extension.","If a savefile is also configured, make sure that path is writable too (error 9 fires next otherwise)."],"exampleFix":"// before — error text stored and only surfaced in the GUI\nlet mut cap_type = match CaptureType::from_source(source, pcap_out_path) {\n    Ok(c) => c,\n    Err(e) => return Self::Error(e.to_string()),\n};\n\n// after — classify common root causes before storing the message\nlet cap_type = match CaptureType::from_source(source, pcap_out_path) {\n    Ok(c) => c,\n    Err(e) => {\n        let hint = if e.to_string().contains(\"permission\") {\n            format!(\"{e} (try running with elevated privileges)\")\n        } else {\n            e.to_string()\n        };\n        return Self::Error(hint);\n    }\n};","handlingStrategy":"try-catch","validationCode":"// preflight the capture source before building the CaptureContext\nfn source_usable(source: &CaptureSource) -> Option<String> {\n    match source {\n        CaptureSource::Device(dev) => {\n            let exists = pcap::Device::list()\n                .map(|list| list.iter().any(|d| d.name == dev.name))\n                .unwrap_or(false);\n            if exists { None } else { Some(format!(\"device {} not present\", dev.name)) }\n        }\n        CaptureSource::File(f) => std::fs::metadata(&f.path)\n            .err()\n            .map(|e| format!(\"pcap file {}: {e}\", f.path)),\n    }\n}","typeGuard":null,"tryCatchPattern":"// handle the degraded enum instead of assuming a live capture\nlet ctx = CaptureContext::new(source, out_path, filters);\nlet cap = match ctx.consume() {\n    (Some(cap), savefile) => cap,\n    (None, _) => {\n        // ctx was CaptureContext::Error(msg); surface msg and let the user retry\n        show_capture_error(get_error_text(&ctx));\n        return;\n    }\n};","preventionTips":["Run with CAP_NET_RAW/root for live capture; install Npcap on Windows.","Re-select the adapter at run time rather than trusting a remembered name across reboots/docks.","Verify pcap files open in Wireshark/tcpdump before feeding them to Sniffnet.","Check the pcap_error string first — it names the exact failing step (device open, file open)."],"tags":["rust","sniffnet","pcap","capture","permissions","device"],"backgroundTag":null,"analyzedSha":"48b0575dc0d3c7e503a466a3cb2c1466ba600f8e","analyzedAt":"2026-08-16T09:14:10.427Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}