P0: config, DTOs, source traits, fakes, error, state and module stubs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
aa9b0da962
commit
1802f97d94
22 files changed
+1736
No files matched your search
+135
@@ -0,0 +1,135 @@
|
||||
//! Application error type shared by every surface.
|
||||
//!
|
||||
//! Sources return `AppError`; REST renders it through [`IntoResponse`], MCP
|
||||
//! through `From<AppError> for rmcp::ErrorData`.
|
||||
|
||||
use axum::http::{header, HeaderValue, StatusCode};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::Json;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Clone, thiserror::Error)]
|
||||
pub enum AppError {
|
||||
/// Unknown unit, container, route target.
|
||||
#[error("not found: {0}")]
|
||||
NotFound(String),
|
||||
/// Invalid input (bad unit name, out-of-range limit, bad priority).
|
||||
#[error("bad request: {0}")]
|
||||
BadRequest(String),
|
||||
/// Missing, malformed, unknown or revoked bearer token.
|
||||
#[error("unauthorized: {0}")]
|
||||
Unauthorized(String),
|
||||
/// A backend (D-Bus, journald, Docker socket, DB) is absent or denied.
|
||||
#[error("unavailable: {0}")]
|
||||
Unavailable(String),
|
||||
/// Anything else. Details are logged, not necessarily exposed.
|
||||
#[error("internal error: {0}")]
|
||||
Internal(String),
|
||||
}
|
||||
|
||||
pub type AppResult<T> = Result<T, AppError>;
|
||||
|
||||
impl AppError {
|
||||
/// Stable machine-readable code used in REST bodies and MCP error data.
|
||||
pub fn code(&self) -> &'static str {
|
||||
match self {
|
||||
Self::NotFound(_) => "not_found",
|
||||
Self::BadRequest(_) => "bad_request",
|
||||
Self::Unauthorized(_) => "unauthorized",
|
||||
Self::Unavailable(_) => "unavailable",
|
||||
Self::Internal(_) => "internal",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn status(&self) -> StatusCode {
|
||||
match self {
|
||||
Self::NotFound(_) => StatusCode::NOT_FOUND,
|
||||
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
|
||||
Self::Unauthorized(_) => StatusCode::UNAUTHORIZED,
|
||||
Self::Unavailable(_) => StatusCode::SERVICE_UNAVAILABLE,
|
||||
Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
|
||||
/// The bare message without the variant prefix.
|
||||
pub fn message(&self) -> &str {
|
||||
match self {
|
||||
Self::NotFound(m)
|
||||
| Self::BadRequest(m)
|
||||
| Self::Unauthorized(m)
|
||||
| Self::Unavailable(m)
|
||||
| Self::Internal(m) => m,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// REST error body: `{"error": {"code": "...", "message": "..."}}`.
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct ErrorBody<'a> {
|
||||
pub error: ErrorDetail<'a>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct ErrorDetail<'a> {
|
||||
pub code: &'a str,
|
||||
pub message: &'a str,
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
fn into_response(self) -> Response {
|
||||
let body = ErrorBody {
|
||||
error: ErrorDetail {
|
||||
code: self.code(),
|
||||
message: self.message(),
|
||||
},
|
||||
};
|
||||
let mut resp = (self.status(), Json(body)).into_response();
|
||||
if matches!(self, AppError::Unauthorized(_)) {
|
||||
resp.headers_mut()
|
||||
.insert(header::WWW_AUTHENTICATE, HeaderValue::from_static("Bearer"));
|
||||
}
|
||||
resp
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AppError> for rmcp::ErrorData {
|
||||
fn from(err: AppError) -> Self {
|
||||
let data = Some(serde_json::json!({ "code": err.code() }));
|
||||
let msg = err.message().to_owned();
|
||||
match err {
|
||||
AppError::NotFound(_) => rmcp::ErrorData::resource_not_found(msg, data),
|
||||
AppError::BadRequest(_) => rmcp::ErrorData::invalid_params(msg, data),
|
||||
AppError::Unauthorized(_) => rmcp::ErrorData::invalid_request(msg, data),
|
||||
AppError::Unavailable(_) | AppError::Internal(_) => {
|
||||
rmcp::ErrorData::internal_error(msg, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<sqlx::Error> for AppError {
|
||||
fn from(err: sqlx::Error) -> Self {
|
||||
AppError::Internal(format!("database: {err}"))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn status_and_code() {
|
||||
let e = AppError::NotFound("x".into());
|
||||
assert_eq!(e.status(), StatusCode::NOT_FOUND);
|
||||
assert_eq!(e.code(), "not_found");
|
||||
let r = AppError::Unauthorized("no".into()).into_response();
|
||||
assert_eq!(r.status(), StatusCode::UNAUTHORIZED);
|
||||
assert!(r.headers().contains_key(header::WWW_AUTHENTICATE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mcp_mapping() {
|
||||
let e: rmcp::ErrorData = AppError::BadRequest("bad".into()).into();
|
||||
assert_eq!(e.message, "bad");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user