Skip to content

Commit

Permalink
server: error management
Browse files Browse the repository at this point in the history
  • Loading branch information
pnmadelaine committed Sep 26, 2023
1 parent 4f52844 commit 7cf4ec4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 6 additions & 4 deletions typhon/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum Error {
ScriptNotFound,
SecretsNotFound,
WrongRecipient,
Unexpected,
}

impl std::fmt::Display for Error {
Expand All @@ -26,6 +27,7 @@ impl std::fmt::Display for Error {
ScriptNotFound => write!(f, "Action script not found"),
SecretsNotFound => write!(f, "Secrets file not found"),
WrongRecipient => write!(f, "Secrets file uncrypted with wrong key"),
Unexpected => write!(f, "Unexpected error"),
}
}
}
Expand Down Expand Up @@ -88,13 +90,13 @@ pub async fn run(
.stdout(Stdio::piped())
.spawn()
.expect("command bwrap failed to start");
let mut stdin = child.stdin.take().unwrap(); // TODO: check if unwrap is safe
let mut stdout = child.stdout.take().unwrap(); // TODO: check if unwrap is safe
let mut stderr = child.stderr.take().unwrap(); // TODO: check if unwrap is safe
let mut stdin = child.stdin.take().ok_or(Error::Unexpected)?;
let mut stdout = child.stdout.take().ok_or(Error::Unexpected)?;
let mut stderr = child.stderr.take().ok_or(Error::Unexpected)?;
stdin
.write(action_input.to_string().as_bytes())
.await
.unwrap(); // TODO: check if unwrap is safe
.map_err(|_| Error::Unexpected)?;
drop(stdin); // send EOF

let mut res = String::new();
Expand Down
7 changes: 5 additions & 2 deletions typhon/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ pub enum Error {

impl Error {
pub fn is_internal(&self) -> bool {
use Error::*;
match self {
Error::UnexpectedDatabaseError(_) | Error::Todo => true,
ActionError(actions::Error::Unexpected) | UnexpectedDatabaseError(_) | Todo => true,
_ => false,
}
}
Expand Down Expand Up @@ -97,6 +98,9 @@ impl Into<typhon_types::responses::ResponseError> for Error {
fn into(self) -> typhon_types::responses::ResponseError {
use {typhon_types::responses::ResponseError::*, Error::*};
match self {
ActionError(actions::Error::Unexpected) | UnexpectedDatabaseError(_) | Todo => {
InternalError
}
BuildNotFound(_)
| EvaluationNotFound(_)
| JobNotFound(_)
Expand All @@ -113,7 +117,6 @@ impl Into<typhon_types::responses::ResponseError> for Error {
| ProjectAlreadyExists(_)
| LoginError
| LogNotFound(_) => BadRequest(format!("{}", self)),
Todo | UnexpectedDatabaseError(_) => InternalError,
}
}
}

0 comments on commit 7cf4ec4

Please sign in to comment.