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)]
7#[non_exhaustive]
8pub enum VectorError {
9    /// Database error
10    Db(String),
11    /// Nostr protocol error
12    Nostr(String),
13    /// Cryptographic error
14    Crypto(String),
15    /// Network/HTTP error
16    Network(String),
17    /// I/O error
18    Io(std::io::Error),
19    /// State not initialized (e.g., not logged in)
20    NotInitialized(String),
21    /// Generic error with message
22    Other(String),
23    /// The acting account lacks a permission the action needs, checked the way every reader
24    /// will judge it. `needs` holds the missing bits.
25    NotPermitted { needs: crate::community::roles::Permissions, action: String },
26    /// No such Community, Channel, Role, Category, message or member here.
27    NotFound(String),
28}
29
30impl fmt::Display for VectorError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            VectorError::Db(msg) => write!(f, "Database error: {}", msg),
34            VectorError::Nostr(msg) => write!(f, "Nostr error: {}", msg),
35            VectorError::Crypto(msg) => write!(f, "Crypto error: {}", msg),
36            VectorError::Network(msg) => write!(f, "Network error: {}", msg),
37            VectorError::Io(err) => write!(f, "I/O error: {}", err),
38            VectorError::NotInitialized(msg) => write!(f, "Not initialized: {}", msg),
39            VectorError::Other(msg) => write!(f, "{}", msg),
40            VectorError::NotPermitted { needs, action } => write!(f, "{action} needs permission bits {:#x}", needs.0),
41            VectorError::NotFound(what) => write!(f, "not found: {what}"),
42        }
43    }
44}
45
46impl std::error::Error for VectorError {}
47
48/// Why an attachment download failed, for callers that remember failures.
49#[derive(Debug)]
50pub enum DownloadError {
51    /// More ciphertext than the caller's cap (in bytes). Every mirror serves the same blob.
52    TooLarge(usize),
53    /// Every source answered, and none served bytes that decrypt or match their hash.
54    Unusable(String),
55    /// A source didn't answer; a later attempt may reach it.
56    Unreachable(String),
57    /// The caller stopped it.
58    Cancelled,
59}
60
61impl fmt::Display for DownloadError {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        match self {
64            DownloadError::TooLarge(cap) => write!(f, "attachment exceeds the {} cap", crate::crypto::format_bytes(*cap as u64)),
65            DownloadError::Unusable(msg) | DownloadError::Unreachable(msg) => write!(f, "{msg}"),
66            DownloadError::Cancelled => write!(f, "Download cancelled"),
67        }
68    }
69}
70
71impl std::error::Error for DownloadError {}
72
73impl From<String> for VectorError {
74    fn from(s: String) -> Self {
75        VectorError::Other(s)
76    }
77}
78
79impl From<&str> for VectorError {
80    fn from(s: &str) -> Self {
81        VectorError::Other(s.to_string())
82    }
83}
84
85impl From<std::io::Error> for VectorError {
86    fn from(err: std::io::Error) -> Self {
87        VectorError::Io(err)
88    }
89}
90
91impl From<rusqlite::Error> for VectorError {
92    fn from(err: rusqlite::Error) -> Self {
93        VectorError::Db(err.to_string())
94    }
95}
96
97impl From<nostr_sdk::prelude::Error> for VectorError {
98    fn from(err: nostr_sdk::prelude::Error) -> Self {
99        VectorError::Nostr(err.to_string())
100    }
101}
102
103impl From<reqwest::Error> for VectorError {
104    fn from(err: reqwest::Error) -> Self {
105        VectorError::Network(err.to_string())
106    }
107}
108
109impl From<crate::net::HttpError> for VectorError {
110    fn from(err: crate::net::HttpError) -> Self {
111        VectorError::Network(err.to_string())
112    }
113}
114
115/// Convenience alias used throughout vector-core (matches src-tauri's `Result<T, String>` pattern).
116pub type Result<T> = std::result::Result<T, VectorError>;
117
118/// Convert VectorError to String for compatibility with existing code that returns Result<T, String>.
119impl From<VectorError> for String {
120    fn from(err: VectorError) -> String {
121        err.to_string()
122    }
123}