Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix process spawning error handling #3349

Merged
merged 2 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,8 @@ fn goto_window(cx: &mut Context, align: Align) {
let last_line = view.last_line(doc);

let line = match align {
Align::Top => (view.offset.row + scrolloff + count),
Align::Center => (view.offset.row + ((last_line - view.offset.row) / 2)),
Align::Top => view.offset.row + scrolloff + count,
Align::Center => view.offset.row + ((last_line - view.offset.row) / 2),
Align::Bottom => last_line.saturating_sub(scrolloff + count),
}
.max(view.offset.row + scrolloff)
Expand Down Expand Up @@ -4578,8 +4578,18 @@ fn shell_impl(
}
let output = process.wait_with_output()?;

if !output.stderr.is_empty() {
log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr));
if !output.status.success() {
if !output.stderr.is_empty() {
let err = String::from_utf8_lossy(&output.stderr).to_string();
log::error!("Shell error: {}", err);
bail!("Shell error: {}", err);
}
bail!("Shell command failed");
} else if !output.stderr.is_empty() {
log::debug!(
"Command printed to stderr: {}",
String::from_utf8_lossy(&output.stderr).to_string()
);
}

let str = std::str::from_utf8(&output.stdout)
Expand Down
30 changes: 18 additions & 12 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,17 +440,22 @@ impl Document {
.await
.map_err(|_| FormatterError::WaitForOutputFailed)?;

if !output.stderr.is_empty() {
return Err(FormatterError::Stderr(
String::from_utf8_lossy(&output.stderr).to_string(),
));
}

if !output.status.success() {
return Err(FormatterError::NonZeroExitStatus);
if !output.stderr.is_empty() {
let err = String::from_utf8_lossy(&output.stderr).to_string();
log::error!("Formatter error: {}", err);
return Err(FormatterError::NonZeroExitStatus(Some(err)));
}

return Err(FormatterError::NonZeroExitStatus(None));
} else if !output.stderr.is_empty() {
log::debug!(
"Formatter printed to stderr: {}",
String::from_utf8_lossy(&output.stderr).to_string()
);
}

let str = String::from_utf8(output.stdout)
let str = std::str::from_utf8(&output.stdout)
.map_err(|_| FormatterError::InvalidUtf8Output)?;

Ok(helix_core::diff::compare_ropes(&text, &Rope::from(str)))
Expand Down Expand Up @@ -1092,10 +1097,9 @@ pub enum FormatterError {
},
BrokenStdin,
WaitForOutputFailed,
Stderr(String),
InvalidUtf8Output,
DiskReloadError(String),
NonZeroExitStatus,
NonZeroExitStatus(Option<String>),
}

impl std::error::Error for FormatterError {}
Expand All @@ -1108,10 +1112,12 @@ impl Display for FormatterError {
}
Self::BrokenStdin => write!(f, "Could not write to formatter stdin"),
Self::WaitForOutputFailed => write!(f, "Waiting for formatter output failed"),
Self::Stderr(output) => write!(f, "Formatter error: {}", output),
Self::InvalidUtf8Output => write!(f, "Invalid UTF-8 formatter output"),
Self::DiskReloadError(error) => write!(f, "Error reloading file from disk: {}", error),
Self::NonZeroExitStatus => write!(f, "Formatter exited with non zero exit status:"),
Self::NonZeroExitStatus(Some(output)) => write!(f, "Formatter error: {}", output),
Self::NonZeroExitStatus(None) => {
write!(f, "Formatter exited with non zero exit status")
}
}
}
}
Expand Down