diff --git a/crates/nostr-relay-pool/src/relay/error.rs b/crates/nostr-relay-pool/src/relay/error.rs index c2eb0c33d..27e44f3cb 100644 --- a/crates/nostr-relay-pool/src/relay/error.rs +++ b/crates/nostr-relay-pool/src/relay/error.rs @@ -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 { @@ -159,6 +159,9 @@ pub enum Error { /// Auth failed #[error("authentication failed")] AuthenticationFailed, + /// Premature exit + #[error("premature exit")] + PrematureExit, } impl Error { diff --git a/crates/nostr-relay-pool/src/relay/inner.rs b/crates/nostr-relay-pool/src/relay/inner.rs index 80455500f..b94992188 100644 --- a/crates/nostr-relay-pool/src/relay/inner.rs +++ b/crates/nostr-relay-pool/src/relay/inner.rs @@ -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> { @@ -1227,7 +1227,7 @@ impl InnerRelay { if status { Ok(()) } else { - Err(Error::EventNotPublished(message)) + Err(Error::RelayMessage(message)) } } @@ -1255,10 +1255,7 @@ 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, @@ -1266,9 +1263,7 @@ impl InnerRelay { } } - Err(Error::EventNotPublished(String::from( - "loop prematurely terminated", - ))) + Err(Error::PrematureExit) }) .await .ok_or(Error::Timeout)? @@ -1290,10 +1285,7 @@ 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, @@ -1301,9 +1293,7 @@ impl InnerRelay { } } - Err(Error::EventNotPublished(String::from( - "loop prematurely terminated", - ))) + Err(Error::PrematureExit) }) .await .ok_or(Error::Timeout)? @@ -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 } } } @@ -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, diff --git a/crates/nostr-relay-pool/src/relay/mod.rs b/crates/nostr-relay-pool/src/relay/mod.rs index b993b23a2..b066fb1e4 100644 --- a/crates/nostr-relay-pool/src/relay/mod.rs +++ b/crates/nostr-relay-pool/src/relay/mod.rs @@ -46,7 +46,7 @@ pub enum SubscriptionAutoClosedReason { /// NIP42 authentication failed AuthenticationFailed, /// Closed - Closed, + Closed(String), /// Completed Completed, }