fix: update OutgoingMessageSender::send_response() to take Serialize (#2263)

This makes `send_response()` easier to work with.
This commit is contained in:
Michael Bolin
2025-08-13 14:29:13 -07:00
committed by GitHub
parent d4533a0bb3
commit 37fc4185ef
5 changed files with 29 additions and 17 deletions

View File

@@ -18,6 +18,8 @@ use tokio::sync::mpsc;
use tokio::sync::oneshot;
use tracing::warn;
use crate::error_code::INTERNAL_ERROR_CODE;
/// Sends messages to the client and manages request callbacks.
pub(crate) struct OutgoingMessageSender {
next_request_id: AtomicI64,
@@ -74,9 +76,24 @@ impl OutgoingMessageSender {
}
}
pub(crate) async fn send_response(&self, id: RequestId, result: Result) {
let outgoing_message = OutgoingMessage::Response(OutgoingResponse { id, result });
let _ = self.sender.send(outgoing_message).await;
pub(crate) async fn send_response<T: Serialize>(&self, id: RequestId, response: T) {
match serde_json::to_value(response) {
Ok(result) => {
let outgoing_message = OutgoingMessage::Response(OutgoingResponse { id, result });
let _ = self.sender.send(outgoing_message).await;
}
Err(err) => {
self.send_error(
id,
JSONRPCErrorError {
code: INTERNAL_ERROR_CODE,
message: format!("failed to serialize response: {err}"),
data: None,
},
)
.await;
}
}
}
pub(crate) async fn send_event_as_notification(