Skip to main content

rostra_client/
error.rs

1use std::io;
2
3use pkarr::dns::SimpleDnsError;
4use rostra_client_db::DbError;
5use rostra_core::ShortEventId;
6use rostra_core::event::ContentValidationError;
7use rostra_core::id::{RostraId, RostraIdSecretKeyError};
8use rostra_util_error::BoxedError;
9use snafu::Snafu;
10
11/// Meh alias
12pub type IrohError = anyhow::Error;
13pub type IrohResult<T> = anyhow::Result<T>;
14
15#[derive(Debug, Snafu)]
16#[snafu(visibility(pub(crate)))]
17pub enum InitError {
18    #[snafu(display("Pkarr Client initialization error"))]
19    InitPkarrClient { source: pkarr::errors::BuildError },
20    #[snafu(display("Iroh Client initialization error"))]
21    InitIrohClient { source: iroh::endpoint::BindError },
22    #[snafu(display("Failed to activate"))]
23    Activate { source: ActivateError },
24    #[snafu(transparent)]
25    Db { source: DbError },
26}
27
28pub type InitResult<T> = std::result::Result<T, InitError>;
29
30#[derive(Debug, Snafu)]
31#[snafu(visibility(pub(crate)))]
32pub enum ActivateError {
33    #[snafu(display("Secret key does not match RostraId"))]
34    SecretMismatch,
35    #[snafu(display("Failed to store the local node announcement: {source}"))]
36    LocalAnnouncementStorage { source: PostError },
37}
38
39pub type ActivateResult<T> = std::result::Result<T, ActivateError>;
40
41#[derive(Debug, Snafu)]
42#[snafu(visibility(pub(crate)))]
43pub enum IdResolveError {
44    NotFound,
45    InvalidId {
46        source: pkarr::errors::PublicKeyError,
47    },
48    RRecord {
49        source: RRecordError,
50    },
51    MissingTicket,
52    MalformedIrohTicket,
53    PkarrResolve,
54}
55
56pub(crate) type IdResolveResult<T> = std::result::Result<T, IdResolveError>;
57
58#[derive(Debug, Snafu)]
59#[snafu(visibility(pub(crate)))]
60pub enum IdPublishError {
61    PkarrSignedPacket {
62        source: pkarr::errors::SignedPacketBuildError,
63    },
64    PkarrPublish {
65        source: pkarr::errors::PublishError,
66    },
67    PkarrPacketBuild {
68        source: pkarr::errors::SignedPacketBuildError,
69    },
70    #[snafu(display("Iroh Client initialization error"))]
71    Dns {
72        source: SimpleDnsError,
73    },
74}
75
76pub type IdPublishResult<T> = std::result::Result<T, IdPublishError>;
77
78#[derive(Debug, Snafu)]
79#[snafu(visibility(pub(crate)))]
80pub enum ConnectError {
81    Resolve {
82        source: IdResolveError,
83    },
84    #[snafu(display("Ping failed after connecting: {source}"))]
85    PingFailed {
86        source: rostra_p2p::RpcError,
87    },
88    ConnectIroh {
89        source: iroh::endpoint::ConnectError,
90    },
91    #[snafu(display("Node is in backoff, try again later"))]
92    NodeInBackoff,
93    #[snafu(display("Resolved to own endpoint, cannot connect to self"))]
94    ResolvedToSelf,
95}
96
97pub type ConnectResult<T> = std::result::Result<T, ConnectError>;
98
99#[derive(Debug, Snafu)]
100#[snafu(visibility(pub(crate)))]
101pub enum IdSecretReadError {
102    Io { source: io::Error },
103    Parsing { source: RostraIdSecretKeyError },
104}
105
106pub type IdSecretReadResult<T> = std::result::Result<T, IdSecretReadError>;
107
108#[derive(Debug, Snafu)]
109#[snafu(visibility(pub(crate)))]
110pub enum PostError {
111    #[snafu(transparent)]
112    Resolve { source: IdResolveError },
113    #[snafu(display("Encoding error: {source}"))]
114    #[snafu(visibility(pub))]
115    Encode { source: BoxedError },
116    #[snafu(transparent)]
117    Validation { source: ContentValidationError },
118    #[snafu(display("Failed to store the published event: {source}"))]
119    Storage { source: DbError },
120}
121
122pub type PostResult<T> = std::result::Result<T, PostError>;
123
124/// Failure to ingest verified event content at a client request boundary.
125#[derive(Debug, Snafu)]
126#[snafu(display("Failed to store event {event_id} by {author_id}: {source}"))]
127pub struct StoreEventError {
128    /// Authenticated author of the event being stored.
129    pub author_id: RostraId,
130    /// Short identifier of the event being stored.
131    pub event_id: ShortEventId,
132    /// Database ingestion failure.
133    pub source: DbError,
134}
135
136/// Result of storing verified event content through the client boundary.
137pub type StoreEventResult<T> = std::result::Result<T, StoreEventError>;
138
139#[derive(Debug, Snafu)]
140#[snafu(visibility(pub(crate)))]
141pub enum RRecordError {
142    MissingRecord,
143    WrongType,
144    MissingValue,
145    // TODO: InvalidEncoding { source: BoxedError },
146    InvalidEncoding,
147    InvalidKey { source: SimpleDnsError },
148    InvalidDomain { source: SimpleDnsError },
149}
150pub type RRecordResult<T> = Result<T, RRecordError>;