1use std::fmt;
4
5#[derive(Debug)]
7#[non_exhaustive]
8pub enum VectorError {
9 Db(String),
11 Nostr(String),
13 Crypto(String),
15 Network(String),
17 Io(std::io::Error),
19 NotInitialized(String),
21 Other(String),
23 NotPermitted { needs: crate::community::roles::Permissions, action: String },
26 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#[derive(Debug)]
50pub enum DownloadError {
51 TooLarge(usize),
53 Unusable(String),
55 Unreachable(String),
57 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
115pub type Result<T> = std::result::Result<T, VectorError>;
117
118impl From<VectorError> for String {
120 fn from(err: VectorError) -> String {
121 err.to_string()
122 }
123}