diff --git a/src/error.rs b/src/error.rs index 022e2cc..7eeb0cf 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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) } } }; diff --git a/src/routes.rs b/src/routes.rs index ae37df5..f8a43f2 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -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, 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")]