Skip to main content

nostr_connect/
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
5//! Nostr Connect error
6
7use std::fmt;
8
9use nostr::event::builder;
10use nostr::nips::{nip04, nip44, nip46};
11use nostr::PublicKey;
12use nostr_relay_pool::pool;
13use tokio::sync::SetError;
14
15/// Nostr Connect error
16#[derive(Debug)]
17pub enum Error {
18    /// Event builder error
19    Builder(builder::Error),
20    /// NIP04 error
21    NIP04(nip04::Error),
22    /// NIP44 error
23    NIP44(nip44::Error),
24    /// NIP46 error
25    NIP46(nip46::Error),
26    /// Pool
27    Pool(pool::Error),
28    /// Set user public key error
29    SetUserPublicKey(SetError<PublicKey>),
30    /// NIP46 response error
31    Response(String),
32    /// Signer public key not found
33    SignerPublicKeyNotFound,
34    /// Request timeout
35    Timeout,
36    /// Unexpected URI
37    UnexpectedUri,
38    /// Public key not match
39    PublicKeyNotMatchAppKeys,
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::Builder(e) => e.fmt(f),
48            Self::NIP04(e) => e.fmt(f),
49            Self::NIP44(e) => e.fmt(f),
50            Self::NIP46(e) => e.fmt(f),
51            Self::Pool(e) => e.fmt(f),
52            Self::SetUserPublicKey(e) => e.fmt(f),
53            Self::Response(e) => e.fmt(f),
54            Self::SignerPublicKeyNotFound => f.write_str("signer public key not found"),
55            Self::Timeout => f.write_str("timeout"),
56            Self::UnexpectedUri => f.write_str("unexpected URI"),
57            Self::PublicKeyNotMatchAppKeys => f.write_str("public key not match app keys"),
58        }
59    }
60}
61
62impl From<builder::Error> for Error {
63    fn from(e: builder::Error) -> Self {
64        Self::Builder(e)
65    }
66}
67
68impl From<nip04::Error> for Error {
69    fn from(e: nip04::Error) -> Self {
70        Self::NIP04(e)
71    }
72}
73
74impl From<nip44::Error> for Error {
75    fn from(e: nip44::Error) -> Self {
76        Self::NIP44(e)
77    }
78}
79
80impl From<nip46::Error> for Error {
81    fn from(e: nip46::Error) -> Self {
82        Self::NIP46(e)
83    }
84}
85
86impl From<pool::Error> for Error {
87    fn from(e: pool::Error) -> Self {
88        Self::Pool(e)
89    }
90}
91
92impl From<SetError<PublicKey>> for Error {
93    fn from(e: SetError<PublicKey>) -> Self {
94        Self::SetUserPublicKey(e)
95    }
96}