CI / test (push) Successful in 46s
Mail in Trash is purged after TRASH_RETENTION_DAYS (default 30, 0
disables), hourly. Stored files are content-addressed and shared, so a
file is only removed once nothing references it — and not if written or
reused in the last hour, since ingest writes a file before inserting the
row that points at it. Reusing an existing blob now refreshes its mtime
so that grace period protects it too. Trash rows report when they purge.
"Stop processing further rules" moves from an action to a per-rule
option (stop_after_match): it's a property of the rule, not something
done to a message. Migration 0007 converts existing stop actions, and a
stop submitted as an action is now rejected.
Rules can be run now against existing mail outside Trash, disabled ones
included so a rule can be tried before it's switched on. It pages by
key, one transaction per page, and only re-reads stored raw messages
when the rule tests a header.
All JSON bodies now go through an AppJson extractor so malformed or
mistyped bodies return the API's {"error": …} shape. Axum's default was
a plain-text 422 the webapp could only show as "request failed: 422" —
losing the useful part, e.g. "unknown variant `stop`".
Verified live: the stop flag blocks a lower rule on arriving mail; run
now on a disabled header rule archived 2 of 4 messages; a purge removed
an expired message and its unique raw file while keeping an attachment
file still shared with a live copy, with no orphaned rows.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
2.0 KiB
Rust
62 lines
2.0 KiB
Rust
use axum::{
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
Json,
|
|
};
|
|
use serde_json::json;
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
pub enum ApiError {
|
|
#[error("not found")]
|
|
NotFound,
|
|
#[error("unauthorized")]
|
|
Unauthorized,
|
|
#[error("bad request: {0}")]
|
|
BadRequest(String),
|
|
#[error(transparent)]
|
|
Internal(#[from] anyhow::Error),
|
|
#[error(transparent)]
|
|
Sqlx(#[from] sqlx::Error),
|
|
}
|
|
|
|
impl IntoResponse for ApiError {
|
|
fn into_response(self) -> Response {
|
|
let (status, message) = match &self {
|
|
ApiError::NotFound => (StatusCode::NOT_FOUND, self.to_string()),
|
|
ApiError::Unauthorized => (StatusCode::UNAUTHORIZED, self.to_string()),
|
|
ApiError::BadRequest(_) => (StatusCode::BAD_REQUEST, self.to_string()),
|
|
ApiError::Sqlx(e) => {
|
|
tracing::error!("db error: {e:#}");
|
|
(
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
"internal server error".to_string(),
|
|
)
|
|
}
|
|
ApiError::Internal(e) => {
|
|
tracing::error!("internal error: {e:#}");
|
|
(
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
"internal server error".to_string(),
|
|
)
|
|
}
|
|
};
|
|
(status, Json(json!({ "error": message }))).into_response()
|
|
}
|
|
}
|
|
|
|
pub type ApiResult<T> = Result<T, ApiError>;
|
|
|
|
/// JSON body extractor whose failures use the API's `{"error": …}` shape.
|
|
/// Axum's own `Json` rejects malformed or mistyped bodies with a plain-text
|
|
/// 422, which clients can't show properly — and its message is exactly the
|
|
/// useful part ("unknown variant `stop`, expected one of …").
|
|
#[derive(axum::extract::FromRequest)]
|
|
#[from_request(via(axum::Json), rejection(ApiError))]
|
|
pub struct AppJson<T>(pub T);
|
|
|
|
impl From<axum::extract::rejection::JsonRejection> for ApiError {
|
|
fn from(rejection: axum::extract::rejection::JsonRejection) -> Self {
|
|
ApiError::BadRequest(rejection.body_text())
|
|
}
|
|
}
|