nostr_sdk/client/
error.rs

1// Copyright (c) 2022-2023 Yuki Kishimoto
2// Copyright (c) 2023-2025 Rust Nostr Developers
3// Distributed under the MIT software license
4
5use std::fmt;
6
7use nostr::prelude::*;
8use nostr::serde_json;
9use nostr_database::prelude::*;
10use nostr_gossip::error::GossipError;
11use nostr_relay_pool::__private::SharedStateError;
12use nostr_relay_pool::prelude::*;
13
14/// Client error
15#[derive(Debug)]
16pub enum Error {
17    /// Relay error
18    Relay(nostr_relay_pool::relay::Error),
19    /// Relay Pool error
20    RelayPool(pool::Error),
21    /// Database error
22    Database(DatabaseError),
23    /// Signer error
24    Signer(SignerError),
25    /// Gossip error
26    Gossip(GossipError),
27    /// [`EventBuilder`] error
28    EventBuilder(event::builder::Error),
29    /// Json error
30    Json(serde_json::Error),
31    /// Shared state error
32    SharedState(SharedStateError),
33    /// NIP59
34    #[cfg(feature = "nip59")]
35    NIP59(nip59::Error),
36    /// Broken down filters for gossip are empty
37    GossipFiltersEmpty,
38    /// Private message (NIP17) relays not found
39    PrivateMsgRelaysNotFound,
40}
41
42impl std::error::Error for Error {}
43
44impl fmt::Display for Error {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Self::Relay(e) => e.fmt(f),
48            Self::RelayPool(e) => e.fmt(f),
49            Self::Database(e) => e.fmt(f),
50            Self::Signer(e) => e.fmt(f),
51            Self::Gossip(e) => e.fmt(f),
52            Self::EventBuilder(e) => e.fmt(f),
53            Self::Json(e) => e.fmt(f),
54            Self::SharedState(e) => e.fmt(f),
55            #[cfg(feature = "nip59")]
56            Self::NIP59(e) => e.fmt(f),
57            Self::GossipFiltersEmpty => {
58                f.write_str("gossip broken down filters are empty")
59            }
60            Self::PrivateMsgRelaysNotFound => f.write_str("Private message relays not found. The user is not ready to receive private messages."),
61        }
62    }
63}
64
65impl From<nostr_relay_pool::relay::Error> for Error {
66    fn from(e: nostr_relay_pool::relay::Error) -> Self {
67        Self::Relay(e)
68    }
69}
70
71impl From<pool::Error> for Error {
72    fn from(e: pool::Error) -> Self {
73        Self::RelayPool(e)
74    }
75}
76
77impl From<DatabaseError> for Error {
78    fn from(e: DatabaseError) -> Self {
79        Self::Database(e)
80    }
81}
82
83impl From<SignerError> for Error {
84    fn from(e: SignerError) -> Self {
85        Self::Signer(e)
86    }
87}
88
89impl From<GossipError> for Error {
90    fn from(e: GossipError) -> Self {
91        Self::Gossip(e)
92    }
93}
94
95impl From<event::builder::Error> for Error {
96    fn from(e: event::builder::Error) -> Self {
97        Self::EventBuilder(e)
98    }
99}
100
101impl From<serde_json::Error> for Error {
102    fn from(e: serde_json::Error) -> Self {
103        Self::Json(e)
104    }
105}
106
107impl From<SharedStateError> for Error {
108    fn from(e: SharedStateError) -> Self {
109        Self::SharedState(e)
110    }
111}
112
113#[cfg(feature = "nip59")]
114impl From<nip59::Error> for Error {
115    fn from(e: nip59::Error) -> Self {
116        Self::NIP59(e)
117    }
118}