Adds jray-project as a submodule at scripts/vendor/jray-project, so this repo runs the same extractor as every other component rather than its own copy, and gains the system spec that defines the PR/SR requirements its register traces up to. scripts/traceability-gate.sh is a thin wrapper holding only what is specific to this repo: UR/DR prefixes, .rs sources, and REPO_ROOT — which the shared gate cannot infer once vendored, since its default resolves to the submodule itself. Each override fails silently in a way that looks like "no work done" rather than "misconfigured", so the wrapper documents why each is needed. Annotates 35 units with TRACES tags, on the code that decides rather than every helper it calls. Coverage is 23/32 (71.9%) with no orphan tags. The nine untraced are genuinely unimplemented: UR-007 is plugin-side, UR-008 is federation, and UR-015..018 are the pending SR-003 schema bump. The gate caught a real error in the first pass: several tags separated IDs of different types with commas. A comma joins IDs within one type; a pipe separates types. Fixed, and the diagnostics are now clean. MIN_COVERAGE stays 0 deliberately. The gate still fails on orphan tags, a >100% ratio, a register parsing to nothing, or an empty source scan — raise the threshold as a ratchet once the remaining work lands. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
85 lines
2.6 KiB
Rust
85 lines
2.6 KiB
Rust
//! API error type mapping onto the status codes §4 specifies.
|
|
|
|
use axum::http::StatusCode;
|
|
use axum::response::{IntoResponse, Response};
|
|
use axum::Json;
|
|
use serde::Serialize;
|
|
|
|
/// TRACES: DR-013 | SR-003
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum ApiError {
|
|
/// §6 stage 2 — malformed, unrecognised or forbidden field. The message
|
|
/// names the offending field so a client that forgets to strip `movie` or
|
|
/// `jellyfin_id` gets a hard, diagnosable `400` (§6).
|
|
#[error("{0}")]
|
|
BadRequest(String),
|
|
|
|
#[error("not found")]
|
|
NotFound,
|
|
|
|
/// §4 — identical `(identity, cut)` already exists from this contributor.
|
|
#[error("{0}")]
|
|
Conflict(String),
|
|
|
|
#[error("{0}")]
|
|
PayloadTooLarge(String),
|
|
|
|
#[error("missing or invalid API token")]
|
|
Unauthorized,
|
|
|
|
/// §5 — carries the `Retry-After` value in seconds.
|
|
#[error("rate limited")]
|
|
RateLimited { retry_after: u64 },
|
|
|
|
#[error("internal error")]
|
|
Internal(#[from] anyhow::Error),
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
struct ErrorBody {
|
|
error: String,
|
|
message: String,
|
|
}
|
|
|
|
impl IntoResponse for ApiError {
|
|
fn into_response(self) -> Response {
|
|
let (status, code) = match &self {
|
|
ApiError::BadRequest(_) => (StatusCode::BAD_REQUEST, "bad_request"),
|
|
ApiError::NotFound => (StatusCode::NOT_FOUND, "not_found"),
|
|
ApiError::Conflict(_) => (StatusCode::CONFLICT, "conflict"),
|
|
ApiError::PayloadTooLarge(_) => (StatusCode::PAYLOAD_TOO_LARGE, "payload_too_large"),
|
|
ApiError::Unauthorized => (StatusCode::UNAUTHORIZED, "unauthorized"),
|
|
ApiError::RateLimited { .. } => (StatusCode::TOO_MANY_REQUESTS, "rate_limited"),
|
|
ApiError::Internal(e) => {
|
|
// Internal detail is logged, never returned.
|
|
tracing::error!(error = ?e, "internal error");
|
|
(StatusCode::INTERNAL_SERVER_ERROR, "internal")
|
|
}
|
|
};
|
|
|
|
let body = Json(ErrorBody {
|
|
error: code.to_string(),
|
|
message: match &self {
|
|
ApiError::Internal(_) => "internal error".to_string(),
|
|
other => other.to_string(),
|
|
},
|
|
});
|
|
|
|
let mut resp = (status, body).into_response();
|
|
if let ApiError::RateLimited { retry_after } = self {
|
|
if let Ok(v) = retry_after.to_string().parse() {
|
|
resp.headers_mut().insert(axum::http::header::RETRY_AFTER, v);
|
|
}
|
|
}
|
|
resp
|
|
}
|
|
}
|
|
|
|
impl From<rusqlite::Error> for ApiError {
|
|
fn from(e: rusqlite::Error) -> Self {
|
|
ApiError::Internal(anyhow::Error::new(e))
|
|
}
|
|
}
|
|
|
|
pub type ApiResult<T> = Result<T, ApiError>;
|