Skip to content

Commit d2d35af

Browse files
committed
Handle unsuccessful exit status.
1 parent ac09da1 commit d2d35af

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/main.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
extern crate open;
2-
3-
use std::{
4-
env,
5-
io::{stderr, Write},
6-
process,
7-
};
1+
use std::{env, process};
82

93
fn main() {
104
let path_or_url = match env::args().nth(1) {
115
Some(arg) => arg,
126
None => {
13-
writeln!(stderr(), "usage: open <path-or-url>").ok();
7+
eprintln!("usage: open <path-or-url>");
148
process::exit(1);
159
}
1610
};
1711

18-
if let Err(err) = open::that(&path_or_url) {
19-
writeln!(
20-
stderr(),
21-
"An error occourred when opening '{}': {}",
22-
path_or_url,
23-
err
24-
)
25-
.ok();
26-
process::exit(3);
12+
match open::that(&path_or_url) {
13+
Ok(status) if status.success() => (),
14+
Ok(status) => match status.code() {
15+
Some(code) => open_error(code, &path_or_url, &format!("error code: {}", code)),
16+
None => open_error(3, &path_or_url, "error unknown"),
17+
},
18+
Err(err) => open_error(3, &path_or_url, &err.to_string()),
2719
}
2820
}
21+
22+
fn open_error(code: i32, path: &str, error_message: &str) {
23+
eprintln!(
24+
"An error occurred when opening '{}': {}",
25+
path, error_message
26+
);
27+
process::exit(code);
28+
}

0 commit comments

Comments
 (0)