Skip to content

Commit b055935

Browse files
committed
handle errors instead of using expect()
1 parent 520d92d commit b055935

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

crates/volta-core/src/monitor.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,55 @@ use crate::event::Event;
1111
/// send event to the spawned command process
1212
// if hook command is not configured, this is not called
1313
pub fn send_events(command: &str, events: &[Event]) {
14-
// TODO: make these expects into errors or whatever
15-
let events_json =
16-
serde_json::to_string_pretty(&events).expect("Problem serializing events JSON data");
17-
let mut events_file = NamedTempFile::new().expect("Could not create temp file for events");
18-
events_file
19-
.write_all(events_json.as_bytes())
20-
.expect("Writing data to file failed");
21-
// don't automatically delete this temp file please
22-
let path = events_file.into_temp_path();
23-
let tempfile_path = path.keep().expect("Could not persist temp file");
14+
match serde_json::to_string_pretty(&events) {
15+
Ok(events_json) => {
16+
if let Some(tempfile_path) = write_events_file(events_json.clone()) {
17+
if let Some(ref mut child_process) = spawn_process(command, tempfile_path) {
18+
if let Some(ref mut p_stdin) = child_process.stdin.as_mut() {
19+
if let Err(error) = writeln!(p_stdin, "{}", events_json) {
20+
debug!("Could not write events to executable stdin: {:?}", error);
21+
}
22+
}
23+
}
24+
}
25+
}
26+
Err(error) => {
27+
debug!("Could not serialize events data to JSON: {:?}", error);
28+
}
29+
}
30+
}
2431

25-
// spawn a child process, with the path to that temp file as an env var
26-
if let Some(ref mut child_process) = spawn_process(command, tempfile_path) {
27-
if let Some(ref mut p_stdin) = child_process.stdin.as_mut() {
28-
// still send the data over stdin
29-
writeln!(p_stdin, "{}", events_json).expect("Writing data to plugin failed!");
32+
// Write the events JSON to a file in the temporary directory
33+
fn write_events_file(events_json: String) -> Option<PathBuf> {
34+
match NamedTempFile::new() {
35+
Ok(mut events_file) => {
36+
match events_file.write_all(events_json.as_bytes()) {
37+
Ok(()) => {
38+
let path = events_file.into_temp_path();
39+
// if it's not persisted, the temp file will be automatically deleted
40+
// (and the executable won't be able to read it)
41+
match path.keep() {
42+
Ok(tempfile_path) => Some(tempfile_path),
43+
Err(error) => {
44+
debug!("Failed to persist temp file for events data: {:?}", error);
45+
None
46+
}
47+
}
48+
}
49+
Err(error) => {
50+
debug!("Failed to write events to the temp file: {:?}", error);
51+
None
52+
}
53+
}
54+
}
55+
Err(error) => {
56+
debug!("Failed to create a temp file for events data: {:?}", error);
57+
None
3058
}
3159
}
3260
}
3361

62+
// Spawn a child process to receive the events data, setting the path to the events file as an env var
3463
fn spawn_process(command: &str, tempfile_path: PathBuf) -> Option<Child> {
3564
command.split(' ').take(1).next().and_then(|executable| {
3665
let mut child = create_command(executable);
@@ -44,7 +73,7 @@ fn spawn_process(command: &str, tempfile_path: PathBuf) -> Option<Child> {
4473

4574
match child.spawn() {
4675
Err(err) => {
47-
debug!("Unable to run plugin command: '{}'\n{}", command, err);
76+
debug!("Unable to run executable command: '{}'\n{}", command, err);
4877
None
4978
}
5079
Ok(c) => Some(c),

0 commit comments

Comments
 (0)