refactor response processing

pull/574/head
Dmitry Sharshakov 3 years ago committed by Blaž Hrastnik
parent 9678df1c62
commit 59d6b92e5b

@ -1,6 +1,6 @@
use crate::{Error, Result}; use crate::{Error, Result};
use anyhow::Context; use anyhow::Context;
use log::{error, info}; use log::{error, info, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use std::collections::HashMap; use std::collections::HashMap;
@ -162,54 +162,50 @@ impl Transport {
Ok(()) Ok(())
} }
fn process_response(res: Response) -> Result<Response> {
match res.success {
true => {
info!(
"<- DAP success ({}, in response to {})",
res.seq, res.request_seq
);
Ok(res)
}
false => {
error!(
"<- DAP error {:?} ({:?}) for command #{} {}",
res.message, res.body, res.request_seq, res.command
);
Err(Error::Other(anyhow::format_err!("{:?}", res.body)))
}
}
}
async fn process_server_message( async fn process_server_message(
&self, &self,
client_tx: &UnboundedSender<Payload>, client_tx: &UnboundedSender<Payload>,
msg: Payload, msg: Payload,
) -> Result<()> { ) -> Result<()> {
match msg { match msg {
Payload::Response(Response { Payload::Response(res) => {
ref success, let request_seq = res.request_seq;
ref seq, let tx = self.pending_requests.lock().await.remove(&request_seq);
request_seq,
ref command, match tx {
ref message, Some(tx) => match tx.send(Self::process_response(res)).await {
ref body, Ok(_) => (),
.. Err(_) => error!(
}) => { "Tried sending response into a closed channel (id={:?}), original request likely timed out",
let result = match success { request_seq
true => { ),
info!("<- DAP success ({}, in response to {})", seq, request_seq);
if let Payload::Response(val) = msg {
Ok(val)
} else {
unreachable!();
}
} }
false => { None => {
error!( warn!("Response to nonexistent request #{}", res.request_seq);
"<- DAP error {:?} ({:?}) for command #{} {}", client_tx.send(Payload::Response(res)).expect("Failed to send");
message, body, request_seq, command
);
Err(Error::Other(anyhow::format_err!("{:?}", body)))
} }
}; }
let tx = self
.pending_requests
.lock()
.await
.remove(&request_seq)
.expect("pending_request with id not found!");
match tx.send(result).await {
Ok(_) => (),
Err(_) => error!(
"Tried sending response into a closed channel (id={:?}), original request likely timed out",
request_seq
),
};
Ok(()) Ok(())
} }

Loading…
Cancel
Save