diff options
author | Aleksey Kladov <[email protected]> | 2020-04-01 11:45:37 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-04-01 12:14:38 +0100 |
commit | cc8113dd464fdce61fd425157e1145404be3b568 (patch) | |
tree | d6f25e08d77a45c70754d7e2b91a7be9897b0ce3 /crates/ra_flycheck/src/lib.rs | |
parent | f01846b8eec9c8084153cb3cd6aff6e15608a01f (diff) |
Simplify error handing
Diffstat (limited to 'crates/ra_flycheck/src/lib.rs')
-rw-r--r-- | crates/ra_flycheck/src/lib.rs | 55 |
1 files changed, 17 insertions, 38 deletions
diff --git a/crates/ra_flycheck/src/lib.rs b/crates/ra_flycheck/src/lib.rs index f099bdfe8..13494a731 100644 --- a/crates/ra_flycheck/src/lib.rs +++ b/crates/ra_flycheck/src/lib.rs | |||
@@ -4,8 +4,8 @@ | |||
4 | mod conv; | 4 | mod conv; |
5 | 5 | ||
6 | use std::{ | 6 | use std::{ |
7 | env, error, fmt, | 7 | env, |
8 | io::{BufRead, BufReader}, | 8 | io::{self, BufRead, BufReader}, |
9 | path::PathBuf, | 9 | path::PathBuf, |
10 | process::{Command, Stdio}, | 10 | process::{Command, Stdio}, |
11 | time::Instant, | 11 | time::Instant, |
@@ -279,27 +279,12 @@ enum CheckEvent { | |||
279 | End, | 279 | End, |
280 | } | 280 | } |
281 | 281 | ||
282 | #[derive(Debug)] | ||
283 | pub struct CargoError(String); | ||
284 | |||
285 | impl fmt::Display for CargoError { | ||
286 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
287 | write!(f, "Cargo failed: {}", self.0) | ||
288 | } | ||
289 | } | ||
290 | impl error::Error for CargoError {} | ||
291 | |||
292 | fn run_cargo( | 282 | fn run_cargo( |
293 | mut command: Command, | 283 | mut command: Command, |
294 | on_message: &mut dyn FnMut(cargo_metadata::Message) -> bool, | 284 | on_message: &mut dyn FnMut(cargo_metadata::Message) -> bool, |
295 | ) -> Result<(), CargoError> { | 285 | ) -> io::Result<()> { |
296 | dbg!(&command); | 286 | let mut child = |
297 | let mut child = command | 287 | command.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null()).spawn()?; |
298 | .stdout(Stdio::piped()) | ||
299 | .stderr(Stdio::null()) | ||
300 | .stdin(Stdio::null()) | ||
301 | .spawn() | ||
302 | .expect("couldn't launch cargo"); | ||
303 | 288 | ||
304 | // We manually read a line at a time, instead of using serde's | 289 | // We manually read a line at a time, instead of using serde's |
305 | // stream deserializers, because the deserializer cannot recover | 290 | // stream deserializers, because the deserializer cannot recover |
@@ -313,13 +298,7 @@ fn run_cargo( | |||
313 | let mut read_at_least_one_message = false; | 298 | let mut read_at_least_one_message = false; |
314 | 299 | ||
315 | for line in stdout.lines() { | 300 | for line in stdout.lines() { |
316 | let line = match line { | 301 | let line = line?; |
317 | Ok(line) => line, | ||
318 | Err(err) => { | ||
319 | log::error!("Couldn't read line from cargo: {}", err); | ||
320 | continue; | ||
321 | } | ||
322 | }; | ||
323 | 302 | ||
324 | let message = serde_json::from_str::<cargo_metadata::Message>(&line); | 303 | let message = serde_json::from_str::<cargo_metadata::Message>(&line); |
325 | let message = match message { | 304 | let message = match message { |
@@ -340,20 +319,20 @@ fn run_cargo( | |||
340 | // It is okay to ignore the result, as it only errors if the process is already dead | 319 | // It is okay to ignore the result, as it only errors if the process is already dead |
341 | let _ = child.kill(); | 320 | let _ = child.kill(); |
342 | 321 | ||
343 | let err_msg = match child.wait() { | 322 | let exit_status = child.wait()?; |
344 | Ok(exit_code) if !exit_code.success() && !read_at_least_one_message => { | 323 | if !exit_status.success() && !read_at_least_one_message { |
345 | // FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment: | 324 | // FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment: |
346 | // https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298 | 325 | // https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298 |
326 | return Err(io::Error::new( | ||
327 | io::ErrorKind::Other, | ||
347 | format!( | 328 | format!( |
348 | "the command produced no valid metadata (exit code: {:?}): {:?}", | 329 | "the command produced no valid metadata (exit code: {:?}): {:?}", |
349 | exit_code, command | 330 | exit_status, command |
350 | ) | 331 | ), |
351 | } | 332 | )); |
352 | Err(err) => format!("io error: {:?}", err), | 333 | } |
353 | Ok(_) => return Ok(()), | ||
354 | }; | ||
355 | 334 | ||
356 | Err(CargoError(err_msg)) | 335 | Ok(()) |
357 | } | 336 | } |
358 | 337 | ||
359 | fn cargo_binary() -> String { | 338 | fn cargo_binary() -> String { |