Skip to content

Commit

Permalink
pool: better errors
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Dec 3, 2024
1 parent 3094faa commit ab94ee0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
9 changes: 6 additions & 3 deletions crates/nostr-relay-pool/src/relay/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ pub enum Error {
/// Received shutdown
#[error("received shutdown")]
Shutdown,
/// Event not published
#[error("event not published: {0}")]
EventNotPublished(String),
/// Relay message
#[error("{0}")]
RelayMessage(String),
/// Only some events
#[error("partial publish: published={}, missing={}", published.len(), not_published.len())]
PartialPublish {
Expand Down Expand Up @@ -159,6 +159,9 @@ pub enum Error {
/// Auth failed
#[error("authentication failed")]
AuthenticationFailed,
/// Premature exit
#[error("premature exit")]
PrematureExit,
}

impl Error {
Expand Down
37 changes: 15 additions & 22 deletions crates/nostr-relay-pool/src/relay/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,12 +1193,12 @@ impl InnerRelay {
return if status {
Ok(event_id)
} else {
Err(Error::EventNotPublished(message))
Err(Error::RelayMessage(message))
};
}
}

Err(Error::EventNotPublished(message))
Err(Error::RelayMessage(message))
}

async fn auth(&self, challenge: String) -> Result<(), Error> {
Expand Down Expand Up @@ -1227,7 +1227,7 @@ impl InnerRelay {
if status {
Ok(())
} else {
Err(Error::EventNotPublished(message))
Err(Error::RelayMessage(message))
}
}

Expand Down Expand Up @@ -1255,20 +1255,15 @@ impl InnerRelay {
}
RelayNotification::RelayStatus { status } => {
if status.is_disconnected() {
// TODO: use another error?
return Err(Error::EventNotPublished(String::from(
"relay not connected (status changed)",
)));
return Err(Error::NotConnected);
}
}
RelayNotification::Shutdown => break,
_ => (),
}
}

Err(Error::EventNotPublished(String::from(
"loop prematurely terminated",
)))
Err(Error::PrematureExit)
})
.await
.ok_or(Error::Timeout)?
Expand All @@ -1290,20 +1285,15 @@ impl InnerRelay {
}
RelayNotification::RelayStatus { status } => {
if status.is_disconnected() {
// TODO: use another error?
return Err(Error::EventNotPublished(String::from(
"relay not connected (status changed)",
)));
return Err(Error::NotConnected);
}
}
RelayNotification::Shutdown => break,
_ => (),
}
}

Err(Error::EventNotPublished(String::from(
"loop prematurely terminated",
)))
Err(Error::PrematureExit)
})
.await
.ok_or(Error::Timeout)?
Expand Down Expand Up @@ -1399,10 +1389,14 @@ impl InnerRelay {
// Check if auth required
match MachineReadablePrefix::parse(&message) {
Some(MachineReadablePrefix::AuthRequired) => {
require_resubscription = true;
if relay.state.is_auto_authentication_enabled() {
require_resubscription = true;
} else {
return (false, Some(SubscriptionAutoClosedReason::Closed(message))); // No need to send CLOSE msg
}
}
_ => {
return (false, Some(SubscriptionAutoClosedReason::Closed)); // No need to send CLOSE msg
return (false, Some(SubscriptionAutoClosedReason::Closed(message))); // No need to send CLOSE msg
}
}
}
Expand Down Expand Up @@ -1560,9 +1554,8 @@ impl InnerRelay {
SubscriptionAutoClosedReason::AuthenticationFailed => {
return Err(Error::AuthenticationFailed);
}
SubscriptionAutoClosedReason::Closed => {
// TODO: return error?
break;
SubscriptionAutoClosedReason::Closed(message) => {
return Err(Error::RelayMessage(message));
}
// Completed
SubscriptionAutoClosedReason::Completed => break,
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-relay-pool/src/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum SubscriptionAutoClosedReason {
/// NIP42 authentication failed
AuthenticationFailed,
/// Closed
Closed,
Closed(String),
/// Completed
Completed,
}
Expand Down

0 comments on commit ab94ee0

Please sign in to comment.