source-gamepanel/apps/daemon/src/error.rs

53 lines
1.5 KiB
Rust

use thiserror::Error;
#[derive(Error, Debug)]
pub enum DaemonError {
#[error("Docker error: {0}")]
Docker(#[from] bollard::errors::Error),
#[error("Server not found: {0}")]
ServerNotFound(String),
#[error("Server already exists: {0}")]
ServerAlreadyExists(String),
#[error("Invalid state transition: {current} -> {requested}")]
InvalidStateTransition { current: String, requested: String },
#[error("Filesystem error: {0}")]
Filesystem(String),
#[error("Path traversal attempt: {0}")]
PathTraversal(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Authentication failed")]
AuthFailed,
#[error("{0}")]
Internal(String),
}
impl From<DaemonError> for tonic::Status {
fn from(err: DaemonError) -> Self {
match &err {
DaemonError::ServerNotFound(_) => tonic::Status::not_found(err.to_string()),
DaemonError::ServerAlreadyExists(_) => {
tonic::Status::already_exists(err.to_string())
}
DaemonError::InvalidStateTransition { .. } => {
tonic::Status::failed_precondition(err.to_string())
}
DaemonError::PathTraversal(_) => {
tonic::Status::permission_denied(err.to_string())
}
DaemonError::AuthFailed => {
tonic::Status::unauthenticated(err.to_string())
}
_ => tonic::Status::internal(err.to_string()),
}
}
}