Skip to content

Commit

Permalink
Replaced boxed errors with anyhow::Error
Browse files Browse the repository at this point in the history
Changelog: Changed
  • Loading branch information
Florian Guggi authored and florg-32 committed Aug 4, 2024
1 parent 7d23833 commit 78cedf8
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 25 deletions.
76 changes: 76 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serialport = "4.2.2"
test-case = "3.3.1"
tar = "0.4.40"
thiserror = "1.0.62"
anyhow = { version = "1.0.86", features = ["backtrace"] }

[dependencies.filevec]
path = "lib/filevec"
Expand Down
11 changes: 6 additions & 5 deletions src/command/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{CommandError, CommandResult, SyncExecutionContext};
use crate::communication::{CEPPacket, CommunicationHandle};
use anyhow::anyhow;
use std::time::Duration;

pub fn check_length(
Expand All @@ -12,9 +13,9 @@ pub fn check_length(
Ok(())
} else {
com.send_packet(&CEPPacket::Nack)?;
Err(CommandError::ProtocolViolation(
format!("Received command with {actual_len} bytes, expected {n}").into(),
))
Err(CommandError::ProtocolViolation(anyhow!(
"Received command with {actual_len} bytes, expected {n}"
)))
}
}

Expand Down Expand Up @@ -48,10 +49,10 @@ pub fn terminate_student_program(exec: &mut SyncExecutionContext) -> CommandResu
.take()
.unwrap()
.join()
.or(Err(CommandError::NonRecoverable("Supervisor thread panicked".into())))?;
.or(Err(CommandError::NonRecoverable(anyhow!("Supervisor thread panicked"))))?;
return Ok(());
}
}

Err(CommandError::NonRecoverable("Supervisor thread did not finish in time".into()))
Err(CommandError::NonRecoverable(anyhow!("Supervisor thread did not finish in time")))
}
14 changes: 6 additions & 8 deletions src/command/error.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::communication::CommunicationError;

type BoxedError = Box<dyn std::error::Error>;

#[derive(Debug, thiserror::Error)]
pub enum CommandError {
#[error("Non-recoverable: {0}")]
NonRecoverable(BoxedError),
#[error("External: {0}")]
External(BoxedError),
#[error("Protocol Violation: {0}")]
ProtocolViolation(BoxedError),
#[error("Non-recoverable: {0:?}")]
NonRecoverable(anyhow::Error),
#[error("External: {0:?}")]
External(anyhow::Error),
#[error("Protocol Violation: {0:?}")]
ProtocolViolation(anyhow::Error),
}

impl From<std::io::Error> for CommandError {
Expand Down
3 changes: 2 additions & 1 deletion src/command/execute_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
},
communication::{CEPPacket, CommunicationHandle},
};
use anyhow::anyhow;
use std::{
io::ErrorKind,
path::{Path, PathBuf},
Expand Down Expand Up @@ -73,7 +74,7 @@ pub fn execute_program(
fn create_student_process(program_id: u16, timestamp: u32) -> Result<Popen, CommandError> {
let program_path = format!("./archives/{program_id}/main.py");
if !Path::new(&program_path).exists() {
return Err(CommandError::ProtocolViolation("Could not find matching program".into()));
return Err(CommandError::ProtocolViolation(anyhow!("Could not find matching program")));
}

// TODO run the program from a student user (setuid)
Expand Down
11 changes: 6 additions & 5 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod store_archive;
mod update_time;

use crate::communication::{CEPPacket, CommunicationHandle};
use anyhow::anyhow;
pub use common::*;
pub use error::CommandError;
use execute_program::execute_program;
Expand Down Expand Up @@ -50,13 +51,13 @@ pub fn process_command(
) -> CommandResult {
let packet = com.receive_packet()?;
let CEPPacket::Data(data) = packet else {
return Err(CommandError::NonRecoverable(
format!("Received {packet:?} as command start, expected Data").into(),
));
return Err(CommandError::NonRecoverable(anyhow!(
"Received {packet:?} as command start, expected Data"
)));
};

if data.is_empty() {
return Err(CommandError::ProtocolViolation("No data sent with data packet".into()));
return Err(CommandError::ProtocolViolation(anyhow!("Received empty data packet")));
}

match data.first().unwrap() {
Expand All @@ -67,7 +68,7 @@ pub fn process_command(
0x05 => return_result(&data, com, exec)?,
0x06 => update_time(&data, com, exec)?,
b => {
return Err(CommandError::ProtocolViolation(format!("Unknown command {b:#x}").into()));
return Err(CommandError::ProtocolViolation(anyhow!("Unknown command {b:#x}")));
}
};

Expand Down
7 changes: 4 additions & 3 deletions src/command/return_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
command::{check_length, CommandError, Event, ResultId, COMMAND_TIMEOUT},
communication::{CEPPacket, CommunicationHandle},
};
use anyhow::anyhow;

/// Handles a complete return result command. The result tar file is only deleted if a final Ack is
/// received.
Expand All @@ -19,9 +20,9 @@ pub fn return_result(

if !std::path::Path::new(&result_path).exists() {
com.send_packet(&CEPPacket::Nack)?;
return Err(CommandError::ProtocolViolation(
format!("Result {program_id}:{timestamp} does not exist").into(),
));
return Err(CommandError::ProtocolViolation(anyhow!(
"Result {program_id}:{timestamp} does not exist"
)));
}

let bytes = std::fs::read(result_path)?;
Expand Down
3 changes: 2 additions & 1 deletion src/command/store_archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
command::check_length,
communication::{CEPPacket, CommunicationHandle},
};
use anyhow::anyhow;
use std::{io::Write, process::Command};

/// This function implements the Store Archive command, including the reception of the archive itself
Expand Down Expand Up @@ -49,7 +50,7 @@ fn unpack_archive(folder: &str, bytes: &[u8]) -> CommandResult {
match exit_status {
Ok(status) => {
if !status.success() {
return Err(CommandError::NonRecoverable("unzip failed".into()));
return Err(CommandError::NonRecoverable(anyhow!("unzip failed")));
}
}
Err(err) => {
Expand Down
3 changes: 2 additions & 1 deletion src/command/update_time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{check_length, CommandError, CommandResult, SyncExecutionContext};
use crate::communication::{CEPPacket, CommunicationHandle};
use anyhow::anyhow;
use std::process::Command;

/// Handles the update time command
Expand All @@ -20,7 +21,7 @@ pub fn update_time(
fn set_system_time(s_since_epoch: i32) -> CommandResult {
let exit_status = Command::new("date").arg("-s").arg(format!("@{s_since_epoch}")).status()?;
if !exit_status.success() {
return Err(CommandError::NonRecoverable("date utility failed".into()));
return Err(CommandError::NonRecoverable(anyhow!("date utility failed")));
}

Ok(())
Expand Down
1 change: 0 additions & 1 deletion src/communication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ impl From<CEPParseError> for CommunicationError {
}
}


#[allow(clippy::needless_pass_by_value)]
#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 78cedf8

Please sign in to comment.