handle custom programs' exit codes
This commit is contained in:
parent
71df3394ad
commit
6e2deccf24
2 changed files with 28 additions and 14 deletions
12
src/error.rs
12
src/error.rs
|
@ -8,6 +8,7 @@ pub enum BunBunError {
|
||||||
ParseError(serde_yaml::Error),
|
ParseError(serde_yaml::Error),
|
||||||
WatchError(hotwatch::Error),
|
WatchError(hotwatch::Error),
|
||||||
LoggerInitError(log::SetLoggerError),
|
LoggerInitError(log::SetLoggerError),
|
||||||
|
CustomProgramError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for BunBunError {}
|
impl Error for BunBunError {}
|
||||||
|
@ -15,10 +16,11 @@ impl Error for BunBunError {}
|
||||||
impl fmt::Display for BunBunError {
|
impl fmt::Display for BunBunError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
BunBunError::IoError(e) => e.fmt(f),
|
Self::IoError(e) => e.fmt(f),
|
||||||
BunBunError::ParseError(e) => e.fmt(f),
|
Self::ParseError(e) => e.fmt(f),
|
||||||
BunBunError::WatchError(e) => e.fmt(f),
|
Self::WatchError(e) => e.fmt(f),
|
||||||
BunBunError::LoggerInitError(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) => {
|
($from:ty, $to:ident) => {
|
||||||
impl From<$from> for BunBunError {
|
impl From<$from> for BunBunError {
|
||||||
fn from(e: $from) -> Self {
|
fn from(e: $from) -> Self {
|
||||||
BunBunError::$to(e)
|
Self::$to(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -137,8 +137,8 @@ pub async fn hop(
|
||||||
)
|
)
|
||||||
.finish(),
|
.finish(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Failed to resolve template for path {}: {}", path, e);
|
error!("Failed to redirect user for {}: {}", path, e);
|
||||||
HttpResponse::InternalServerError().body("Something went wrong :(")
|
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(
|
fn resolve_path(
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
args: &str,
|
args: &str,
|
||||||
) -> Result<Vec<u8>, crate::BunBunError> {
|
) -> Result<Vec<u8>, crate::BunBunError> {
|
||||||
Ok(
|
|
||||||
// Unwrap is OK, we validated the path exists already
|
// Unwrap is OK, we validated the path exists already
|
||||||
Command::new(path.canonicalize().unwrap())
|
let output = Command::new(path.canonicalize().unwrap())
|
||||||
.arg(args)
|
.arg(args)
|
||||||
.output()?
|
.output()?;
|
||||||
.stdout,
|
|
||||||
)
|
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")]
|
#[get("/bunbunsearch.xml")]
|
||||||
|
|
Loading…
Reference in a new issue