use nostr::key::PublicKey;
use tokio::sync::SetError;
opaquerr::define_kind! {
pub ErrorKind {
Protocol => "nostr protocol error",
Sdk => "SDK error",
Invalid => "input violates a signer invariant",
Rejected => "operation rejected",
Timeout => "timeout",
NotFound => "not found",
State => "invalid state",
Other => "other error",
}
}
opaquerr::define_error! {
pub Error(ErrorKind)
from {
nostr::error::Error => ErrorKind::Protocol,
nostr_sdk::error::Error => ErrorKind::Sdk,
SetError<PublicKey> => ErrorKind::State,
}
}
impl Error {
#[inline]
pub fn other<E>(error: E) -> Self
where
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
Self::new(ErrorKind::Other, error)
}
pub(crate) fn invalid_response<S>(response: S) -> Self
where
S: Into<String>,
{
Self::new(ErrorKind::Invalid, response.into())
}
pub(crate) fn response<S>(message: S) -> Self
where
S: Into<String>,
{
Self::new(ErrorKind::Rejected, message.into())
}
pub(crate) fn signer_public_key_not_found() -> Self {
Self::with_static_message(ErrorKind::NotFound, "signer public key not found")
}
pub(crate) fn timeout() -> Self {
Self::simple(ErrorKind::Timeout)
}
pub(crate) fn unexpected_uri() -> Self {
Self::with_static_message(ErrorKind::Invalid, "unexpected URI")
}
pub(crate) fn public_key_not_match_app_keys() -> Self {
Self::with_static_message(ErrorKind::Invalid, "public key not match app keys")
}
pub(crate) fn no_client_secret() -> Self {
Self::with_static_message(ErrorKind::State, "missing client secret")
}
}