1use crate::{EntityUid, PolicyId};
20pub use cedar_policy_core::ast::{
21 expression_construction_errors, restricted_expr_errors, ContainsUnknown,
22 ExpressionConstructionError, PartialValueToValueError, RestrictedExpressionError,
23};
24#[cfg(feature = "tpe")]
25use cedar_policy_core::entities::conformance::err::EntitySchemaConformanceError;
26#[cfg(feature = "entity-manifest")]
27use cedar_policy_core::entities::err::EntitiesError;
28pub use cedar_policy_core::evaluator::{evaluation_errors, EvaluationError};
29pub use cedar_policy_core::extensions::{
30 extension_function_lookup_errors, ExtensionFunctionLookupError,
31};
32pub use cedar_policy_core::validator::cedar_schema::{schema_warnings, SchemaWarning};
33#[cfg(feature = "entity-manifest")]
34pub use cedar_policy_core::validator::entity_manifest::slicing::EntitySliceError;
35#[cfg(feature = "entity-manifest")]
36use cedar_policy_core::validator::entity_manifest::{
37 self, PartialExpressionError, PartialRequestError, UnsupportedCedarFeatureError,
38};
39pub use cedar_policy_core::validator::{schema_errors, SchemaError};
40use cedar_policy_core::{ast, authorizer, est};
41use miette::Diagnostic;
42use ref_cast::RefCast;
43use smol_str::SmolStr;
44use thiserror::Error;
45use to_cedar_syntax_errors::NameCollisionsError;
46
47#[cfg(feature = "entity-manifest")]
48use super::ValidationResult;
49
50#[cfg(feature = "tpe")]
51pub use cedar_policy_core::tpe::err as tpe_err;
52
53pub mod entities_errors {
55 pub use cedar_policy_core::entities::err::{Duplicate, EntitiesError, TransitiveClosureError};
56}
57
58pub mod entities_json_errors {
60 pub use cedar_policy_core::entities::json::err::{
61 ActionParentIsNotAction, DuplicateKey, ExpectedExtnValue, ExpectedLiteralEntityRef,
62 ExtnCall0Arguments, JsonDeserializationError, JsonError, JsonSerializationError,
63 MissingImpliedConstructor, MissingRequiredRecordAttr, ParseEscape, ReservedKey, Residual,
64 TypeMismatch, UnexpectedRecordAttr, UnexpectedRestrictedExprKind,
65 };
66}
67
68pub mod conformance_errors {
70 pub use cedar_policy_core::entities::conformance::err::{
71 ActionDeclarationMismatch, EntitySchemaConformanceError, ExtensionFunctionLookup,
72 InvalidAncestorType, MissingRequiredEntityAttr, TypeMismatch, UndeclaredAction,
73 UnexpectedEntityAttr, UnexpectedEntityTag, UnexpectedEntityTypeError,
74 };
75}
76
77#[derive(Debug, Diagnostic, PartialEq, Eq, Error, Clone)]
79pub enum AuthorizationError {
80 #[error(transparent)]
82 #[diagnostic(transparent)]
83 PolicyEvaluationError(#[from] authorization_errors::PolicyEvaluationError),
84}
85
86pub mod authorization_errors {
88 use crate::{EvaluationError, PolicyId};
89 use cedar_policy_core::{ast, authorizer};
90 use miette::Diagnostic;
91 use ref_cast::RefCast;
92 use thiserror::Error;
93
94 #[derive(Debug, Diagnostic, PartialEq, Eq, Error, Clone)]
96 #[error("error while evaluating policy `{id}`: {error}")]
97 pub struct PolicyEvaluationError {
98 id: ast::PolicyID,
100 #[diagnostic(transparent)]
102 error: EvaluationError,
103 }
104
105 impl PolicyEvaluationError {
106 pub fn policy_id(&self) -> &PolicyId {
108 PolicyId::ref_cast(&self.id)
109 }
110
111 pub fn inner(&self) -> &EvaluationError {
113 &self.error
114 }
115
116 pub fn into_inner(self) -> EvaluationError {
118 self.error
119 }
120 }
121
122 #[doc(hidden)]
123 impl From<authorizer::AuthorizationError> for PolicyEvaluationError {
124 fn from(e: authorizer::AuthorizationError) -> Self {
125 match e {
126 authorizer::AuthorizationError::PolicyEvaluationError { id, error } => {
127 Self { id, error }
128 }
129 }
130 }
131 }
132}
133
134#[doc(hidden)]
135impl From<authorizer::AuthorizationError> for AuthorizationError {
136 fn from(value: authorizer::AuthorizationError) -> Self {
137 Self::PolicyEvaluationError(value.into())
138 }
139}
140
141#[derive(Debug, Diagnostic, Error)]
143#[error(transparent)]
144#[diagnostic(transparent)]
145pub struct ConcretizationError(pub(crate) cedar_policy_core::authorizer::ConcretizationError);
146
147#[derive(Debug, Diagnostic, Error)]
149pub enum ReauthorizationError {
150 #[error(transparent)]
152 #[diagnostic(transparent)]
153 Evaluation(#[from] EvaluationError),
154 #[error(transparent)]
156 #[diagnostic(transparent)]
157 PolicySet(#[from] PolicySetError),
158 #[error(transparent)]
160 #[diagnostic(transparent)]
161 Concretization(#[from] ConcretizationError),
162}
163
164#[doc(hidden)]
165impl From<cedar_policy_core::authorizer::ReauthorizationError> for ReauthorizationError {
166 fn from(e: cedar_policy_core::authorizer::ReauthorizationError) -> Self {
167 match e {
168 cedar_policy_core::authorizer::ReauthorizationError::PolicySetError(err) => {
169 Self::PolicySet(err.into())
170 }
171 cedar_policy_core::authorizer::ReauthorizationError::ConcretizationError(err) => {
172 Self::Concretization(ConcretizationError(err))
173 }
174 }
175 }
176}
177
178#[derive(Debug, Error, Diagnostic)]
180#[non_exhaustive]
181pub enum ToCedarSchemaError {
182 #[error(transparent)]
184 #[diagnostic(transparent)]
185 NameCollisions(#[from] to_cedar_syntax_errors::NameCollisionsError),
186}
187
188pub mod to_cedar_syntax_errors {
190 use miette::Diagnostic;
191 use thiserror::Error;
192
193 #[derive(Debug, Error, Diagnostic)]
195 #[error("{err}")]
196 pub struct NameCollisionsError {
197 #[diagnostic(transparent)]
198 pub(super) err: cedar_policy_core::validator::cedar_schema::fmt::NameCollisionsError,
199 pub(super) names_as_strings: Vec<String>,
201 }
202
203 impl NameCollisionsError {
204 pub fn names(&self) -> impl Iterator<Item = &str> {
206 self.names_as_strings
207 .iter()
208 .map(std::string::String::as_str)
209 }
210 }
211}
212
213#[doc(hidden)]
214impl From<cedar_policy_core::validator::cedar_schema::fmt::ToCedarSchemaSyntaxError>
215 for ToCedarSchemaError
216{
217 fn from(
218 value: cedar_policy_core::validator::cedar_schema::fmt::ToCedarSchemaSyntaxError,
219 ) -> Self {
220 match value {
221 cedar_policy_core::validator::cedar_schema::fmt::ToCedarSchemaSyntaxError::NameCollisions(
222 name_collision_err,
223 ) => NameCollisionsError {
224 names_as_strings: name_collision_err
225 .names()
226 .map(ToString::to_string)
227 .collect(),
228 err: name_collision_err,
229 }
230 .into(),
231 }
232 }
233}
234
235pub mod cedar_schema_errors {
237 use miette::Diagnostic;
238 use thiserror::Error;
239
240 pub use cedar_policy_core::validator::CedarSchemaParseError as ParseError;
241
242 #[derive(Debug, Error, Diagnostic)]
244 #[error(transparent)]
245 pub struct IoError(#[from] pub(super) std::io::Error);
246}
247
248#[derive(Debug, Diagnostic, Error)]
250#[non_exhaustive]
251pub enum CedarSchemaError {
252 #[error(transparent)]
254 #[diagnostic(transparent)]
255 Parse(#[from] cedar_schema_errors::ParseError),
256 #[error(transparent)]
258 #[diagnostic(transparent)]
259 Io(#[from] cedar_schema_errors::IoError),
260 #[error(transparent)]
262 #[diagnostic(transparent)]
263 Schema(#[from] SchemaError),
264}
265
266#[doc(hidden)]
267impl From<cedar_policy_core::validator::CedarSchemaError> for CedarSchemaError {
268 fn from(value: cedar_policy_core::validator::CedarSchemaError) -> Self {
269 match value {
270 cedar_policy_core::validator::CedarSchemaError::Schema(e) => e.into(),
271 cedar_policy_core::validator::CedarSchemaError::IO(e) => {
272 cedar_schema_errors::IoError(e).into()
273 }
274 cedar_policy_core::validator::CedarSchemaError::Parsing(e) => e.into(),
275 }
276 }
277}
278
279#[derive(Debug, Diagnostic, Error)]
281#[error("in {} `{attr_or_tag}` of `{uid}`: {err}", if *.was_attr { "attribute" } else { "tag" })]
282pub struct EntityAttrEvaluationError {
283 uid: EntityUid,
285 attr_or_tag: SmolStr,
287 was_attr: bool,
289 #[diagnostic(transparent)]
291 err: EvaluationError,
292}
293
294impl EntityAttrEvaluationError {
295 pub fn action(&self) -> &EntityUid {
297 &self.uid
298 }
299
300 pub fn attr(&self) -> &SmolStr {
304 &self.attr_or_tag
305 }
306
307 pub fn inner(&self) -> &EvaluationError {
309 &self.err
310 }
311}
312
313#[doc(hidden)]
314impl From<ast::EntityAttrEvaluationError> for EntityAttrEvaluationError {
315 fn from(err: ast::EntityAttrEvaluationError) -> Self {
316 Self {
317 uid: err.uid.into(),
318 attr_or_tag: err.attr_or_tag,
319 was_attr: err.was_attr,
320 err: err.err,
321 }
322 }
323}
324
325#[derive(Debug, Diagnostic, Error)]
327pub enum ContextCreationError {
328 #[error(transparent)]
330 #[diagnostic(transparent)]
331 NotARecord(context_creation_errors::NotARecord),
332 #[error(transparent)]
334 #[diagnostic(transparent)]
335 Evaluation(#[from] EvaluationError),
336 #[error(transparent)]
339 #[diagnostic(transparent)]
340 ExpressionConstruction(#[from] ExpressionConstructionError),
341}
342
343#[doc(hidden)]
344impl From<ast::ContextCreationError> for ContextCreationError {
345 fn from(e: ast::ContextCreationError) -> Self {
346 match e {
347 ast::ContextCreationError::NotARecord(nre) => Self::NotARecord(nre),
348 ast::ContextCreationError::Evaluation(e) => Self::Evaluation(e),
349 ast::ContextCreationError::ExpressionConstruction(ece) => {
350 Self::ExpressionConstruction(ece)
351 }
352 }
353 }
354}
355
356mod context_creation_errors {
358 pub use cedar_policy_core::ast::context_creation_errors::NotARecord;
359}
360
361pub mod validation_errors;
365
366#[derive(Debug, Clone, Error, Diagnostic)]
369#[non_exhaustive]
370pub enum ValidationError {
371 #[error(transparent)]
373 #[diagnostic(transparent)]
374 UnrecognizedEntityType(#[from] validation_errors::UnrecognizedEntityType),
375 #[error(transparent)]
377 #[diagnostic(transparent)]
378 UnrecognizedActionId(#[from] validation_errors::UnrecognizedActionId),
379 #[error(transparent)]
383 #[diagnostic(transparent)]
384 InvalidActionApplication(#[from] validation_errors::InvalidActionApplication),
385 #[error(transparent)]
388 #[diagnostic(transparent)]
389 UnexpectedType(#[from] validation_errors::UnexpectedType),
390 #[error(transparent)]
392 #[diagnostic(transparent)]
393 IncompatibleTypes(#[from] validation_errors::IncompatibleTypes),
394 #[error(transparent)]
397 #[diagnostic(transparent)]
398 UnsafeAttributeAccess(#[from] validation_errors::UnsafeAttributeAccess),
399 #[error(transparent)]
402 #[diagnostic(transparent)]
403 UnsafeOptionalAttributeAccess(#[from] validation_errors::UnsafeOptionalAttributeAccess),
404 #[error(transparent)]
406 #[diagnostic(transparent)]
407 UnsafeTagAccess(#[from] validation_errors::UnsafeTagAccess),
408 #[error(transparent)]
410 #[diagnostic(transparent)]
411 NoTagsAllowed(#[from] validation_errors::NoTagsAllowed),
412 #[error(transparent)]
414 #[diagnostic(transparent)]
415 UndefinedFunction(#[from] validation_errors::UndefinedFunction),
416 #[error(transparent)]
418 #[diagnostic(transparent)]
419 WrongNumberArguments(#[from] validation_errors::WrongNumberArguments),
420 #[diagnostic(transparent)]
422 #[error(transparent)]
423 FunctionArgumentValidation(#[from] validation_errors::FunctionArgumentValidation),
424 #[diagnostic(transparent)]
426 #[error(transparent)]
427 EmptySetForbidden(#[from] validation_errors::EmptySetForbidden),
428 #[diagnostic(transparent)]
430 #[error(transparent)]
431 NonLitExtConstructor(#[from] validation_errors::NonLitExtConstructor),
432 #[error(transparent)]
436 #[diagnostic(transparent)]
437 HierarchyNotRespected(#[from] validation_errors::HierarchyNotRespected),
438 #[error(transparent)]
441 #[diagnostic(transparent)]
442 InternalInvariantViolation(#[from] validation_errors::InternalInvariantViolation),
443 #[error(transparent)]
445 #[diagnostic(transparent)]
446 EntityDerefLevelViolation(#[from] validation_errors::EntityDerefLevelViolation),
447 #[error(transparent)]
449 #[diagnostic(transparent)]
450 InvalidEnumEntity(#[from] validation_errors::InvalidEnumEntity),
451}
452
453impl ValidationError {
454 pub fn policy_id(&self) -> &crate::PolicyId {
456 match self {
457 Self::UnrecognizedEntityType(e) => e.policy_id(),
458 Self::UnrecognizedActionId(e) => e.policy_id(),
459 Self::InvalidActionApplication(e) => e.policy_id(),
460 Self::UnexpectedType(e) => e.policy_id(),
461 Self::IncompatibleTypes(e) => e.policy_id(),
462 Self::UnsafeAttributeAccess(e) => e.policy_id(),
463 Self::UnsafeOptionalAttributeAccess(e) => e.policy_id(),
464 Self::UnsafeTagAccess(e) => e.policy_id(),
465 Self::NoTagsAllowed(e) => e.policy_id(),
466 Self::UndefinedFunction(e) => e.policy_id(),
467 Self::WrongNumberArguments(e) => e.policy_id(),
468 Self::FunctionArgumentValidation(e) => e.policy_id(),
469 Self::EmptySetForbidden(e) => e.policy_id(),
470 Self::NonLitExtConstructor(e) => e.policy_id(),
471 Self::HierarchyNotRespected(e) => e.policy_id(),
472 Self::InternalInvariantViolation(e) => e.policy_id(),
473 Self::EntityDerefLevelViolation(e) => e.policy_id(),
474 Self::InvalidEnumEntity(e) => e.policy_id(),
475 }
476 }
477}
478
479#[doc(hidden)]
480impl From<cedar_policy_core::validator::ValidationError> for ValidationError {
481 fn from(error: cedar_policy_core::validator::ValidationError) -> Self {
482 match error {
483 cedar_policy_core::validator::ValidationError::UnrecognizedEntityType(e) => {
484 Self::UnrecognizedEntityType(e.into())
485 }
486 cedar_policy_core::validator::ValidationError::UnrecognizedActionId(e) => {
487 Self::UnrecognizedActionId(e.into())
488 }
489 cedar_policy_core::validator::ValidationError::InvalidActionApplication(e) => {
490 Self::InvalidActionApplication(e.into())
491 }
492 cedar_policy_core::validator::ValidationError::UnexpectedType(e) => {
493 Self::UnexpectedType(e.into())
494 }
495 cedar_policy_core::validator::ValidationError::IncompatibleTypes(e) => {
496 Self::IncompatibleTypes(e.into())
497 }
498 cedar_policy_core::validator::ValidationError::UnsafeAttributeAccess(e) => {
499 Self::UnsafeAttributeAccess(e.into())
500 }
501 cedar_policy_core::validator::ValidationError::UnsafeOptionalAttributeAccess(e) => {
502 Self::UnsafeOptionalAttributeAccess(e.into())
503 }
504 cedar_policy_core::validator::ValidationError::UnsafeTagAccess(e) => {
505 Self::UnsafeTagAccess(e.into())
506 }
507 cedar_policy_core::validator::ValidationError::NoTagsAllowed(e) => {
508 Self::NoTagsAllowed(e.into())
509 }
510 cedar_policy_core::validator::ValidationError::UndefinedFunction(e) => {
511 Self::UndefinedFunction(e.into())
512 }
513 cedar_policy_core::validator::ValidationError::WrongNumberArguments(e) => {
514 Self::WrongNumberArguments(e.into())
515 }
516 cedar_policy_core::validator::ValidationError::FunctionArgumentValidation(e) => {
517 Self::FunctionArgumentValidation(e.into())
518 }
519 cedar_policy_core::validator::ValidationError::EmptySetForbidden(e) => {
520 Self::EmptySetForbidden(e.into())
521 }
522 cedar_policy_core::validator::ValidationError::NonLitExtConstructor(e) => {
523 Self::NonLitExtConstructor(e.into())
524 }
525 cedar_policy_core::validator::ValidationError::InternalInvariantViolation(e) => {
526 Self::InternalInvariantViolation(e.into())
527 }
528 cedar_policy_core::validator::ValidationError::InvalidEnumEntity(e) => {
529 Self::InvalidEnumEntity(e.into())
530 }
531 cedar_policy_core::validator::ValidationError::EntityDerefLevelViolation(e) => {
532 Self::EntityDerefLevelViolation(e.into())
533 }
534 }
535 }
536}
537
538pub mod validation_warnings;
542
543#[derive(Debug, Clone, Error, Diagnostic)]
549#[non_exhaustive]
550pub enum ValidationWarning {
551 #[diagnostic(transparent)]
555 #[error(transparent)]
556 MixedScriptString(#[from] validation_warnings::MixedScriptString),
557 #[diagnostic(transparent)]
559 #[error(transparent)]
560 BidiCharsInString(#[from] validation_warnings::BidiCharsInString),
561 #[diagnostic(transparent)]
563 #[error(transparent)]
564 BidiCharsInIdentifier(#[from] validation_warnings::BidiCharsInIdentifier),
565 #[diagnostic(transparent)]
569 #[error(transparent)]
570 MixedScriptIdentifier(#[from] validation_warnings::MixedScriptIdentifier),
571 #[diagnostic(transparent)]
576 #[error(transparent)]
577 ConfusableIdentifier(#[from] validation_warnings::ConfusableIdentifier),
578 #[diagnostic(transparent)]
580 #[error(transparent)]
581 ImpossiblePolicy(#[from] validation_warnings::ImpossiblePolicy),
582}
583
584impl ValidationWarning {
585 pub fn policy_id(&self) -> &PolicyId {
587 match self {
588 Self::MixedScriptString(w) => w.policy_id(),
589 Self::BidiCharsInString(w) => w.policy_id(),
590 Self::BidiCharsInIdentifier(w) => w.policy_id(),
591 Self::MixedScriptIdentifier(w) => w.policy_id(),
592 Self::ConfusableIdentifier(w) => w.policy_id(),
593 Self::ImpossiblePolicy(w) => w.policy_id(),
594 }
595 }
596}
597
598#[doc(hidden)]
599impl From<cedar_policy_core::validator::ValidationWarning> for ValidationWarning {
600 fn from(warning: cedar_policy_core::validator::ValidationWarning) -> Self {
601 match warning {
602 cedar_policy_core::validator::ValidationWarning::MixedScriptString(w) => {
603 Self::MixedScriptString(w.into())
604 }
605 cedar_policy_core::validator::ValidationWarning::BidiCharsInString(w) => {
606 Self::BidiCharsInString(w.into())
607 }
608 cedar_policy_core::validator::ValidationWarning::BidiCharsInIdentifier(w) => {
609 Self::BidiCharsInIdentifier(w.into())
610 }
611 cedar_policy_core::validator::ValidationWarning::MixedScriptIdentifier(w) => {
612 Self::MixedScriptIdentifier(w.into())
613 }
614 cedar_policy_core::validator::ValidationWarning::ConfusableIdentifier(w) => {
615 Self::ConfusableIdentifier(w.into())
616 }
617 cedar_policy_core::validator::ValidationWarning::ImpossiblePolicy(w) => {
618 Self::ImpossiblePolicy(w.into())
619 }
620 }
621 }
622}
623
624pub mod policy_set_errors {
626 use super::Error;
627 use crate::PolicyId;
628 use cedar_policy_core::ast;
629 use miette::Diagnostic;
630
631 #[derive(Debug, Diagnostic, Error)]
634 #[error("duplicate template or policy id `{id}`")]
635 pub struct AlreadyDefined {
636 pub(crate) id: PolicyId,
637 }
638
639 impl AlreadyDefined {
640 pub fn duplicate_id(&self) -> &PolicyId {
642 &self.id
643 }
644 }
645
646 #[derive(Debug, Diagnostic, Error)]
648 #[error("unable to link template")]
649 pub struct LinkingError {
650 #[from]
651 #[diagnostic(transparent)]
652 pub(crate) inner: ast::LinkingError,
653 }
654
655 #[derive(Debug, Diagnostic, Error)]
657 #[error("expected a static policy, but a template-linked policy was provided")]
658 pub struct ExpectedStatic {
659 _dummy: (),
664 }
665
666 impl ExpectedStatic {
667 pub(crate) fn new() -> Self {
668 Self { _dummy: () }
669 }
670 }
671
672 #[derive(Debug, Diagnostic, Error)]
674 #[error("expected a template, but a static policy was provided")]
675 pub struct ExpectedTemplate {
676 _dummy: (),
681 }
682
683 impl ExpectedTemplate {
684 pub(crate) fn new() -> Self {
685 Self { _dummy: () }
686 }
687 }
688
689 #[derive(Debug, Diagnostic, Error)]
691 #[error("unable to remove static policy `{policy_id}` because it does not exist")]
692 pub struct PolicyNonexistentError {
693 pub(crate) policy_id: PolicyId,
694 }
695
696 impl PolicyNonexistentError {
697 pub fn policy_id(&self) -> &PolicyId {
699 &self.policy_id
700 }
701 }
702
703 #[derive(Debug, Diagnostic, Error)]
705 #[error("unable to remove template `{template_id}` because it does not exist")]
706 pub struct TemplateNonexistentError {
707 pub(crate) template_id: PolicyId,
708 }
709
710 impl TemplateNonexistentError {
711 pub fn template_id(&self) -> &PolicyId {
713 &self.template_id
714 }
715 }
716
717 #[derive(Debug, Diagnostic, Error)]
719 #[error("unable to remove policy template `{template_id}` because it has active links")]
720 pub struct RemoveTemplateWithActiveLinksError {
721 pub(crate) template_id: PolicyId,
722 }
723
724 impl RemoveTemplateWithActiveLinksError {
725 pub fn template_id(&self) -> &PolicyId {
727 &self.template_id
728 }
729 }
730
731 #[derive(Debug, Diagnostic, Error)]
733 #[error("unable to remove policy template `{template_id}` because it is not a template")]
734 pub struct RemoveTemplateNotTemplateError {
735 pub(crate) template_id: PolicyId,
736 }
737
738 impl RemoveTemplateNotTemplateError {
739 pub fn template_id(&self) -> &PolicyId {
741 &self.template_id
742 }
743 }
744
745 #[derive(Debug, Diagnostic, Error)]
747 #[error("unable to unlink policy `{policy_id}` because it does not exist")]
748 pub struct LinkNonexistentError {
749 pub(crate) policy_id: PolicyId,
750 }
751
752 impl LinkNonexistentError {
753 pub fn policy_id(&self) -> &PolicyId {
755 &self.policy_id
756 }
757 }
758
759 #[derive(Debug, Diagnostic, Error)]
761 #[error("unable to unlink `{policy_id}` because it is not a link")]
762 pub struct UnlinkLinkNotLinkError {
763 pub(crate) policy_id: PolicyId,
764 }
765
766 impl UnlinkLinkNotLinkError {
767 pub fn policy_id(&self) -> &PolicyId {
769 &self.policy_id
770 }
771 }
772
773 #[derive(Debug, Diagnostic, Error)]
775 #[error("error serializing/deserializing policy set to/from JSON")]
776 pub struct JsonPolicySetError {
777 #[from]
778 pub(crate) inner: serde_json::Error,
779 }
780}
781
782#[derive(Debug, Diagnostic, Error)]
784#[non_exhaustive]
785pub enum PolicySetError {
786 #[error(transparent)]
789 #[diagnostic(transparent)]
790 AlreadyDefined(#[from] policy_set_errors::AlreadyDefined),
791 #[error(transparent)]
793 #[diagnostic(transparent)]
794 Linking(#[from] policy_set_errors::LinkingError),
795 #[error(transparent)]
797 #[diagnostic(transparent)]
798 ExpectedStatic(#[from] policy_set_errors::ExpectedStatic),
799 #[error(transparent)]
801 #[diagnostic(transparent)]
802 ExpectedTemplate(#[from] policy_set_errors::ExpectedTemplate),
803 #[error(transparent)]
805 #[diagnostic(transparent)]
806 PolicyNonexistent(#[from] policy_set_errors::PolicyNonexistentError),
807 #[error(transparent)]
809 #[diagnostic(transparent)]
810 TemplateNonexistent(#[from] policy_set_errors::TemplateNonexistentError),
811 #[error(transparent)]
813 #[diagnostic(transparent)]
814 RemoveTemplateWithActiveLinks(#[from] policy_set_errors::RemoveTemplateWithActiveLinksError),
815 #[error(transparent)]
817 #[diagnostic(transparent)]
818 RemoveTemplateNotTemplate(#[from] policy_set_errors::RemoveTemplateNotTemplateError),
819 #[error(transparent)]
821 #[diagnostic(transparent)]
822 LinkNonexistent(#[from] policy_set_errors::LinkNonexistentError),
823 #[error(transparent)]
825 #[diagnostic(transparent)]
826 UnlinkLinkNotLink(#[from] policy_set_errors::UnlinkLinkNotLinkError),
827 #[error(transparent)]
829 #[diagnostic(transparent)]
830 FromJson(#[from] PolicyFromJsonError),
831 #[error("Error serializing a policy/template to JSON")]
833 #[diagnostic(transparent)]
834 ToJson(#[from] PolicyToJsonError),
835 #[error(transparent)]
837 #[diagnostic(transparent)]
838 JsonPolicySet(#[from] policy_set_errors::JsonPolicySetError),
839}
840
841#[doc(hidden)]
842impl From<ast::PolicySetError> for PolicySetError {
843 fn from(e: ast::PolicySetError) -> Self {
844 match e {
845 ast::PolicySetError::Occupied { id } => {
846 Self::AlreadyDefined(policy_set_errors::AlreadyDefined {
847 id: PolicyId::new(id),
848 })
849 }
850 }
851 }
852}
853
854#[doc(hidden)]
855impl From<ast::LinkingError> for PolicySetError {
856 fn from(e: ast::LinkingError) -> Self {
857 Self::Linking(e.into())
858 }
859}
860
861#[doc(hidden)]
862impl From<ast::UnexpectedSlotError> for PolicySetError {
863 fn from(_: ast::UnexpectedSlotError) -> Self {
864 Self::ExpectedStatic(policy_set_errors::ExpectedStatic::new())
865 }
866}
867
868#[doc(hidden)]
869impl From<est::PolicySetFromJsonError> for PolicySetError {
870 fn from(e: est::PolicySetFromJsonError) -> Self {
871 match e {
872 est::PolicySetFromJsonError::PolicySet(e) => e.into(),
873 est::PolicySetFromJsonError::Linking(e) => e.into(),
874 est::PolicySetFromJsonError::FromJsonError(e) => Self::FromJson(e.into()),
875 }
876 }
877}
878
879#[derive(Debug, Diagnostic, Error)]
885#[error(transparent)]
886#[diagnostic(transparent)]
887pub struct ParseErrors(#[from] cedar_policy_core::parser::err::ParseErrors);
888
889impl ParseErrors {
890 pub fn iter(&self) -> impl Iterator<Item = &ParseError> {
893 self.0.iter().map(ParseError::ref_cast)
894 }
895}
896
897#[derive(Debug, Diagnostic, Error, RefCast)]
902#[repr(transparent)]
903#[error(transparent)]
904#[diagnostic(transparent)]
905#[non_exhaustive]
906pub struct ParseError {
907 #[from]
908 inner: cedar_policy_core::parser::err::ParseError,
909}
910
911#[derive(Debug, Diagnostic, Error)]
913pub enum PolicyToJsonError {
914 #[error(transparent)]
916 #[diagnostic(transparent)]
917 Parse(#[from] ParseErrors),
918 #[error(transparent)]
920 #[diagnostic(transparent)]
921 Link(#[from] policy_to_json_errors::JsonLinkError),
922 #[error(transparent)]
924 JsonSerialization(#[from] policy_to_json_errors::PolicyJsonSerializationError),
925}
926
927#[doc(hidden)]
928impl From<est::LinkingError> for PolicyToJsonError {
929 fn from(e: est::LinkingError) -> Self {
930 policy_to_json_errors::JsonLinkError::from(e).into()
931 }
932}
933
934impl From<serde_json::Error> for PolicyToJsonError {
935 fn from(e: serde_json::Error) -> Self {
936 policy_to_json_errors::PolicyJsonSerializationError::from(e).into()
937 }
938}
939
940pub mod policy_to_json_errors {
942 use cedar_policy_core::est;
943 use miette::Diagnostic;
944 use thiserror::Error;
945
946 #[derive(Debug, Diagnostic, Error)]
948 #[error(transparent)]
949 #[diagnostic(transparent)]
950 pub struct JsonLinkError {
951 #[from]
953 err: est::LinkingError,
954 }
955
956 #[derive(Debug, Diagnostic, Error)]
958 #[error(transparent)]
959 pub struct PolicyJsonSerializationError {
960 #[from]
962 err: serde_json::Error,
963 }
964}
965
966#[derive(Debug, Diagnostic, Error)]
968#[error("error deserializing a policy/template from JSON")]
969#[diagnostic(transparent)]
970pub struct PolicyFromJsonError {
971 #[from]
972 pub(crate) inner: cedar_policy_core::est::FromJsonError,
973}
974
975#[derive(Debug, Diagnostic, Error)]
977pub enum ContextJsonError {
978 #[error(transparent)]
980 #[diagnostic(transparent)]
981 JsonDeserialization(#[from] entities_json_errors::JsonDeserializationError),
982 #[error(transparent)]
984 #[diagnostic(transparent)]
985 ContextCreation(#[from] ContextCreationError),
986 #[error(transparent)]
988 #[diagnostic(transparent)]
989 MissingAction(#[from] context_json_errors::MissingActionError),
990}
991
992impl ContextJsonError {
993 pub(crate) fn missing_action(action: EntityUid) -> Self {
995 Self::MissingAction(context_json_errors::MissingActionError { action })
996 }
997}
998
999#[doc(hidden)]
1000impl From<cedar_policy_core::entities::json::ContextJsonDeserializationError> for ContextJsonError {
1001 fn from(e: cedar_policy_core::entities::json::ContextJsonDeserializationError) -> Self {
1002 match e {
1003 cedar_policy_core::entities::json::ContextJsonDeserializationError::JsonDeserialization(e) => Self::JsonDeserialization(e),
1004 cedar_policy_core::entities::json::ContextJsonDeserializationError::ContextCreation(e) => Self::ContextCreation(e.into())
1005 }
1006 }
1007}
1008
1009pub mod context_json_errors {
1011 use super::EntityUid;
1012 use miette::Diagnostic;
1013 use thiserror::Error;
1014
1015 #[derive(Debug, Diagnostic, Error)]
1017 #[error("action `{action}` does not exist in the supplied schema")]
1018 pub struct MissingActionError {
1019 pub(super) action: EntityUid,
1021 }
1022
1023 impl MissingActionError {
1024 pub fn action(&self) -> &EntityUid {
1026 &self.action
1027 }
1028 }
1029}
1030
1031#[derive(Debug, Diagnostic, Error)]
1033#[non_exhaustive]
1034pub enum RestrictedExpressionParseError {
1035 #[error(transparent)]
1037 #[diagnostic(transparent)]
1038 Parse(#[from] ParseErrors),
1039 #[error(transparent)]
1042 #[diagnostic(transparent)]
1043 InvalidRestrictedExpression(#[from] RestrictedExpressionError),
1044}
1045
1046#[doc(hidden)]
1047impl From<cedar_policy_core::ast::RestrictedExpressionParseError>
1048 for RestrictedExpressionParseError
1049{
1050 fn from(e: cedar_policy_core::ast::RestrictedExpressionParseError) -> Self {
1051 match e {
1052 cedar_policy_core::ast::RestrictedExpressionParseError::Parse(e) => {
1053 Self::Parse(e.into())
1054 }
1055 cedar_policy_core::ast::RestrictedExpressionParseError::InvalidRestrictedExpression(
1056 e,
1057 ) => e.into(),
1058 }
1059 }
1060}
1061
1062#[derive(Debug, Diagnostic, Error)]
1064#[non_exhaustive]
1065pub enum RequestValidationError {
1066 #[error(transparent)]
1068 #[diagnostic(transparent)]
1069 UndeclaredAction(#[from] request_validation_errors::UndeclaredActionError),
1070 #[error(transparent)]
1072 #[diagnostic(transparent)]
1073 UndeclaredPrincipalType(#[from] request_validation_errors::UndeclaredPrincipalTypeError),
1074 #[error(transparent)]
1076 #[diagnostic(transparent)]
1077 UndeclaredResourceType(#[from] request_validation_errors::UndeclaredResourceTypeError),
1078 #[error(transparent)]
1081 #[diagnostic(transparent)]
1082 InvalidPrincipalType(#[from] request_validation_errors::InvalidPrincipalTypeError),
1083 #[error(transparent)]
1086 #[diagnostic(transparent)]
1087 InvalidResourceType(#[from] request_validation_errors::InvalidResourceTypeError),
1088 #[error(transparent)]
1090 #[diagnostic(transparent)]
1091 InvalidContext(#[from] request_validation_errors::InvalidContextError),
1092 #[error(transparent)]
1094 #[diagnostic(transparent)]
1095 TypeOfContext(#[from] request_validation_errors::TypeOfContextError),
1096 #[error(transparent)]
1099 #[diagnostic(transparent)]
1100 InvalidEnumEntity(#[from] request_validation_errors::InvalidEnumEntityError),
1101}
1102
1103#[doc(hidden)]
1104impl From<cedar_policy_core::validator::RequestValidationError> for RequestValidationError {
1105 fn from(e: cedar_policy_core::validator::RequestValidationError) -> Self {
1106 match e {
1107 cedar_policy_core::validator::RequestValidationError::UndeclaredAction(e) => {
1108 Self::UndeclaredAction(e.into())
1109 }
1110 cedar_policy_core::validator::RequestValidationError::UndeclaredPrincipalType(e) => {
1111 Self::UndeclaredPrincipalType(e.into())
1112 }
1113 cedar_policy_core::validator::RequestValidationError::UndeclaredResourceType(e) => {
1114 Self::UndeclaredResourceType(e.into())
1115 }
1116 cedar_policy_core::validator::RequestValidationError::InvalidPrincipalType(e) => {
1117 Self::InvalidPrincipalType(e.into())
1118 }
1119 cedar_policy_core::validator::RequestValidationError::InvalidResourceType(e) => {
1120 Self::InvalidResourceType(e.into())
1121 }
1122 cedar_policy_core::validator::RequestValidationError::InvalidContext(e) => {
1123 Self::InvalidContext(e.into())
1124 }
1125 cedar_policy_core::validator::RequestValidationError::TypeOfContext(e) => {
1126 Self::TypeOfContext(e.into())
1127 }
1128 cedar_policy_core::validator::RequestValidationError::InvalidEnumEntity(e) => {
1129 Self::InvalidEnumEntity(e.into())
1130 }
1131 }
1132 }
1133}
1134
1135pub mod request_validation_errors {
1137 use cedar_policy_core::extensions::ExtensionFunctionLookupError;
1138 use miette::Diagnostic;
1139 use ref_cast::RefCast;
1140 use thiserror::Error;
1141
1142 use crate::{Context, EntityTypeName, EntityUid};
1143
1144 #[derive(Debug, Diagnostic, Error)]
1146 #[error(transparent)]
1147 #[diagnostic(transparent)]
1148 pub struct UndeclaredActionError(
1149 #[from] cedar_policy_core::validator::request_validation_errors::UndeclaredActionError,
1150 );
1151
1152 impl UndeclaredActionError {
1153 pub fn action(&self) -> &EntityUid {
1155 RefCast::ref_cast(self.0.action())
1156 }
1157 }
1158
1159 #[derive(Debug, Diagnostic, Error)]
1161 #[error(transparent)]
1162 #[diagnostic(transparent)]
1163 pub struct UndeclaredPrincipalTypeError(
1164 #[from]
1165 cedar_policy_core::validator::request_validation_errors::UndeclaredPrincipalTypeError,
1166 );
1167
1168 impl UndeclaredPrincipalTypeError {
1169 pub fn principal_ty(&self) -> &EntityTypeName {
1171 RefCast::ref_cast(self.0.principal_ty())
1172 }
1173 }
1174
1175 #[derive(Debug, Diagnostic, Error)]
1177 #[error(transparent)]
1178 #[diagnostic(transparent)]
1179 pub struct UndeclaredResourceTypeError(
1180 #[from]
1181 cedar_policy_core::validator::request_validation_errors::UndeclaredResourceTypeError,
1182 );
1183
1184 impl UndeclaredResourceTypeError {
1185 pub fn resource_ty(&self) -> &EntityTypeName {
1187 RefCast::ref_cast(self.0.resource_ty())
1188 }
1189 }
1190
1191 #[derive(Debug, Diagnostic, Error)]
1194 #[error(transparent)]
1195 #[diagnostic(transparent)]
1196 pub struct InvalidPrincipalTypeError(
1197 #[from] cedar_policy_core::validator::request_validation_errors::InvalidPrincipalTypeError,
1198 );
1199
1200 impl InvalidPrincipalTypeError {
1201 pub fn principal_ty(&self) -> &EntityTypeName {
1203 RefCast::ref_cast(self.0.principal_ty())
1204 }
1205
1206 pub fn action(&self) -> &EntityUid {
1208 RefCast::ref_cast(self.0.action())
1209 }
1210 }
1211
1212 #[derive(Debug, Diagnostic, Error)]
1215 #[error(transparent)]
1216 #[diagnostic(transparent)]
1217 pub struct InvalidResourceTypeError(
1218 #[from] cedar_policy_core::validator::request_validation_errors::InvalidResourceTypeError,
1219 );
1220
1221 impl InvalidResourceTypeError {
1222 pub fn resource_ty(&self) -> &EntityTypeName {
1224 RefCast::ref_cast(self.0.resource_ty())
1225 }
1226
1227 pub fn action(&self) -> &EntityUid {
1229 RefCast::ref_cast(self.0.action())
1230 }
1231 }
1232
1233 #[derive(Debug, Diagnostic, Error)]
1235 #[error(transparent)]
1236 #[diagnostic(transparent)]
1237 pub struct InvalidContextError(
1238 #[from] cedar_policy_core::validator::request_validation_errors::InvalidContextError,
1239 );
1240
1241 impl InvalidContextError {
1242 pub fn context(&self) -> &Context {
1244 RefCast::ref_cast(self.0.context())
1245 }
1246
1247 pub fn action(&self) -> &EntityUid {
1249 RefCast::ref_cast(self.0.action())
1250 }
1251 }
1252
1253 #[derive(Debug, Diagnostic, Error)]
1255 #[error(transparent)]
1256 #[diagnostic(transparent)]
1257 pub struct TypeOfContextError(#[from] ExtensionFunctionLookupError);
1258
1259 #[derive(Debug, Diagnostic, Error)]
1262 #[error(transparent)]
1263 #[diagnostic(transparent)]
1264 pub struct InvalidEnumEntityError(
1265 #[from] cedar_policy_core::entities::conformance::err::InvalidEnumEntityError,
1266 );
1267}
1268
1269#[derive(Debug, Error, Diagnostic)]
1271#[non_exhaustive]
1272#[cfg(feature = "entity-manifest")]
1273pub enum EntityManifestError {
1274 #[error(transparent)]
1276 #[diagnostic(transparent)]
1277 Validation(#[from] ValidationResult),
1278 #[error(transparent)]
1280 #[diagnostic(transparent)]
1281 Entities(#[from] EntitiesError),
1282
1283 #[error(transparent)]
1285 #[diagnostic(transparent)]
1286 PartialRequest(#[from] PartialRequestError),
1287 #[error(transparent)]
1289 #[diagnostic(transparent)]
1290 PartialExpression(#[from] PartialExpressionError),
1291 #[error(transparent)]
1293 #[diagnostic(transparent)]
1294 UnsupportedCedarFeature(#[from] UnsupportedCedarFeatureError),
1295}
1296
1297#[cfg(feature = "entity-manifest")]
1298impl From<entity_manifest::EntityManifestError> for EntityManifestError {
1299 fn from(e: entity_manifest::EntityManifestError) -> Self {
1300 match e {
1301 entity_manifest::EntityManifestError::Validation(e) => Self::Validation(e.into()),
1302 entity_manifest::EntityManifestError::Entities(e) => Self::Entities(e),
1303 entity_manifest::EntityManifestError::PartialRequest(e) => Self::PartialRequest(e),
1304 entity_manifest::EntityManifestError::PartialExpression(e) => {
1305 Self::PartialExpression(e)
1306 }
1307 entity_manifest::EntityManifestError::UnsupportedCedarFeature(e) => {
1308 Self::UnsupportedCedarFeature(e)
1309 }
1310 }
1311 }
1312}
1313
1314#[cfg(feature = "tpe")]
1315#[derive(Debug, Error, Diagnostic)]
1317pub enum PartialRequestCreationError {
1318 #[error("Context contains unknowns")]
1320 ContextContainsUnknowns,
1321 #[error(transparent)]
1323 #[diagnostic(transparent)]
1324 Validation(#[from] RequestValidationError),
1325}
1326
1327#[cfg(feature = "tpe")]
1328#[derive(Debug, Error)]
1330pub enum TPEReauthorizationError {
1331 #[error(transparent)]
1333 RequestValidation(#[from] RequestValidationError),
1334 #[error(transparent)]
1336 EntityValidation(#[from] EntitySchemaConformanceError),
1337 #[error(transparent)]
1339 InconsistentEntities(#[from] tpe_err::EntitiesConsistencyError),
1340 #[error(transparent)]
1342 InconsistentRequests(#[from] tpe_err::RequestConsistencyError),
1343}
1344
1345#[cfg(feature = "tpe")]
1346impl From<tpe_err::ReauthorizationError> for TPEReauthorizationError {
1347 fn from(value: tpe_err::ReauthorizationError) -> Self {
1348 match value {
1349 tpe_err::ReauthorizationError::EntitiesConsistentcy(e) => Self::InconsistentEntities(e),
1350 tpe_err::ReauthorizationError::EntityValidation(e) => Self::EntityValidation(e),
1351 tpe_err::ReauthorizationError::RequestConsistentcy(e) => Self::InconsistentRequests(e),
1352 tpe_err::ReauthorizationError::RequestValidation(e) => {
1353 Self::RequestValidation(e.into())
1354 }
1355 }
1356 }
1357}
1358
1359#[cfg(feature = "tpe")]
1360#[derive(Debug, Error)]
1362pub enum PermissionQueryError {
1363 #[error(transparent)]
1365 EntitiesContainUnknown(#[from] PartialValueToValueError),
1366 #[error(transparent)]
1368 TPE(#[from] tpe_err::TPEError),
1369 #[error(transparent)]
1371 EntityValidation(#[from] EntitySchemaConformanceError),
1372}