Skip to content

Commit

Permalink
Supervisor errors should be handled
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed May 30, 2024
1 parent 6a560b9 commit 1226839
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/actors/relay_event_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl<T: NostrPort> Actor for RelayEventDispatcher<T> {
subscriber.subscribe_to_port(&state.event_received_output_port);
}
RelayEventDispatcherMessage::EventReceived(event) => {
info!("Event received: {}", event.id());
state.event_received_output_port.send(event);
counter!("event_received").increment(1);
}
Expand Down
23 changes: 17 additions & 6 deletions src/actors/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,32 @@ where
) -> Result<(), ActorProcessingErr> {
match message {
Self::Msg::Publish(report) => {
cast!(
if let Err(e) = cast!(
event_dispatcher,
RelayEventDispatcherMessage::Publish(report)
)?;
) {
error!("Failed to publish report: {}", e);
}
}
Self::Msg::GetNip05(request, reply_port) => {
let result = call_t!(
let result = match call_t!(
event_dispatcher,
RelayEventDispatcherMessage::GetNip05,
1000,
100,
request
)?;
) {
Ok(Some(nip05)) => Some(nip05),
Ok(None) => None,
Err(e) => {
error!("Failed to get nip05: {}", e);
None
}
};

if !reply_port.is_closed() {
reply_port.send(result)?;
if let Err(e) = reply_port.send(result) {
error!("Failed to send reply: {}", e);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/http_server/app_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use tracing::error;

#[derive(Debug)]
enum AppErrorKind {
Expand All @@ -28,6 +29,7 @@ impl AppError {

impl IntoResponse for AppError {
fn into_response(self) -> Response {
error!("{:?}", self);
match self.kind {
AppErrorKind::General(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
Expand Down
34 changes: 28 additions & 6 deletions src/adapters/http_server/slack_interactions_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::app_errors::AppError;
use super::WebAppState;
use crate::actors::messages::SupervisorMessage;
use crate::domain_objects::{ModerationCategory, ReportRequest, ReportTarget};
use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, bail, Context, Result};
use axum::{extract::State, routing::post, Extension, Router};
use nostr_sdk::prelude::*;
use ractor::{call_t, cast, ActorRef};
Expand Down Expand Up @@ -80,13 +80,26 @@ async fn slack_message(
slack_username: String,
) -> Result<String, AppError> {
let reporter_nip05_markdown =
try_njump(message_dispatcher.clone(), report_request.reporter_pubkey()).await?;
match try_njump(message_dispatcher.clone(), report_request.reporter_pubkey()).await {
Ok(nip05) => nip05,
Err(e) => {
info!("Failed to get nip05 link for reporter: {}", e);
format!("`{}`", report_request.reporter_pubkey())
}
};

let reported_nip05_markdown = try_njump(
let reported_nip05_markdown = match try_njump(
message_dispatcher.clone(),
&report_request.target().pubkey(),
)
.await?;
.await
{
Ok(nip05) => nip05,
Err(e) => {
info!("Failed to get nip05 link for reported: {}", e);
format!("`{}`", report_request.target().pubkey())
}
};

if let Some(moderated_report) = report_request.report(maybe_category.as_ref())? {
let report_id = moderated_report.id();
Expand Down Expand Up @@ -235,8 +248,17 @@ async fn try_njump(
message_dispatcher: ActorRef<SupervisorMessage>,
pubkey: &PublicKey,
) -> Result<String> {
let maybe_reporter_nip05 =
call_t!(message_dispatcher, SupervisorMessage::GetNip05, 50, *pubkey)?;
let maybe_reporter_nip05 = match call_t!(
message_dispatcher,
SupervisorMessage::GetNip05,
100,
*pubkey
) {
Ok(nip05) => nip05,
Err(e) => {
bail!("Failed to get nip05 link {}", e);
}
};

Ok(maybe_reporter_nip05
.as_ref()
Expand Down
11 changes: 4 additions & 7 deletions src/adapters/nostr_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,21 @@ impl NostrPort for NostrService {

async fn get_nip05(&self, public_key: PublicKey) -> Option<String> {
let Some(metadata) = self.client.metadata(public_key).await.ok() else {
error!("Failed to get metadata for public key: {:?}", public_key);
error!("Failed to get metadata for public key: {}", public_key);
return None;
};

if let Some(nip05_value) = metadata.nip05 {
let Ok(()) = nip05::verify(&public_key, &nip05_value, None).await else {
error!("Failed to verify Nip05 for public key: {:?}", public_key);
error!("Failed to verify Nip05 for public key: {}", public_key);
return None;
};

info!(
"Nip05 for public key: {:?} is: {:?}",
public_key, nip05_value
);
info!("Nip05 for public key: {} is: {}", public_key, nip05_value);
return Some(nip05_value);
}

info!("No Nip05 found for public key: {:?}", public_key);
info!("No Nip05 found for public key: {}", public_key);
None
}

Expand Down
2 changes: 1 addition & 1 deletion src/adapters/slack_client_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl SlackClientAdapter {

async fn try_njump(&self, pubkey: PublicKey) -> Result<String> {
let maybe_reporter_nip05 =
call_t!(self.nostr_actor, SupervisorMessage::GetNip05, 50, pubkey)?;
call_t!(self.nostr_actor, SupervisorMessage::GetNip05, 100, pubkey)?;

Ok(maybe_reporter_nip05
.as_ref()
Expand Down
6 changes: 5 additions & 1 deletion src/domain_objects/gift_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ impl TryFrom<Event> for GiftWrappedReportRequest {
if event.kind == Kind::GiftWrap {
Ok(GiftWrappedReportRequest::new(event))
} else {
bail!("Event kind is not 1059")
bail!(
"Event kind is not 1059. id:{} kind:{}",
event.id,
event.kind
)
}
}
}
4 changes: 2 additions & 2 deletions src/service_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl ServiceManager {
loop {
tokio::select! {
_ = token_clone.cancelled() => {
debug!("ServiceManager is being dropped, cancelling all tasks");
debug!("Cleaning up. Cancelling all tasks");
for actor in &actors {
debug!("Stopping actor");
actor.stop(Some("ServiceManager is being dropped".to_string()));
Expand All @@ -202,7 +202,7 @@ impl ServiceManager {
impl Drop for ServiceManager {
fn drop(&mut self) {
if !self.token.is_cancelled() {
debug!("ServiceManager is being dropped, cancelling all tasks");
debug!("ServiceManager is being dropped");
self.token.cancel();
}
}
Expand Down

0 comments on commit 1226839

Please sign in to comment.