wami_core/error.rs
1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum AmiError {
5 /// A provider rejected an operation.
6 ///
7 /// Replaces the AwsSdk, StsSdk and SsoAdminSdk variants removed in 0.14.
8 /// Those named SDK error types in their signature, so every consumer
9 /// compiled three AWS SDKs to hold three variants this workspace never
10 /// constructed once. Carrying the provider name and its message keeps the
11 /// information without keeping the dependency:
12 ///
13 /// ```ignore
14 /// client.get_user().send().await.map_err(|e| AmiError::Provider {
15 /// provider: "aws".to_string(),
16 /// message: e.to_string(),
17 /// })?
18 /// ```
19 #[error("{provider} error: {message}")]
20 Provider {
21 /// Which provider refused.
22 provider: String,
23 /// What it said.
24 message: String,
25 },
26
27 #[error("Serialization error: {0}")]
28 Serialization(#[from] serde_json::Error),
29
30 #[error("Invalid parameter: {message}")]
31 InvalidParameter { message: String },
32
33 #[error("Operation not supported: {operation}")]
34 OperationNotSupported { operation: String },
35
36 #[error("Resource not found: {resource}")]
37 ResourceNotFound { resource: String },
38
39 #[error("Permission denied: {reason}")]
40 PermissionDenied { reason: String },
41
42 #[error("Access denied: {message}")]
43 AccessDenied { message: String },
44
45 #[error("Resource limit exceeded: {resource_type} limit is {limit}")]
46 ResourceLimitExceeded { resource_type: String, limit: usize },
47
48 #[error("Resource already exists: {resource}")]
49 ResourceExists { resource: String },
50
51 #[error("Store error: {0}")]
52 StoreError(String),
53
54 /// A policy document could not be parsed, so no decision can be made from it.
55 ///
56 /// Deliberately an error and not a denial. A denial means "the rules
57 /// forbid this"; an unreadable policy means "the rules cannot be read" —
58 /// and the caller must be able to tell those apart, because the first is a
59 /// 403 the user can act on and the second is a corrupt store that should
60 /// page someone. Folding it into `AccessDenied` would hide a broken policy
61 /// store behind what looks like an ordinary permission failure.
62 ///
63 /// When several documents cannot be read, one error names them all, in
64 /// the authorizer's canonical order: each field lists the documents'
65 /// values joined by `"; "`. The message names the statement (`Sid`, else
66 /// `#` and its index) and what the policy's author wrote — never a value.
67 #[error("policy cannot be read ({policy}): {message}")]
68 UnreadablePolicy {
69 /// Which policy failed to parse, so an operator knows what to fix.
70 policy: String,
71 /// What the parser objected to.
72 message: String,
73 },
74
75 /// A statement's condition could not be evaluated, so no decision can be
76 /// made from the policy that carries it.
77 ///
78 /// Deliberately an error and not a `false`. A false condition means "the
79 /// statement does not apply here"; an unreadable one means "whether it
80 /// applies cannot be known" — and answering that as `false` made the
81 /// statement vanish, so a `Deny` carrying it stopped denying. As with
82 /// [`AmiError::UnreadablePolicy`], it is not folded into `AccessDenied`
83 /// either: the policy is broken, and the operator must be told which one.
84 ///
85 /// What still reaches evaluation after the parse check is the request
86 /// side — a value the operator cannot compare, such as an `aws:SourceIp`
87 /// that is no address, or a custom key holding a number under
88 /// `StringEquals` — or a document that did not go through
89 /// `parse_policy_doc`. Every statement whose action and resource match is
90 /// read before any verdict, so a `Deny` that holds does not hide one.
91 ///
92 /// Several such statements are named in one error, in order, each field
93 /// listing the values joined by `"; "` (`policy` and `statement` pair up
94 /// by position). The message names kinds and positions, never the
95 /// request's values.
96 #[error("condition cannot be read ({policy}, statement {statement}): {message}")]
97 UnreadableCondition {
98 /// Which policy holds the statement.
99 policy: String,
100 /// The statement: its `Sid`, else `#` and its index in the document.
101 statement: String,
102 /// What the evaluation objected to.
103 message: String,
104 },
105}
106
107pub type Result<T> = std::result::Result<T, AmiError>;
108
109// Re-export helper functions and traits
110pub mod helpers;
111pub use helpers::OptionExt;
112
113// Re-export AmiError helper methods through the enum itself
114// The helper methods are implemented as extension methods on AmiError