use actix_web::error::ResponseError; use actix_web::http::header::ContentType; use actix_web::http::StatusCode; use actix_web::HttpResponse; use thiserror::Error; use uuid::Uuid; #[derive(Error, Debug)] pub enum HttpServerResultError { #[error("Telemetry Name Not Found: {tlm}")] TlmNameNotFound { tlm: String }, #[error("Invalid Uuid: {uuid}")] InvalidUuid { uuid: String }, #[error("Telemetry Uuid Not Found: {uuid}")] TlmUuidNotFound { uuid: Uuid }, #[error("DateTime Parsing Error: {date_time}")] InvalidDateTime { date_time: String }, #[error("Timed out")] Timeout, #[error("Internal Error")] InternalError(#[from] anyhow::Error), #[error("Panel Uuid Not Found: {uuid}")] PanelUuidNotFound { uuid: Uuid }, #[error(transparent)] Command(#[from] crate::command::error::Error), } impl ResponseError for HttpServerResultError { fn status_code(&self) -> StatusCode { match self { HttpServerResultError::TlmNameNotFound { .. } => StatusCode::NOT_FOUND, HttpServerResultError::InvalidUuid { .. } => StatusCode::BAD_REQUEST, HttpServerResultError::TlmUuidNotFound { .. } => StatusCode::NOT_FOUND, HttpServerResultError::InvalidDateTime { .. } => StatusCode::BAD_REQUEST, HttpServerResultError::Timeout => StatusCode::GATEWAY_TIMEOUT, HttpServerResultError::InternalError { .. } => StatusCode::INTERNAL_SERVER_ERROR, HttpServerResultError::PanelUuidNotFound { .. } => StatusCode::NOT_FOUND, HttpServerResultError::Command(inner) => inner.status_code(), } } fn error_response(&self) -> HttpResponse { HttpResponse::build(self.status_code()) .insert_header(ContentType::plaintext()) .body(self.to_string()) } }