1use std::fmt::Display;
21
22use smol_str::SmolStr;
23use thiserror::Error;
24
25use crate::{
26 ast::{Eid, EntityType, EntityUID, PartialValueToValueError},
27 entities::{
28 conformance::err::{EntitySchemaConformanceError, InvalidEnumEntityError},
29 err::Duplicate,
30 },
31 evaluator::{evaluation_errors::UnlinkedSlotError, EvaluationError},
32 transitive_closure::TcError,
33 validator::{RequestValidationError, ValidationError},
34};
35
36#[derive(Debug, Error)]
38#[error("Unexpected action: `{}`", .action)]
39pub struct UnexpectedActionError {
40 pub(super) action: EntityUID,
41}
42
43#[derive(Debug, Error)]
45pub enum JsonDeserializationError {
46 #[error(transparent)]
48 Concrete(#[from] crate::entities::json::err::JsonDeserializationError),
49 #[error(transparent)]
52 UnexpectedAction(#[from] UnexpectedActionError),
53 #[error(transparent)]
55 RestrictedExprEvaluation(#[from] EvaluationError),
56}
57
58#[derive(Debug, Error)]
60pub enum EntityValidationError {
61 #[error(transparent)]
63 Concrete(#[from] EntitySchemaConformanceError),
64 #[error(transparent)]
66 UnknownActionComponent(#[from] UnknownActionComponentError),
67 #[error(transparent)]
69 MismatchedActionAncestors(#[from] MismatchedActionAncestorsError),
70}
71
72#[derive(Debug, Error)]
74#[error("action `{}` has unknown ancestors/attrs/tags", .action)]
75pub struct UnknownActionComponentError {
76 pub(super) action: EntityUID,
77}
78
79#[derive(Debug, Error)]
81#[error("action `{}`'s ancestors do not match the schema", .action)]
82pub struct MismatchedActionAncestorsError {
83 pub(super) action: EntityUID,
84}
85
86#[derive(Debug, Error)]
88#[error("`{}`'s ancestor `{}` has unknown ancestors", .uid, .ancestor)]
89pub struct AncestorValidationError {
90 pub(crate) uid: EntityUID,
91 pub(crate) ancestor: EntityUID,
92}
93
94#[derive(Debug, Error)]
96pub enum TpeError {
97 #[error(transparent)]
100 NoMatchingReqEnv(#[from] NoMatchingReqEnvError),
101 #[error(transparent)]
103 NonstaticPolicy(#[from] NonstaticPolicyError),
104 #[error("Failed validation: {:#?}", .0)]
106 Validation(Vec<ValidationError>),
107 #[error(transparent)]
109 ExprToResidualError(#[from] ExprToResidualError),
110}
111
112#[derive(Debug, Error)]
115#[non_exhaustive]
116pub enum ExprToResidualError {
117 #[error(transparent)]
119 MissingTypeAnnotation(#[from] MissingTypeAnnotationError),
120 #[error(transparent)]
122 UnlinkedSlotError(#[from] UnlinkedSlotError),
123 #[error(transparent)]
125 UnknownNotSupported(#[from] UnknownNotSupportedError),
126 #[error(transparent)]
128 ErrorNotSupported(#[from] ErrorNotSupportedError),
129}
130
131#[derive(Debug, Error)]
133#[error("Expression is missing type annotation")]
134pub struct MissingTypeAnnotationError;
135
136#[derive(Debug, Error)]
138#[error("Expression contains an unknown which is not supported in residuals")]
139pub struct UnknownNotSupportedError;
140
141#[derive(Debug, Error)]
143#[error("Expression contains an error which is not supported in residuals")]
144pub struct ErrorNotSupportedError;
145
146#[derive(Debug, Error)]
148#[error("Found a partial request when a concrete request was expected")]
149pub struct PartialRequestError {}
150
151#[derive(Debug, Error)]
154#[error("Can't find a matching request environment")]
155pub struct NoMatchingReqEnvError;
156
157#[derive(Debug, Error)]
159#[error("Found a non-static policy")]
160pub struct NonstaticPolicyError;
161
162#[derive(Debug, Error)]
164pub enum RequestBuilderError {
165 #[error(transparent)]
167 Validation(#[from] RequestValidationError),
168 #[error(transparent)]
170 ExistingPrincipal(#[from] ExistingPrincipalError),
171 #[error(transparent)]
173 ExistingResource(#[from] ExistingResourceError),
174 #[error("Context already exists")]
176 ExistingContext,
177 #[error(transparent)]
180 IncorrectPrincipalEntityType(#[from] IncorrectPrincipalEntityTypeError),
181 #[error(transparent)]
184 IncorrectResourceEntityType(#[from] IncorrectResourceEntityTypeError),
185 #[error("invalid principal candidate: {}", .0)]
187 InvalidPrincipalCandidate(InvalidEnumEntityError),
188 #[error("invalid resource candidate: {}", .0)]
190 InvalidResourceCandidate(InvalidEnumEntityError),
191 #[error("context candidate doesn't validate: {}", .0)]
193 IllTypedContextCandidate(RequestValidationError),
194 #[error("context candidate contains unknowns")]
196 UnknownContextCandidate,
197}
198
199#[derive(Debug, Error)]
202#[error("Principal `{}` already exists", .principal)]
203pub struct ExistingPrincipalError {
204 pub(super) principal: EntityUID,
205}
206
207#[derive(Debug, Error)]
210#[error("Resource `{}` already exists", .resource)]
211pub struct ExistingResourceError {
212 pub(super) resource: EntityUID,
213}
214
215#[derive(Debug, Error)]
218#[error("Principal type `{}` is inconsistent with the partial request's `{}`", .ty, .expected)]
219pub struct IncorrectPrincipalEntityTypeError {
220 pub(super) ty: EntityType,
221 pub(super) expected: EntityType,
222}
223
224#[derive(Debug, Error)]
227#[error("Resource type `{}` is inconsistent with the partial request's `{}`", .ty, .expected)]
228pub struct IncorrectResourceEntityTypeError {
229 pub(super) ty: EntityType,
230 pub(super) expected: EntityType,
231}
232
233#[derive(Debug, Error)]
235pub enum EntitiesError {
236 #[error(transparent)]
238 Deserialization(#[from] JsonDeserializationError),
239 #[error(transparent)]
241 Validation(#[from] EntityValidationError),
242 #[error(transparent)]
244 AncestorValidation(#[from] AncestorValidationError),
245 #[error(transparent)]
247 TCComputation(#[from] TcError<EntityUID>),
248 #[error(transparent)]
251 Duplicate(#[from] Duplicate),
252 #[error(transparent)]
254 PartialValueToValue(#[from] PartialValueToValueError),
255}
256
257#[derive(Debug, Error)]
260pub enum EntitiesConsistencyError {
261 #[error(transparent)]
263 MissingEntity(#[from] MissingEntityError),
264 #[error(transparent)]
266 UnknownEntity(#[from] UnknownEntityError),
267 #[error(transparent)]
270 InconsistentEntity(#[from] EntityConsistencyError),
271}
272
273#[derive(Debug, Error)]
276pub enum EntityConsistencyError {
277 #[error(transparent)]
279 UnknownAttribute(#[from] UnknownAttributeError),
280 #[error(transparent)]
282 MismatchedAttribute(#[from] MismatchedAttributeError),
283 #[error(transparent)]
285 MismatchedAncestor(#[from] MismatchedAncestorError),
286 #[error(transparent)]
288 UnknownTag(#[from] UnknownTagError),
289 #[error(transparent)]
291 MismatchedTag(#[from] MismatchedTagError),
292}
293
294#[derive(Debug, Error)]
296#[error("Concrete entity `{uid}` contains unknown attribute `{attr}`")]
297pub struct UnknownAttributeError {
298 pub(super) uid: EntityUID,
299 pub(super) attr: SmolStr,
300}
301
302#[derive(Debug, Error)]
304#[error("Entity `{uid}`'s attributes do not match")]
305pub struct MismatchedAttributeError {
306 pub(super) uid: EntityUID,
307}
308
309#[derive(Debug, Error)]
311#[error("Concrete entity `{uid}` contains unknown tag `{tag}`")]
312pub struct UnknownTagError {
313 pub(super) uid: EntityUID,
314 pub(super) tag: SmolStr,
315}
316
317#[derive(Debug, Error)]
319#[error("Entity `{uid}`'s tags do not match")]
320pub struct MismatchedTagError {
321 pub(super) uid: EntityUID,
322}
323
324#[derive(Debug, Error)]
326#[error("Entity `{uid}`'s ancestors do not match")]
327pub struct MismatchedAncestorError {
328 pub(super) uid: EntityUID,
329}
330
331#[derive(Debug, Error)]
333#[error("Concrete entities does not include `{uid}`")]
334pub struct MissingEntityError {
335 pub(super) uid: EntityUID,
336}
337
338#[derive(Debug, Error)]
340#[error("Concrete entities contains unknown entity `{uid}`")]
341pub struct UnknownEntityError {
342 pub(super) uid: EntityUID,
343}
344
345#[derive(Debug, Error)]
347pub struct MissingEntitiesError {
348 pub(super) missing_entities: Vec<EntityUID>,
349}
350
351impl MissingEntitiesError {
352 pub fn new(missing_entities: Vec<EntityUID>) -> Self {
354 Self { missing_entities }
355 }
356}
357
358impl Display for MissingEntitiesError {
359 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
360 write!(
361 f,
362 "Failed to load entities: {}",
363 self.missing_entities
364 .iter()
365 .map(|uid| uid.to_string())
366 .collect::<Vec<_>>()
367 .join(", ")
368 )
369 }
370}
371
372#[derive(Debug, Error)]
374pub enum RequestConsistencyError {
375 #[error("Concrete principal is unknown")]
377 UnknownPrincipal,
378 #[error("Concrete resource is unknown")]
380 UnknownResource,
381 #[error("Concrete action is unknown")]
383 UnknownAction,
384 #[error("Concrete context is unknown")]
386 UnknownContext,
387 #[error(transparent)]
389 InconsistentPrincipalType(#[from] InconsistentPrincipalTypeError),
390 #[error(transparent)]
392 InconsistentPrincipalEid(#[from] InconsistentPrincipalEidError),
393 #[error(transparent)]
395 InconsistentResourceType(#[from] InconsistentResourceTypeError),
396 #[error(transparent)]
398 InconsistentResourceEid(#[from] InconsistentResourceEidError),
399 #[error(transparent)]
401 InconsistentAction(#[from] InconsistentActionError),
402 #[error("Contexts are inconsistent")]
404 InconsistentContext,
405 #[error("Concrete context contains unknowns")]
407 ConcreteContextContainsUnknowns,
408}
409
410#[derive(Debug, Error)]
412#[error("Principal types `{partial}` and `{concrete}` do not match")]
413pub struct InconsistentPrincipalTypeError {
414 pub(super) partial: EntityType,
415 pub(super) concrete: EntityType,
416}
417
418#[derive(Debug, Error)]
420#[error("Principal eid `{}` and `{}` do not match", .partial.escaped(), .concrete.escaped())]
421pub struct InconsistentPrincipalEidError {
422 pub(super) partial: Eid,
423 pub(super) concrete: Eid,
424}
425
426#[derive(Debug, Error)]
428#[error("Resource types `{partial}` and `{concrete}` do not match")]
429pub struct InconsistentResourceTypeError {
430 pub(super) partial: EntityType,
431 pub(super) concrete: EntityType,
432}
433
434#[derive(Debug, Error)]
436#[error("Resource eid `{}` and `{}` do not match", .partial.escaped(), .concrete.escaped())]
437pub struct InconsistentResourceEidError {
438 pub(super) partial: Eid,
439 pub(super) concrete: Eid,
440}
441
442#[derive(Debug, Error)]
444#[error("Actions `{}` and `{}` do not match", .partial, .concrete)]
445pub struct InconsistentActionError {
446 pub(super) partial: EntityUID,
447 pub(super) concrete: EntityUID,
448}
449
450#[derive(Debug, Error)]
452pub enum ReauthorizationError {
453 #[error(transparent)]
455 RequestValidation(#[from] RequestValidationError),
456 #[error(transparent)]
458 EntityValidation(#[from] EntitySchemaConformanceError),
459 #[error(transparent)]
461 EntitiesConsistentcy(#[from] EntitiesConsistencyError),
462 #[error(transparent)]
464 RequestConsistentcy(#[from] RequestConsistencyError),
465}