Skip to content

Commit

Permalink
Use StdResult as try_send result
Browse files Browse the repository at this point in the history
  • Loading branch information
sfauvel committed Nov 6, 2024
1 parent 1708d3a commit 90c54c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
18 changes: 9 additions & 9 deletions mithril-aggregator/src/event_store/transmitter_service.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::Context;
use slog::{warn, Logger};
use std::fmt::Debug;
use tokio::sync::mpsc::UnboundedSender;

use mithril_common::logging::LoggerExtensions;
use mithril_common::{logging::LoggerExtensions, StdResult};

use super::EventMessage;

Expand Down Expand Up @@ -36,11 +37,12 @@ where

impl TransmitterService<EventMessage> {
/// Send an [EventMessage].
pub fn try_send(
&self,
message: EventMessage,
) -> Result<(), tokio::sync::mpsc::error::SendError<EventMessage>> {
self.get_transmitter().send(message)
pub fn try_send(&self, message: EventMessage) -> StdResult<()> {
self.get_transmitter()
.send(message.clone())
.with_context(|| {
format!("An error occurred when sending message {message:?} to monitoring.")
})
}

/// Send an [EventMessage].
Expand All @@ -49,9 +51,7 @@ impl TransmitterService<EventMessage> {
/// If there is one, a warning is issued so the resulting error may be safely ignored by the caller.
pub fn send(&self, message: EventMessage) {
if let Err(e) = self.try_send(message.clone()) {
let error_msg =
format!("An error occurred when sending message {message:?} to monitoring: '{e}'.");
warn!(self.logger, "Event message error"; "error" => &error_msg);
warn!(self.logger, "Event message error"; "error" => ?e);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ mod handlers {

match event_transmitter.try_send(message.clone()) {
Err(e) => {
let error_msg = format!(
"An error occurred when sending message {message:?} to monitoring: '{e}'."
);
warn!(logger, "Event message error"; "error" => &error_msg);
Ok(reply::internal_server_error(error_msg))
warn!(logger, "Event message error"; "error" => ?e);
Ok(reply::internal_server_error(e))
}
Ok(_) => Ok(reply::empty(StatusCode::CREATED)),
}
Expand Down

0 comments on commit 90c54c3

Please sign in to comment.