1use crate::{InternalError, InternalErrorClass, InternalErrorOrigin, dto::error::Error};
2
3fn internal_error_to_public(err: &InternalError) -> Error {
4 if let Some(public) = err.public_error() {
5 return public.clone();
6 }
7
8 let message = err.to_string();
9
10 match err.class() {
11 InternalErrorClass::Access => Error::unauthorized(message),
12
13 InternalErrorClass::Domain => match err.origin() {
14 InternalErrorOrigin::Config => Error::invalid(message),
15 _ => Error::conflict(message),
16 },
17
18 InternalErrorClass::Invariant => Error::invariant(message),
19
20 InternalErrorClass::Infra | InternalErrorClass::Ops | InternalErrorClass::Workflow => {
21 Error::internal(message)
22 }
23 }
24}
25
26impl From<&InternalError> for Error {
27 fn from(err: &InternalError) -> Self {
28 internal_error_to_public(err)
29 }
30}
31
32impl From<InternalError> for Error {
33 fn from(err: InternalError) -> Self {
34 internal_error_to_public(&err)
35 }
36}
37
38#[cfg(test)]
43mod tests {
44 use super::*;
45 use crate::{
46 access::AccessError,
47 cdk::types::Principal,
48 domain::policy::pure::{
49 component_allocation::ComponentAllocationPolicyError,
50 component_child_allocation::ComponentChildAllocationPolicyError,
51 topology::{TopologyPolicyError, registry::RegistryPolicyError},
52 },
53 dto::error::ErrorCode,
54 ids::CanisterRole,
55 };
56
57 fn p(id: u8) -> Principal {
58 Principal::from_slice(&[id; 29])
59 }
60
61 #[test]
62 fn internal_error_mapping_matches_class_contract() {
63 let access: Error = InternalError::from(AccessError::Denied("denied".to_string())).into();
64 assert_eq!(access.code, ErrorCode::Unauthorized);
65
66 let domain_config: Error =
67 InternalError::domain(InternalErrorOrigin::Config, "bad config").into();
68 assert_eq!(domain_config.code, ErrorCode::InvalidInput);
69
70 let domain_other: Error =
71 InternalError::domain(InternalErrorOrigin::Domain, "conflict").into();
72 assert_eq!(domain_other.code, ErrorCode::Conflict);
73
74 let invariant: Error =
75 InternalError::invariant(InternalErrorOrigin::Ops, "broken invariant").into();
76 assert_eq!(invariant.code, ErrorCode::InvariantViolation);
77
78 let infra: Error = InternalError::infra(InternalErrorOrigin::Infra, "infra fail").into();
79 assert_eq!(infra.code, ErrorCode::Internal);
80
81 let ops: Error = InternalError::ops(InternalErrorOrigin::Ops, "ops fail").into();
82 assert_eq!(ops.code, ErrorCode::Internal);
83
84 let workflow: Error =
85 InternalError::workflow(InternalErrorOrigin::Workflow, "workflow fail").into();
86 assert_eq!(workflow.code, ErrorCode::Internal);
87
88 let invalid_allocation: Error =
89 InternalError::from(ComponentAllocationPolicyError::EmptyOperationId).into();
90 assert_eq!(invalid_allocation.code, ErrorCode::InvalidInput);
91
92 let exhausted_allocation: Error =
93 InternalError::from(ComponentAllocationPolicyError::ComponentCapacityExhausted).into();
94 assert_eq!(exhausted_allocation.code, ErrorCode::ResourceExhausted);
95
96 let invalid_authority: Error =
97 InternalError::from(ComponentAllocationPolicyError::RootTopologyDigestMismatch).into();
98 assert_eq!(invalid_authority.code, ErrorCode::InvariantViolation);
99
100 let forbidden_child: Error =
101 InternalError::from(ComponentChildAllocationPolicyError::SpawnGrantMissing {
102 parent_role: CanisterRole::new("project_hub"),
103 child_role: CanisterRole::new("project_ledger"),
104 })
105 .into();
106 assert_eq!(forbidden_child.code, ErrorCode::Forbidden);
107
108 let stale_child: Error = InternalError::from(
109 ComponentChildAllocationPolicyError::ComponentRegistryAuthorityMismatch,
110 )
111 .into();
112 assert_eq!(stale_child.code, ErrorCode::Conflict);
113
114 let exhausted_child: Error = InternalError::from(
115 ComponentChildAllocationPolicyError::ComponentDescendantCapacityExhausted,
116 )
117 .into();
118 assert_eq!(exhausted_child.code, ErrorCode::ResourceExhausted);
119
120 let token_expired: Error = AccessError::DelegatedAuthTokenExpired.into();
121 assert_eq!(token_expired.code, ErrorCode::AuthTokenExpired);
122
123 let cert_expired: Error = AccessError::DelegatedAuthCertExpired.into();
124 assert_eq!(cert_expired.code, ErrorCode::AuthProofExpired);
125 }
126
127 #[test]
128 fn public_error_is_preserved_without_remap() {
129 let public = Error::not_found("missing");
130 let remapped: Error = InternalError::public(public.clone()).into();
131 assert_eq!(remapped, public);
132 }
133
134 #[test]
135 fn registry_policy_errors_map_to_stable_public_policy_codes() {
136 let err = RegistryPolicyError::RoleAlreadyRegistered {
137 role: CanisterRole::new("app"),
138 pid: p(7),
139 };
140 let internal: InternalError = TopologyPolicyError::from(err).into();
141 let public: Error = internal.into();
142 assert_eq!(public.code, ErrorCode::PolicyRoleAlreadyRegistered);
143 }
144
145 #[test]
146 fn registry_policy_service_parent_errors_use_service_owned_public_codes() {
147 let cases = [
148 (
149 RegistryPolicyError::ReplicaRequiresServiceWithScaling {
150 role: CanisterRole::new("replica"),
151 parent_role: CanisterRole::new("plain_parent"),
152 },
153 ErrorCode::PolicyReplicaRequiresServiceWithScaling,
154 ),
155 (
156 RegistryPolicyError::ShardRequiresServiceWithSharding {
157 role: CanisterRole::new("shard"),
158 parent_role: CanisterRole::new("plain_parent"),
159 },
160 ErrorCode::PolicyShardRequiresServiceWithSharding,
161 ),
162 (
163 RegistryPolicyError::InstanceRequiresServiceWithIndex {
164 role: CanisterRole::new("instance"),
165 parent_role: CanisterRole::new("plain_parent"),
166 },
167 ErrorCode::PolicyInstanceRequiresServiceWithIndex,
168 ),
169 ];
170
171 for (err, expected) in cases {
172 let internal: InternalError = TopologyPolicyError::from(err).into();
173 let public: Error = internal.into();
174
175 assert_eq!(public.code, expected);
176 }
177 }
178}