capy_core/event_error.rs
1use crate::{AppError, ValidationErrors};
2use axum::http::StatusCode;
3
4/// An intentional, public application policy rejection. Only these statuses may
5/// be exposed by listeners. Codes and messages must be static, safe application
6/// text, never dependency errors, credentials or account-existence diagnostics.
7#[derive(Clone, Debug, thiserror::Error)]
8#[non_exhaustive]
9pub enum EventRejection {
10 /// Policy forbids the operation (403).
11 #[error("{message}")]
12 Forbidden {
13 /// Stable application-owned machine code.
14 code: &'static str,
15 /// Safe public explanation.
16 message: &'static str,
17 },
18 /// Application state conflicts with the operation (409).
19 #[error("{message}")]
20 Conflict {
21 /// Stable application-owned machine code.
22 code: &'static str,
23 /// Safe public explanation.
24 message: &'static str,
25 },
26 /// Application validation failed (422). Field messages are explicitly public.
27 #[error("Validation failed")]
28 Validation {
29 /// Stable application-owned machine code.
30 code: &'static str,
31 /// Safe field messages; do not reflect secrets or internal error strings.
32 errors: ValidationErrors,
33 },
34 /// The application has temporarily closed this operation (503).
35 #[error("{message}")]
36 Unavailable {
37 /// Stable application-owned machine code.
38 code: &'static str,
39 /// Safe public explanation.
40 message: &'static str,
41 },
42}
43impl EventRejection {
44 /// Stable application code included in the JSON error envelope.
45 pub fn code(&self) -> &'static str {
46 match self {
47 Self::Forbidden { code, .. }
48 | Self::Conflict { code, .. }
49 | Self::Validation { code, .. }
50 | Self::Unavailable { code, .. } => code,
51 }
52 }
53 /// Intentional HTTP response status.
54 pub fn status(&self) -> StatusCode {
55 match self {
56 Self::Forbidden { .. } => StatusCode::FORBIDDEN,
57 Self::Conflict { .. } => StatusCode::CONFLICT,
58 Self::Validation { .. } => StatusCode::UNPROCESSABLE_ENTITY,
59 Self::Unavailable { .. } => StatusCode::SERVICE_UNAVAILABLE,
60 }
61 }
62}
63
64/// Listener outcome. Only `Reject` exposes application-authored public text.
65/// `?` on database/anyhow/unexpected application errors takes the private failure
66/// path. A nested operation's already-typed `AppError::Rejected` remains a rejection.
67#[derive(Debug, thiserror::Error)]
68pub enum EventError {
69 /// Expected application policy rejection; the entire transition must roll back.
70 #[error("Application rejected lifecycle operation")]
71 Reject(#[from] EventRejection),
72 /// Unexpected failure; never expose its message as an application rejection.
73 #[error("Lifecycle listener failed")]
74 Failure(#[source] AppError),
75}
76impl From<AppError> for EventError {
77 fn from(error: AppError) -> Self {
78 match error {
79 AppError::Rejected(problem) => Self::Reject(problem),
80 error => Self::Failure(error),
81 }
82 }
83}
84impl From<sea_orm::DbErr> for EventError {
85 fn from(error: sea_orm::DbErr) -> Self {
86 Self::Failure(error.into())
87 }
88}
89impl From<anyhow::Error> for EventError {
90 fn from(error: anyhow::Error) -> Self {
91 Self::Failure(error.into())
92 }
93}
94/// Result returned by a transactional lifecycle listener.
95pub type EventResult = std::result::Result<(), EventError>;