Files
Holzleitner---Backend--aktu…/crates/api/src/error.rs
Dennis Nemec 6a9b5872e1 Backend-Arbeitsstand: ERP-Sync, Lieferlebenszyklus, Reports + config.toml
Bringt das Backend vom initialen Skeleton auf den aktuellen Arbeitsstand
(Clean Architecture: domain → application → infrastructure → api).

Wesentliche Bereiche:
- ERP-Anbindung (MSSQL-Pull der Touren, Import-Scheduler, Rückschreiben)
- Lieferlebenszyklus: Scan/Hold/Cancel/Complete, Gutschriften, Notizen,
  Bild-Anhänge, Unterschriften, PDF-Lieferreport → DOCUframe
- Stammdaten: Kunden, Artikel, Lager, Zahlungsarten, Services
- Keycloak-JWT-Gate + Fahrer-Provisionierung via Admin-API
- Admin-API-Key-Gate (X-Admin-Api-Key) für Maschinen-Endpunkte

Jüngste Änderungen dieser Session:
- Belegspezifische Kontaktdaten: alle ERP-Adressen (Beleg-/Liefer-/
  Rechnungsadresse, Ansprechpartner, Kundenstamm) mit Telefon/Mobil/
  E-Mail werden gesynct (Migration 0029, MSSQL-Query, TourDetails)
- Konfiguration von .env (envy/dotenvy) auf config.toml (toml/serde)
  umgestellt; Vorlage config.example.toml, Pfad via HOLZLEITNER_CONFIG

Nicht im Repo (per .gitignore): config.toml (Secrets), data/ (Laufzeit-/
Kundendaten), demo.mp4, .claude/, variocontrol-ai/.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-01 17:52:58 +02:00

53 lines
1.9 KiB
Rust

use axum::Json;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use holzleitner_application::error::ApplicationError;
use holzleitner_application::ports::AuthError;
use serde_json::json;
/// HTTP-Adapter für Application-Fehler. Mappt jede Variante auf
/// einen Statuscode + JSON-Body. Verhindert, dass interne Details
/// (z. B. SQL-Fehler) versehentlich in die Antwort durchschlagen —
/// das eigentliche Detail wird über `tracing` geloggt.
pub struct ApiError(pub ApplicationError);
impl From<ApplicationError> for ApiError {
fn from(value: ApplicationError) -> Self {
Self(value)
}
}
impl From<AuthError> for ApiError {
fn from(err: AuthError) -> Self {
// Auth-Detail für Debugging loggen, aber NICHT an den Client geben —
// dort sehen wir nur „Unauthorized" / „internal error".
tracing::debug!(error = %err, "auth verification failed");
ApiError(err.into())
}
}
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
let (status, code) = match &self.0 {
ApplicationError::NotFound => (StatusCode::NOT_FOUND, "not_found"),
ApplicationError::Unauthorized => (StatusCode::UNAUTHORIZED, "unauthorized"),
ApplicationError::Forbidden => (StatusCode::FORBIDDEN, "forbidden"),
ApplicationError::Validation(_) => (StatusCode::BAD_REQUEST, "validation"),
ApplicationError::Conflict(_) => (StatusCode::CONFLICT, "conflict"),
ApplicationError::Repository(_)
| ApplicationError::External(_)
| ApplicationError::Unexpected(_) => {
tracing::error!(error = %self.0, "internal error");
(StatusCode::INTERNAL_SERVER_ERROR, "internal_error")
}
};
let body = Json(json!({
"error": code,
"message": self.0.to_string(),
}));
(status, body).into_response()
}
}