Skip to main content

vector_core/
error.rs

1//! Error types for vector-core.
2
3use std::fmt;
4
5/// Unified error type for all vector-core operations.
6#[derive(Debug)]
7pub enum VectorError {
8    /// Database error
9    Db(String),
10    /// Nostr protocol error
11    Nostr(String),
12    /// Cryptographic error
13    Crypto(String),
14    /// Network/HTTP error
15    Network(String),
16    /// I/O error
17    Io(std::io::Error),
18    /// State not initialized (e.g., not logged in)
19    NotInitialized(String),
20    /// Generic error with message
21    Other(String),
22}
23
24impl fmt::Display for VectorError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            VectorError::Db(msg) => write!(f, "Database error: {}", msg),
28            VectorError::Nostr(msg) => write!(f, "Nostr error: {}", msg),
29            VectorError::Crypto(msg) => write!(f, "Crypto error: {}", msg),
30            VectorError::Network(msg) => write!(f, "Network error: {}", msg),
31            VectorError::Io(err) => write!(f, "I/O error: {}", err),
32            VectorError::NotInitialized(msg) => write!(f, "Not initialized: {}", msg),
33            VectorError::Other(msg) => write!(f, "{}", msg),
34        }
35    }
36}
37
38impl std::error::Error for VectorError {}
39
40/// Why an attachment download failed, for callers that remember failures.
41#[derive(Debug)]
42pub enum DownloadError {
43    /// More ciphertext than the caller's cap (in bytes). Every mirror serves the same blob.
44    TooLarge(usize),
45    /// Every source answered, and none served bytes that decrypt or match their hash.
46    Unusable(String),
47    /// A source didn't answer; a later attempt may reach it.
48    Unreachable(String),
49}
50
51impl fmt::Display for DownloadError {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        match self {
54            DownloadError::TooLarge(cap) => write!(f, "attachment exceeds the {} cap", crate::crypto::format_bytes(*cap as u64)),
55            DownloadError::Unusable(msg) | DownloadError::Unreachable(msg) => write!(f, "{msg}"),
56        }
57    }
58}
59
60impl std::error::Error for DownloadError {}
61
62impl From<String> for VectorError {
63    fn from(s: String) -> Self {
64        VectorError::Other(s)
65    }
66}
67
68impl From<&str> for VectorError {
69    fn from(s: &str) -> Self {
70        VectorError::Other(s.to_string())
71    }
72}
73
74impl From<std::io::Error> for VectorError {
75    fn from(err: std::io::Error) -> Self {
76        VectorError::Io(err)
77    }
78}
79
80impl From<rusqlite::Error> for VectorError {
81    fn from(err: rusqlite::Error) -> Self {
82        VectorError::Db(err.to_string())
83    }
84}
85
86impl From<nostr_sdk::prelude::Error> for VectorError {
87    fn from(err: nostr_sdk::prelude::Error) -> Self {
88        VectorError::Nostr(err.to_string())
89    }
90}
91
92impl From<reqwest::Error> for VectorError {
93    fn from(err: reqwest::Error) -> Self {
94        VectorError::Network(err.to_string())
95    }
96}
97
98/// Convenience alias used throughout vector-core (matches src-tauri's `Result<T, String>` pattern).
99pub type Result<T> = std::result::Result<T, VectorError>;
100
101/// Convert VectorError to String for compatibility with existing code that returns Result<T, String>.
102impl From<VectorError> for String {
103    fn from(err: VectorError) -> String {
104        err.to_string()
105    }
106}