on master help sign in
refactor response processing
commit59d6b92e5b05a829dd2aeb0994afc51dec0da87f authorDmitry Sharshakov <d3dx12.xx@gmail.com> dateFri 13 Aug 2021, 22:46 (5 years ago) parent9678df1
1 file changed+37−41
helix-dap/src/transport.rs+37−41
helix-dap/src/transport.rs+37−41
@@ -1,6 +1,6 @@
11use crate::{Error, Result};
22use anyhow::Context;
3−use log::{error, info};
3+use log::{error, info, warn};
44use serde::{Deserialize, Serialize};
55use serde_json::Value;
66use std::collections::HashMap;
@@ -162,54 +162,50 @@ impl Transport {
162162 Ok(())
163163 }
164164
165+ fn process_response(res: Response) -> Result<Response> {
166+ match res.success {
167+ true => {
168+ info!(
169+ "<- DAP success ({}, in response to {})",
170+ res.seq, res.request_seq
171+ );
172+
173+ Ok(res)
174+ }
175+ false => {
176+ error!(
177+ "<- DAP error {:?} ({:?}) for command #{} {}",
178+ res.message, res.body, res.request_seq, res.command
179+ );
180+
181+ Err(Error::Other(anyhow::format_err!("{:?}", res.body)))
182+ }
183+ }
184+ }
185+
165186 async fn process_server_message(
166187 &self,
167188 client_tx: &UnboundedSender<Payload>,
168189 msg: Payload,
169190 ) -> Result<()> {
170191 match msg {
171− Payload::Response(Response {
172− ref success,
173− ref seq,
174− request_seq,
175− ref command,
176− ref message,
177− ref body,
178− ..
179− }) => {
180− let result = match success {
181− true => {
182− info!("<- DAP success ({}, in response to {})", seq, request_seq);
183− if let Payload::Response(val) = msg {
184− Ok(val)
185− } else {
186− unreachable!();
187− }
192+ Payload::Response(res) => {
193+ let request_seq = res.request_seq;
194+ let tx = self.pending_requests.lock().await.remove(&request_seq);
195+
196+ match tx {
197+ Some(tx) => match tx.send(Self::process_response(res)).await {
198+ Ok(_) => (),
199+ Err(_) => error!(
200+ "Tried sending response into a closed channel (id={:?}), original request likely timed out",
201+ request_seq
202+ ),
188203 }
189− false => {
190− error!(
191− "<- DAP error {:?} ({:?}) for command #{} {}",
192− message, body, request_seq, command
193− );
194−
195− Err(Error::Other(anyhow::format_err!("{:?}", body)))
204+ None => {
205+ warn!("Response to nonexistent request #{}", res.request_seq);
206+ client_tx.send(Payload::Response(res)).expect("Failed to send");
196207 }
197− };
198−
199− let tx = self
200− .pending_requests
201− .lock()
202− .await
203− .remove(&request_seq)
204− .expect("pending_request with id not found!");
205−
206− match tx.send(result).await {
207− Ok(_) => (),
208− Err(_) => error!(
209− "Tried sending response into a closed channel (id={:?}), original request likely timed out",
210− request_seq
211− ),
212− };
208+ }
213209
214210 Ok(())
215211 }