partition_sim/
errors.rs

1use std::process::Output;
2
3use axum::{http::StatusCode, response::IntoResponse};
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub enum PartitionSimError {
8    #[error("openssh Error: {0}")]
9    OpenSshError(#[from] openssh::Error),
10    #[error("openssh session uninitialized. Did you forget to connect?")]
11    SessionUninitialized,
12    #[error("peer not found: {0}. Are you sure you have the right peer id?")]
13    PeerNotFound(uuid::Uuid),
14    #[error("Command on the remote exited with a bad status code: {0}")]
15    CommandFailed(i32),
16    #[error("IO Error: {0}")]
17    IoError(#[from] std::io::Error),
18    #[error("Consul related error: {0}")]
19    ConsulError(String),
20    #[error("sshpass failed to copy id")]
21    SshCopyIdFailed,
22    #[error("Other error: {0}")]
23    Other(String),
24    #[error("Couldn't parse Uuid: {0}")]
25    UuidParseError(#[from] uuid::Error),
26    #[error(
27        "Command failed (with output): (status: {status_code}, stderr: {stderr}, stdout: {stdout})"
28    )]
29    CommandFailedWithOutput {
30        status_code: i32,
31        stderr: String,
32        stdout: String,
33    },
34}
35
36impl From<Output> for PartitionSimError {
37    fn from(value: Output) -> Self {
38        let stderr = String::from_utf8(value.stderr).unwrap();
39        let stdout = String::from_utf8(value.stdout).unwrap();
40        PartitionSimError::CommandFailedWithOutput {
41            status_code: value.status.code().unwrap(),
42            stderr,
43            stdout,
44        }
45    }
46}
47
48impl IntoResponse for PartitionSimError {
49    fn into_response(self) -> axum::response::Response {
50        let msg = format!("{}", self);
51        (StatusCode::INTERNAL_SERVER_ERROR, msg).into_response()
52    }
53}