43 lines
1.5 KiB
Rust
43 lines
1.5 KiB
Rust
use actix_web::http::StatusCode;
|
|
use actix_web::ResponseError;
|
|
use api::data_type::DataType;
|
|
use thiserror::Error;
|
|
|
|
#[derive(Error, Debug)]
|
|
pub enum Error {
|
|
#[error("Command Not Found {0}")]
|
|
CommandNotFound(String),
|
|
#[error("Incorrect Number of Parameters Specified. {expected} expected. {actual} found.")]
|
|
IncorrectParameterCount { expected: usize, actual: usize },
|
|
#[error("Missing Parameter {0}.")]
|
|
MisingParameter(String),
|
|
#[error("Incorrect Parameter Type for {name}. {expected_type:?} expected.")]
|
|
WrongParameterType {
|
|
name: String,
|
|
expected_type: DataType,
|
|
},
|
|
#[error("No Command Receiver")]
|
|
NoCommandReceiver,
|
|
#[error("Failed to Send")]
|
|
FailedToSend,
|
|
#[error("Failed to Receive Command Response")]
|
|
FailedToReceiveResponse,
|
|
#[error("Command Failure: {0}")]
|
|
CommandFailure(String),
|
|
}
|
|
|
|
impl ResponseError for Error {
|
|
fn status_code(&self) -> StatusCode {
|
|
match *self {
|
|
Error::CommandNotFound(_) => StatusCode::NOT_FOUND,
|
|
Error::IncorrectParameterCount { .. } => StatusCode::BAD_REQUEST,
|
|
Error::MisingParameter(_) => StatusCode::BAD_REQUEST,
|
|
Error::WrongParameterType { .. } => StatusCode::BAD_REQUEST,
|
|
Error::NoCommandReceiver => StatusCode::SERVICE_UNAVAILABLE,
|
|
Error::FailedToSend => StatusCode::INTERNAL_SERVER_ERROR,
|
|
Error::FailedToReceiveResponse => StatusCode::INTERNAL_SERVER_ERROR,
|
|
Error::CommandFailure(_) => StatusCode::BAD_REQUEST,
|
|
}
|
|
}
|
|
}
|