Share error_response and default host allowlist; drop AppResult
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
1 parent
66eb91bf01
commit
d022ebeae5
5 files changed
+38
-60
No files matched your search
+4
-19
@@ -6,7 +6,6 @@
|
||||
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 {
|
||||
@@ -27,8 +26,6 @@ pub enum AppError {
|
||||
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 {
|
||||
@@ -64,26 +61,14 @@ impl AppError {
|
||||
}
|
||||
|
||||
/// 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,
|
||||
pub fn error_response(status: StatusCode, code: &str, message: &str) -> Response {
|
||||
let body = serde_json::json!({"error": {"code": code, "message": message}});
|
||||
(status, Json(body)).into_response()
|
||||
}
|
||||
|
||||
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();
|
||||
let mut resp = error_response(self.status(), self.code(), self.message());
|
||||
if matches!(self, AppError::Unauthorized(_)) {
|
||||
resp.headers_mut()
|
||||
.insert(header::WWW_AUTHENTICATE, HeaderValue::from_static("Bearer"));
|
||||
|
||||
Reference in new issue
Block a user