Skip to main content

treetop_core/
error.rs

1use cedar_policy::{
2    ContextCreationError, EntityAttrEvaluationError, ParseErrors, RequestValidationError,
3    entities_errors::EntitiesError,
4};
5use serde::{Deserialize, Serialize};
6use thiserror::Error;
7
8/// Policy evaluation and validation errors.
9///
10/// Variants correspond to Cedar parse/eval errors and library validation
11/// errors. Call sites attach human-friendly context where possible.
12/// For example, `EntityAttrError` wraps attribute access failures.
13#[derive(Debug, Error, Serialize, Deserialize)]
14#[non_exhaustive]
15pub enum PolicyError {
16    /// Failed to parse Cedar policy text.
17    #[error("failed to parse policy: {0}")]
18    ParseError(String),
19
20    /// Error during policy evaluation.
21    #[error("evaluation error: {0}")]
22    EvalError(String),
23
24    /// Request validation failed (invalid principals, resources, or actions).
25    #[error("request validation error: {0}")]
26    RequestValidationError(String),
27
28    /// Context creation error during request processing.
29    #[error("context creation error: {0}")]
30    ContextError(String),
31
32    /// Error creating or manipulating Cedar entities.
33    #[error("entity error: {0}")]
34    EntityError(String),
35
36    /// Invalid format for a Cedar construct (string parsing failure).
37    #[error("invalid format: {0}")]
38    InvalidFormat(String),
39
40    /// Entity attribute evaluation or access error.
41    #[error("entity attribute error: {0}")]
42    EntityAttrError(String),
43
44    /// A policy-store layout or policy assignment is invalid.
45    #[error("policy-store configuration error: {0}")]
46    PolicyStoreConfigError(String),
47
48    /// A request cannot be routed to exactly one configured policy store.
49    #[error("policy-store routing error: {0}")]
50    PolicyStoreRoutingError(String),
51
52    /// A resource-labeling configuration violates the trusted-output contract.
53    #[error("label configuration error: {0}")]
54    LabelConfigError(String),
55}
56
57impl From<RequestValidationError> for PolicyError {
58    fn from(err: cedar_policy::RequestValidationError) -> Self {
59        PolicyError::RequestValidationError(err.to_string())
60    }
61}
62
63impl From<ParseErrors> for PolicyError {
64    fn from(err: ParseErrors) -> Self {
65        PolicyError::ParseError(err.to_string())
66    }
67}
68
69impl From<ContextCreationError> for PolicyError {
70    fn from(err: ContextCreationError) -> Self {
71        PolicyError::ContextError(err.to_string())
72    }
73}
74
75impl From<EntityAttrEvaluationError> for PolicyError {
76    fn from(err: EntityAttrEvaluationError) -> Self {
77        PolicyError::EntityAttrError(err.to_string())
78    }
79}
80
81impl From<EntitiesError> for PolicyError {
82    fn from(err: EntitiesError) -> Self {
83        PolicyError::EntityError(err.to_string())
84    }
85}