Skip to main content

ferro_audit/
error.rs

1//! `AuditError` — the single error type for the ferro-audit crate.
2//!
3//! Every variant's `Display` impl prefixes `"audit: …"` so production
4//! log greps stay surgical (matches the workspace convention used by
5//! `GuardedError` with `"guarded: …"`, `WalletError` with `"wallet: …"`,
6//! `ConfigError` with `"config: …"`).
7
8use thiserror::Error;
9
10#[derive(Debug, Error)]
11pub enum AuditError {
12    /// The builder was executed with an empty `action` string. An audit
13    /// entry with no action is uninterpretable — the action verb is the
14    /// only required field on every audit entry (D-10, D-16).
15    #[error("audit: action is required")]
16    MissingAction,
17
18    /// Underlying SeaORM database error.
19    #[error("audit: db error: {0}")]
20    Db(#[from] sea_orm::DbErr),
21
22    /// JSON serialization / deserialization error on the `before` / `after`
23    /// payload. In practice this fires only if a caller hands the builder
24    /// a malformed `serde_json::Value` (very rare; included for completeness).
25    #[error("audit: json serialization error: {0}")]
26    Json(#[from] serde_json::Error),
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn error_missing_action_displays_message() {
35        assert_eq!(
36            AuditError::MissingAction.to_string(),
37            "audit: action is required"
38        );
39    }
40
41    #[test]
42    fn db_from_sea_orm_dberr() {
43        let db_err = sea_orm::DbErr::Custom("test".into());
44        let audit_err: AuditError = AuditError::from(db_err);
45        assert!(matches!(audit_err, AuditError::Db(_)));
46        assert!(audit_err.to_string().starts_with("audit: db error: "));
47    }
48
49    #[test]
50    fn json_from_serde_json_error() {
51        let json_err: serde_json::Error =
52            serde_json::from_str::<serde_json::Value>("not json").unwrap_err();
53        let audit_err: AuditError = AuditError::from(json_err);
54        assert!(matches!(audit_err, AuditError::Json(_)));
55        assert!(audit_err
56            .to_string()
57            .starts_with("audit: json serialization error: "));
58    }
59}