Skip to main content

agentstategraph_policy/
error.rs

1//! Error type for `PolicyStore` operations.
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum PolicyError {
7    #[error("policy not found: {0}")]
8    NotFound(String),
9
10    #[error("policy already exists at path {0}")]
11    AlreadyExists(String),
12
13    #[error("policy is already ratified: {0}")]
14    AlreadyRatified(String),
15
16    #[error("policy is not a proposal (already ratified or superseded): {0}")]
17    NotProposal(String),
18
19    #[error("invalid policy path: {0}")]
20    InvalidPath(String),
21
22    #[error("invalid policy: {0}")]
23    Invalid(String),
24
25    #[error("repository error: {0}")]
26    Repo(String),
27
28    #[error("serialization error: {0}")]
29    Serde(#[from] serde_json::Error),
30}
31
32impl PolicyError {
33    /// A stable, low-cardinality code for this error, which telemetry
34    /// records as `error.type`.
35    pub fn code(&self) -> &'static str {
36        match self {
37            PolicyError::NotFound(_) => "policy_not_found",
38            PolicyError::AlreadyExists(_) => "policy_already_exists",
39            PolicyError::AlreadyRatified(_) => "policy_already_ratified",
40            PolicyError::NotProposal(_) => "policy_not_proposal",
41            PolicyError::InvalidPath(_) => "invalid_policy_path",
42            PolicyError::Invalid(_) => "invalid_policy",
43            PolicyError::Repo(_) => "repository_error",
44            PolicyError::Serde(_) => "serialization_error",
45        }
46    }
47}
48
49impl From<agentstategraph::RepoError> for PolicyError {
50    fn from(e: agentstategraph::RepoError) -> Self {
51        PolicyError::Repo(e.to_string())
52    }
53}