Files
telemetry_visualization/server/src/http/error.rs
2026-01-03 00:44:20 -05:00

50 lines
1.9 KiB
Rust

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),
#[error("Command Not Found: {cmd}")]
CmdNotFound { cmd: String },
}
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(),
HttpServerResultError::CmdNotFound { .. } => StatusCode::NOT_FOUND,
}
}
fn error_response(&self) -> HttpResponse {
HttpResponse::build(self.status_code())
.insert_header(ContentType::plaintext())
.body(self.to_string())
}
}