handle custom programs' exit codes

This commit is contained in:
Edward Shen 2019-12-31 19:42:53 -05:00
parent 71df3394ad
commit 6e2deccf24
Signed by: edward
GPG key ID: F350507060ED6C90
2 changed files with 28 additions and 14 deletions

View file

@ -8,6 +8,7 @@ pub enum BunBunError {
ParseError(serde_yaml::Error),
WatchError(hotwatch::Error),
LoggerInitError(log::SetLoggerError),
CustomProgramError(String),
}
impl Error for BunBunError {}
@ -15,10 +16,11 @@ impl Error for BunBunError {}
impl fmt::Display for BunBunError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
BunBunError::IoError(e) => e.fmt(f),
BunBunError::ParseError(e) => e.fmt(f),
BunBunError::WatchError(e) => e.fmt(f),
BunBunError::LoggerInitError(e) => e.fmt(f),
Self::IoError(e) => e.fmt(f),
Self::ParseError(e) => e.fmt(f),
Self::WatchError(e) => e.fmt(f),
Self::LoggerInitError(e) => e.fmt(f),
Self::CustomProgramError(msg) => write!(f, "{}", msg),
}
}
}
@ -29,7 +31,7 @@ macro_rules! from_error {
($from:ty, $to:ident) => {
impl From<$from> for BunBunError {
fn from(e: $from) -> Self {
BunBunError::$to(e)
Self::$to(e)
}
}
};

View file

@ -137,8 +137,8 @@ pub async fn hop(
)
.finish(),
Err(e) => {
error!("Failed to resolve template for path {}: {}", path, e);
HttpResponse::InternalServerError().body("Something went wrong :(")
error!("Failed to redirect user for {}: {}", path, e);
HttpResponse::InternalServerError().body("Something went wrong :(\n")
}
}
}
@ -215,17 +215,29 @@ pub async fn index(data: StateData, req: HttpRequest) -> impl Responder {
)
}
/// Runs the executable with the user's input as a single argument. Returns Ok
/// so long as the executable was successfully executed. Returns an Error if the
/// file doesn't exist or bunbun did not have permission to read and execute the
/// file. Note that thi
fn resolve_path(
path: PathBuf,
args: &str,
) -> Result<Vec<u8>, crate::BunBunError> {
Ok(
// Unwrap is OK, we validated the path exists already
Command::new(path.canonicalize().unwrap())
.arg(args)
.output()?
.stdout,
)
// Unwrap is OK, we validated the path exists already
let output = Command::new(path.canonicalize().unwrap())
.arg(args)
.output()?;
if output.status.success() {
Ok(output.stdout)
} else {
error!(
"Program exit code for {} was not 0! Dumping standard error!",
path.display(),
);
let error = String::from_utf8_lossy(&output.stderr);
Err(crate::BunBunError::CustomProgramError(error.to_string()))
}
}
#[get("/bunbunsearch.xml")]