Skip to content

Commit cc8113d

Browse files
committed
Simplify error handing
1 parent f01846b commit cc8113d

File tree

1 file changed

+17
-38
lines changed

1 file changed

+17
-38
lines changed

crates/ra_flycheck/src/lib.rs

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
mod conv;
55

66
use std::{
7-
env, error, fmt,
8-
io::{BufRead, BufReader},
7+
env,
8+
io::{self, BufRead, BufReader},
99
path::PathBuf,
1010
process::{Command, Stdio},
1111
time::Instant,
@@ -279,27 +279,12 @@ enum CheckEvent {
279279
End,
280280
}
281281

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-
292282
fn run_cargo(
293283
mut command: Command,
294284
on_message: &mut dyn FnMut(cargo_metadata::Message) -> bool,
295-
) -> Result<(), CargoError> {
296-
dbg!(&command);
297-
let mut child = command
298-
.stdout(Stdio::piped())
299-
.stderr(Stdio::null())
300-
.stdin(Stdio::null())
301-
.spawn()
302-
.expect("couldn't launch cargo");
285+
) -> io::Result<()> {
286+
let mut child =
287+
command.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null()).spawn()?;
303288

304289
// We manually read a line at a time, instead of using serde's
305290
// stream deserializers, because the deserializer cannot recover
@@ -313,13 +298,7 @@ fn run_cargo(
313298
let mut read_at_least_one_message = false;
314299

315300
for line in stdout.lines() {
316-
let line = match line {
317-
Ok(line) => line,
318-
Err(err) => {
319-
log::error!("Couldn't read line from cargo: {}", err);
320-
continue;
321-
}
322-
};
301+
let line = line?;
323302

324303
let message = serde_json::from_str::<cargo_metadata::Message>(&line);
325304
let message = match message {
@@ -340,20 +319,20 @@ fn run_cargo(
340319
// It is okay to ignore the result, as it only errors if the process is already dead
341320
let _ = child.kill();
342321

343-
let err_msg = match child.wait() {
344-
Ok(exit_code) if !exit_code.success() && !read_at_least_one_message => {
345-
// 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
322+
let exit_status = child.wait()?;
323+
if !exit_status.success() && !read_at_least_one_message {
324+
// FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment:
325+
// https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298
326+
return Err(io::Error::new(
327+
io::ErrorKind::Other,
347328
format!(
348329
"the command produced no valid metadata (exit code: {:?}): {:?}",
349-
exit_code, command
350-
)
351-
}
352-
Err(err) => format!("io error: {:?}", err),
353-
Ok(_) => return Ok(()),
354-
};
330+
exit_status, command
331+
),
332+
));
333+
}
355334

356-
Err(CargoError(err_msg))
335+
Ok(())
357336
}
358337

359338
fn cargo_binary() -> String {

0 commit comments

Comments
 (0)