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
40impl From<String> for VectorError {
41    fn from(s: String) -> Self {
42        VectorError::Other(s)
43    }
44}
45
46impl From<&str> for VectorError {
47    fn from(s: &str) -> Self {
48        VectorError::Other(s.to_string())
49    }
50}
51
52impl From<std::io::Error> for VectorError {
53    fn from(err: std::io::Error) -> Self {
54        VectorError::Io(err)
55    }
56}
57
58impl From<rusqlite::Error> for VectorError {
59    fn from(err: rusqlite::Error) -> Self {
60        VectorError::Db(err.to_string())
61    }
62}
63
64impl From<nostr_sdk::client::Error> for VectorError {
65    fn from(err: nostr_sdk::client::Error) -> Self {
66        VectorError::Nostr(err.to_string())
67    }
68}
69
70impl From<reqwest::Error> for VectorError {
71    fn from(err: reqwest::Error) -> Self {
72        VectorError::Network(err.to_string())
73    }
74}
75
76/// Convenience alias used throughout vector-core (matches src-tauri's `Result<T, String>` pattern).
77pub type Result<T> = std::result::Result<T, VectorError>;
78
79/// Convert VectorError to String for compatibility with existing code that returns Result<T, String>.
80impl From<VectorError> for String {
81    fn from(err: VectorError) -> String {
82        err.to_string()
83    }
84}