on master help sign in
refactor response processing
59d6b92Dmitry Sharshakov
helix-dap/src/transport.rs+37−41
@@ -1,6 +1,6 @@
1use crate::{Error, Result};
1use crate::{Error, Result};
2use anyhow::Context;
2use anyhow::Context;
3−use log::{error, info};
3+use log::{error, info, warn};
4use serde::{Deserialize, Serialize};
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
5use serde_json::Value;
6use std::collections::HashMap;
6use std::collections::HashMap;
@@ -162,54 +162,50 @@ impl Transport {
162 Ok(())
162 Ok(())
163 }
163 }
164
164
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+
165 async fn process_server_message(
186 async fn process_server_message(
166 &self,
187 &self,
167 client_tx: &UnboundedSender<Payload>,
188 client_tx: &UnboundedSender<Payload>,
168 msg: Payload,
189 msg: Payload,
169 ) -> Result<()> {
190 ) -> Result<()> {
170 match msg {
191 match msg {
171− Payload::Response(Response {
192+ Payload::Response(res) => {
172− ref success,
173− ref seq,
174− request_seq,
193+ let request_seq = res.request_seq;
175− ref command,
194+ let tx = self.pending_requests.lock().await.remove(&request_seq);
176− ref message,
195+
177− ref body,
196+ match tx {
178− ..
197+ Some(tx) => match tx.send(Self::process_response(res)).await {
179− }) => {
198+ Ok(_) => (),
180− let result = match success {
199+ Err(_) => error!(
181− true => {
200+ "Tried sending response into a closed channel (id={:?}), original request likely timed out",
182− info!("<- DAP success ({}, in response to {})", seq, request_seq);
201+ request_seq
183− if let Payload::Response(val) = msg {
202+ ),
184− Ok(val)
185− } else {
186− unreachable!();
187− }
188 }
203 }
189− false => {
204+ None => {
190− error!(
205+ warn!("Response to nonexistent request #{}", res.request_seq);
191− "<- DAP error {:?} ({:?}) for command #{} {}",
206+ client_tx.send(Payload::Response(res)).expect("Failed to send");
192− message, body, request_seq, command
193− );
194−
195− Err(Error::Other(anyhow::format_err!("{:?}", body)))
196 }
207 }
197− };
208+ }
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− };
213
209
214 Ok(())
210 Ok(())
215 }
211 }