Add horoscope-api, synastry, and backend services
- horoscope-api: Swiss Ephemeris natal charts, timezone/place resolution, POST /v1/natal and POST /v1/synastry - backend: users with roles, email/password and Google login, saved charts, synastries and invites, analyses with credit ledger, public shares, admin - shared crates: astro-engine, db (migrations), horoscope-client, service-kit - docker-compose with postgres, legacy Java project for reference Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
use axum::Json;
|
||||
use axum::extract::rejection::{JsonRejection, QueryRejection};
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use geo_tz::{GeoError, PlaceMatch};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ApiError {
|
||||
#[error("{0}")]
|
||||
BadRequest(String),
|
||||
#[error(transparent)]
|
||||
Geo(#[from] GeoError),
|
||||
#[error("place not found: '{query}' in {country}")]
|
||||
PlaceNotFound { query: String, country: String, suggestions: Vec<PlaceMatch> },
|
||||
#[error("calculation failed: {0}")]
|
||||
Calculation(String),
|
||||
#[error("internal error: {0}")]
|
||||
Internal(String),
|
||||
/// An error in one person's data of a multi-person request.
|
||||
#[error("{person}: {source}")]
|
||||
Person { person: &'static str, source: Box<ApiError> },
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ErrorBody<'a> {
|
||||
error: ErrorDetail<'a>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ErrorDetail<'a> {
|
||||
code: &'a str,
|
||||
message: String,
|
||||
/// Which person of a multi-person request the error refers to.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
person: Option<&'a str>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
suggestions: Option<&'a [PlaceMatch]>,
|
||||
}
|
||||
|
||||
impl ApiError {
|
||||
pub fn for_person(self, person: &'static str) -> Self {
|
||||
ApiError::Person { person, source: Box::new(self) }
|
||||
}
|
||||
|
||||
fn status_and_code(&self) -> (StatusCode, &'static str) {
|
||||
match self {
|
||||
ApiError::Person { source, .. } => source.status_and_code(),
|
||||
ApiError::BadRequest(_) => (StatusCode::BAD_REQUEST, "bad_request"),
|
||||
ApiError::PlaceNotFound { .. } => (StatusCode::NOT_FOUND, "place_not_found"),
|
||||
ApiError::Calculation(_) => (StatusCode::UNPROCESSABLE_ENTITY, "calculation_failed"),
|
||||
ApiError::Internal(_) => (StatusCode::INTERNAL_SERVER_ERROR, "internal_error"),
|
||||
ApiError::Geo(e) => match e {
|
||||
GeoError::UnknownCountry(_) => (StatusCode::BAD_REQUEST, "unknown_country"),
|
||||
GeoError::UnknownTimeZone(_) => (StatusCode::BAD_REQUEST, "unknown_time_zone"),
|
||||
GeoError::UnknownPlaceId(_) => (StatusCode::NOT_FOUND, "unknown_place_id"),
|
||||
GeoError::PlaceCountryMismatch { .. } => (StatusCode::BAD_REQUEST, "place_country_mismatch"),
|
||||
GeoError::PlaceNotFound { .. } => (StatusCode::NOT_FOUND, "place_not_found"),
|
||||
GeoError::AmbiguousTime { .. } => (StatusCode::UNPROCESSABLE_ENTITY, "ambiguous_local_time"),
|
||||
GeoError::NonexistentTime { .. } => (StatusCode::UNPROCESSABLE_ENTITY, "nonexistent_local_time"),
|
||||
GeoError::InvalidDateTime(_) => (StatusCode::BAD_REQUEST, "invalid_date_time"),
|
||||
GeoError::NoTimeZone(_) => (StatusCode::UNPROCESSABLE_ENTITY, "no_time_zone"),
|
||||
GeoError::Data { .. } => (StatusCode::INTERNAL_SERVER_ERROR, "data_error"),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for ApiError {
|
||||
fn into_response(self) -> Response {
|
||||
let (status, code) = self.status_and_code();
|
||||
if status.is_server_error() {
|
||||
tracing::error!(error = %self, "request failed");
|
||||
}
|
||||
let (person, inner) = match &self {
|
||||
ApiError::Person { person, source } => (Some(*person), source.as_ref()),
|
||||
other => (None, other),
|
||||
};
|
||||
let suggestions = match inner {
|
||||
ApiError::PlaceNotFound { suggestions, .. } if !suggestions.is_empty() => Some(suggestions.as_slice()),
|
||||
_ => None,
|
||||
};
|
||||
let body = ErrorBody { error: ErrorDetail { code, message: self.to_string(), person, suggestions } };
|
||||
(status, Json(body)).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<JsonRejection> for ApiError {
|
||||
fn from(rejection: JsonRejection) -> Self {
|
||||
ApiError::BadRequest(rejection.body_text())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<QueryRejection> for ApiError {
|
||||
fn from(rejection: QueryRejection) -> Self {
|
||||
ApiError::BadRequest(rejection.body_text())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user