1use crate::policy::PolicyError;
6use crate::rights::Rights;
7use async_trait::async_trait;
8use clonable_error::ClonableError;
9use cm_rust::{CapabilityTypeName, ExposeDeclCommon, OfferDeclCommon, SourceName, UseDeclCommon};
10use cm_types::{Availability, Name};
11use itertools::Itertools;
12use moniker::{ChildName, ExtendedMoniker, Moniker};
13use router_error::{DowncastErrorForTest, Explain, RouterError};
14use std::sync::Arc;
15use thiserror::Error;
16use {fidl_fuchsia_component as fcomponent, zx_status as zx};
17
18#[cfg(feature = "serde")]
19use serde::{Deserialize, Serialize};
20
21#[cfg_attr(feature = "serde", derive(Deserialize, Serialize), serde(rename_all = "snake_case"))]
23#[derive(Debug, Error, Clone)]
24pub enum ComponentInstanceError {
25 #[error("could not find `{moniker}`")]
26 InstanceNotFound { moniker: Moniker },
27 #[error("component is not executable `{moniker}`")]
28 InstanceNotExecutable { moniker: Moniker },
29 #[error("component manager instance unavailable")]
30 ComponentManagerInstanceUnavailable {},
31 #[error("expected a component instance, but got component manager's instance")]
32 ComponentManagerInstanceUnexpected {},
33 #[error("malformed url `{url}` for `{moniker}`")]
34 MalformedUrl { url: String, moniker: Moniker },
35 #[error("url `{url}` for `{moniker}` does not resolve to an absolute url")]
36 NoAbsoluteUrl { url: String, moniker: Moniker },
37 #[cfg_attr(feature = "serde", serde(skip))]
40 #[error("failed to resolve `{moniker}`:\n\t{err}")]
41 ResolveFailed {
42 moniker: Moniker,
43 #[source]
44 err: ClonableError,
45 },
46 #[cfg_attr(feature = "serde", serde(skip))]
49 #[error("failed to start `{moniker}`:\n\t{err_msg}")]
50 StartFailed {
51 moniker: Moniker,
52 err_msg: String,
56 err_as_zx: zx::Status,
57 },
58}
59
60impl ComponentInstanceError {
61 pub fn as_zx_status(&self) -> zx::Status {
62 match self {
63 ComponentInstanceError::ResolveFailed { .. }
64 | ComponentInstanceError::InstanceNotFound { .. }
65 | ComponentInstanceError::ComponentManagerInstanceUnavailable {}
66 | ComponentInstanceError::InstanceNotExecutable { .. }
67 | ComponentInstanceError::NoAbsoluteUrl { .. } => zx::Status::NOT_FOUND,
68 ComponentInstanceError::StartFailed { err_as_zx, .. } => *err_as_zx,
69 ComponentInstanceError::MalformedUrl { .. }
70 | ComponentInstanceError::ComponentManagerInstanceUnexpected { .. } => {
71 zx::Status::INTERNAL
72 }
73 }
74 }
75
76 pub fn instance_not_found(moniker: Moniker) -> ComponentInstanceError {
77 ComponentInstanceError::InstanceNotFound { moniker }
78 }
79
80 pub fn cm_instance_unavailable() -> ComponentInstanceError {
81 ComponentInstanceError::ComponentManagerInstanceUnavailable {}
82 }
83
84 pub fn resolve_failed(moniker: Moniker, err: impl Into<anyhow::Error>) -> Self {
85 Self::ResolveFailed { moniker, err: err.into().into() }
86 }
87}
88
89impl Explain for ComponentInstanceError {
90 fn as_zx_status(&self) -> zx::Status {
91 self.as_zx_status()
92 }
93}
94
95impl From<ComponentInstanceError> for ExtendedMoniker {
96 fn from(err: ComponentInstanceError) -> ExtendedMoniker {
97 match err {
98 ComponentInstanceError::InstanceNotFound { moniker }
99 | ComponentInstanceError::MalformedUrl { moniker, .. }
100 | ComponentInstanceError::NoAbsoluteUrl { moniker, .. }
101 | ComponentInstanceError::InstanceNotExecutable { moniker }
102 | ComponentInstanceError::ResolveFailed { moniker, .. }
103 | ComponentInstanceError::StartFailed { moniker, .. } => {
104 ExtendedMoniker::ComponentInstance(moniker)
105 }
106 ComponentInstanceError::ComponentManagerInstanceUnavailable {}
107 | ComponentInstanceError::ComponentManagerInstanceUnexpected {} => {
108 ExtendedMoniker::ComponentManager
109 }
110 }
111 }
112}
113
114impl PartialEq for ComponentInstanceError {
117 fn eq(&self, other: &Self) -> bool {
118 match (self, other) {
119 (
120 Self::InstanceNotFound { moniker: self_moniker },
121 Self::InstanceNotFound { moniker: other_moniker },
122 ) => self_moniker.eq(other_moniker),
123 (
124 Self::ComponentManagerInstanceUnavailable {},
125 Self::ComponentManagerInstanceUnavailable {},
126 ) => true,
127 (Self::ResolveFailed { .. }, Self::ResolveFailed { .. }) => false,
128 _ => false,
129 }
130 }
131}
132
133#[cfg_attr(feature = "serde", derive(Deserialize, Serialize), serde(rename_all = "snake_case"))]
135#[derive(Debug, Error, Clone, PartialEq)]
136pub enum RoutingError {
137 #[error(
138 "backing directory `{capability_id}` was not exposed to `{moniker}` from `#{child_moniker}`"
139 )]
140 StorageFromChildExposeNotFound {
141 child_moniker: ChildName,
142 moniker: Moniker,
143 capability_id: String,
144 },
145
146 #[error(
147 "`{target_name:?}` tried to use a storage capability from `{source_moniker}` but it is \
148 not in the component id index. https://fuchsia.dev/go/components/instance-id"
149 )]
150 ComponentNotInIdIndex { source_moniker: Moniker, target_name: Option<ChildName> },
151
152 #[error("`{capability_id}` is not a built-in capability")]
153 UseFromComponentManagerNotFound { capability_id: String },
154
155 #[error("`{capability_id}` is not a built-in capability")]
156 RegisterFromComponentManagerNotFound { capability_id: String },
157
158 #[error("`{capability_id}` is not a built-in capability")]
159 OfferFromComponentManagerNotFound { capability_id: String },
160
161 #[error("`{capability_id}` was not offered to `{moniker}` by parent")]
162 UseFromParentNotFound { moniker: Moniker, capability_id: String },
163
164 #[error("`{capability_id}` was not declared as a capability by `{moniker}`")]
165 UseFromSelfNotFound { moniker: Moniker, capability_id: String },
166
167 #[error("`{moniker}` does not have child `#{child_moniker}`")]
168 UseFromChildInstanceNotFound {
169 child_moniker: ChildName,
170 moniker: Moniker,
171 capability_id: String,
172 },
173
174 #[error(
175 "{capability_type} `{capability_name}` was not registered in environment of `{moniker}`"
176 )]
177 UseFromEnvironmentNotFound { moniker: Moniker, capability_type: String, capability_name: Name },
178
179 #[error(
180 "`{moniker}` tried to use {capability_type} `{capability_name}` from the root environment"
181 )]
182 UseFromRootEnvironmentNotAllowed {
183 moniker: Moniker,
184 capability_type: String,
185 capability_name: Name,
186 },
187
188 #[error("{capability_type} `{capability_name}` was not offered to `{moniker}` by parent")]
189 EnvironmentFromParentNotFound {
190 moniker: Moniker,
191 capability_type: String,
192 capability_name: Name,
193 },
194
195 #[error("`{capability_name}` was not exposed to `{moniker}` from `#{child_moniker}`")]
196 EnvironmentFromChildExposeNotFound {
197 child_moniker: ChildName,
198 moniker: Moniker,
199 capability_type: String,
200 capability_name: Name,
201 },
202
203 #[error("`{moniker}` does not have child `#{child_moniker}`")]
204 EnvironmentFromChildInstanceNotFound {
205 child_moniker: ChildName,
206 moniker: Moniker,
207 capability_name: Name,
208 capability_type: String,
209 },
210
211 #[error("`{capability_id}` was not offered to `{moniker}` by parent")]
212 OfferFromParentNotFound { moniker: Moniker, capability_id: String },
213
214 #[error(
215 "cannot offer `{capability_id}` because was not declared as a capability by `{moniker}`"
216 )]
217 OfferFromSelfNotFound { moniker: Moniker, capability_id: String },
218
219 #[error("`{capability_id}` was not offered to `{moniker}` by parent")]
220 StorageFromParentNotFound { moniker: Moniker, capability_id: String },
221
222 #[error("`{moniker}` does not have child `#{child_moniker}`")]
223 OfferFromChildInstanceNotFound {
224 child_moniker: ChildName,
225 moniker: Moniker,
226 capability_id: String,
227 },
228
229 #[error("`{moniker}` does not have collection `#{collection}`")]
230 OfferFromCollectionNotFound { collection: String, moniker: Moniker, capability: Name },
231
232 #[error("`{capability_id}` was not exposed to `{moniker}` from `#{child_moniker}`")]
233 OfferFromChildExposeNotFound {
234 child_moniker: ChildName,
235 moniker: Moniker,
236 capability_id: String,
237 },
238
239 #[error("`{capability_id}` is not a framework capability (at component `{moniker}`)")]
241 CapabilityFromFrameworkNotFound { moniker: Moniker, capability_id: String },
242
243 #[error(
244 "A capability was sourced to a base capability `{capability_id}` from `{moniker}`, but this is unsupported"
245 )]
246 CapabilityFromCapabilityNotFound { moniker: Moniker, capability_id: String },
247
248 #[error("`{capability_id}` is not a framework capability")]
250 CapabilityFromComponentManagerNotFound { capability_id: String },
251
252 #[error(
253 "unable to expose `{capability_id}` because it was not declared as a capability by `{moniker}`"
254 )]
255 ExposeFromSelfNotFound { moniker: Moniker, capability_id: String },
256
257 #[error("`{moniker}` does not have child `#{child_moniker}`")]
258 ExposeFromChildInstanceNotFound {
259 child_moniker: ChildName,
260 moniker: Moniker,
261 capability_id: String,
262 },
263
264 #[error("`{moniker}` does not have collection `#{collection}`")]
265 ExposeFromCollectionNotFound { collection: String, moniker: Moniker, capability: Name },
266
267 #[error("`{capability_id}` was not exposed to `{moniker}` from `#{child_moniker}`")]
268 ExposeFromChildExposeNotFound {
269 child_moniker: ChildName,
270 moniker: Moniker,
271 capability_id: String,
272 },
273
274 #[error(
275 "`{moniker}` tried to expose `{capability_id}` from the framework, but no such framework capability was found"
276 )]
277 ExposeFromFrameworkNotFound { moniker: Moniker, capability_id: String },
278
279 #[error("`{capability_id}` was not exposed to `{moniker}` from `#{child_moniker}`")]
280 UseFromChildExposeNotFound { child_moniker: ChildName, moniker: Moniker, capability_id: String },
281
282 #[error("`{capability_id}` was not exposed from `/`")]
283 UseFromRootExposeNotFound { capability_id: String },
284
285 #[error("routing a capability from an unsupported source type `{source_type}` at `{moniker}`")]
286 UnsupportedRouteSource { source_type: String, moniker: ExtendedMoniker },
287
288 #[error("routing a capability of an unsupported type `{type_name}` at `{moniker}`")]
289 UnsupportedCapabilityType { type_name: CapabilityTypeName, moniker: ExtendedMoniker },
290
291 #[error(
292 "dictionaries are not yet supported for {cap_type} capabilities at component `{moniker}`"
293 )]
294 DictionariesNotSupported { moniker: Moniker, cap_type: CapabilityTypeName },
295
296 #[error("dynamic dictionaries are not allowed at component `{moniker}`")]
297 DynamicDictionariesNotAllowed { moniker: Moniker },
298
299 #[error("the capability does not support member access at `{moniker}`")]
300 BedrockMemberAccessUnsupported { moniker: ExtendedMoniker },
301
302 #[error("item `{name}` is not present in dictionary at component `{moniker}`")]
303 BedrockNotPresentInDictionary { name: String, moniker: ExtendedMoniker },
304
305 #[error(
306 "routed capability was the wrong type at component `{moniker}`. Was: {actual}, expected: {expected}"
307 )]
308 BedrockWrongCapabilityType { actual: String, expected: String, moniker: ExtendedMoniker },
309
310 #[error(
311 "expected type {type_name} for routed capability at component `{moniker}`, but type was missing"
312 )]
313 BedrockMissingCapabilityType { type_name: String, moniker: ExtendedMoniker },
314
315 #[error("there was an error remoting a capability at component `{moniker}`")]
316 BedrockRemoteCapability { moniker: Moniker },
317
318 #[error("source dictionary was not found in child's exposes at component `{moniker}`")]
319 BedrockSourceDictionaryExposeNotFound { moniker: Moniker },
320
321 #[error("Some capability in the routing chain could not be cloned at `{moniker}`.")]
322 BedrockNotCloneable { moniker: ExtendedMoniker },
323
324 #[error(
325 "a capability in a dictionary extended from a source dictionary collides with \
326 a capability in the source dictionary that has the same key at `{moniker}`"
327 )]
328 BedrockSourceDictionaryCollision { moniker: ExtendedMoniker },
329
330 #[error("failed to send message for capability `{capability_id}` from component `{moniker}`")]
331 BedrockFailedToSend { moniker: ExtendedMoniker, capability_id: String },
332
333 #[error(
334 "failed to route capability because the route source has been shutdown and possibly destroyed"
335 )]
336 RouteSourceShutdown { moniker: Moniker },
337
338 #[error(transparent)]
339 ComponentInstanceError(#[from] ComponentInstanceError),
340
341 #[error(transparent)]
342 EventsRoutingError(#[from] EventsRoutingError),
343
344 #[error(transparent)]
345 RightsRoutingError(#[from] RightsRoutingError),
346
347 #[error(transparent)]
348 AvailabilityRoutingError(#[from] AvailabilityRoutingError),
349
350 #[error(transparent)]
351 PolicyError(#[from] PolicyError),
352
353 #[error(
354 "source capability at component {moniker} is void. \
355 If the offer/expose declaration has `source_availability` set to `unknown`, \
356 the source component instance likely isn't defined in the component declaration"
357 )]
358 SourceCapabilityIsVoid { moniker: Moniker },
359
360 #[error(
361 "routes that do not set the `debug` flag are unsupported in the current configuration (at `{moniker}`)."
362 )]
363 NonDebugRoutesUnsupported { moniker: ExtendedMoniker },
364
365 #[error("debug routes are unsupported for external routers (at `{moniker}`).")]
366 DebugRoutesUnsupported { moniker: ExtendedMoniker },
367
368 #[error("{type_name} router unexpectedly returned debug info for target {moniker}")]
369 RouteUnexpectedDebug { type_name: CapabilityTypeName, moniker: ExtendedMoniker },
370
371 #[error("{type_name} router unexpectedly returned unavailable for target {moniker}")]
372 RouteUnexpectedUnavailable { type_name: CapabilityTypeName, moniker: ExtendedMoniker },
373
374 #[error("{name} at {moniker} is missing porcelain type metadata.")]
375 MissingPorcelainType { name: Name, moniker: Moniker },
376
377 #[error("path at `{moniker}` was too long for `{keyword}`: {path}")]
378 PathTooLong { moniker: ExtendedMoniker, path: String, keyword: String },
379
380 #[error(
381 "conflicting dictionary entries detected component `{moniker}`: {}",
382 conflicting_names.iter().map(|n| format!("{}", n)).join(", ")
383 )]
384 ConflictingDictionaryEntries { moniker: ExtendedMoniker, conflicting_names: Vec<Name> },
385}
386
387impl Explain for RoutingError {
388 fn as_zx_status(&self) -> zx::Status {
390 match self {
391 RoutingError::UseFromRootEnvironmentNotAllowed { .. }
392 | RoutingError::DynamicDictionariesNotAllowed { .. } => zx::Status::ACCESS_DENIED,
393 RoutingError::StorageFromChildExposeNotFound { .. }
394 | RoutingError::ComponentNotInIdIndex { .. }
395 | RoutingError::UseFromComponentManagerNotFound { .. }
396 | RoutingError::RegisterFromComponentManagerNotFound { .. }
397 | RoutingError::OfferFromComponentManagerNotFound { .. }
398 | RoutingError::UseFromParentNotFound { .. }
399 | RoutingError::UseFromSelfNotFound { .. }
400 | RoutingError::UseFromChildInstanceNotFound { .. }
401 | RoutingError::UseFromEnvironmentNotFound { .. }
402 | RoutingError::EnvironmentFromParentNotFound { .. }
403 | RoutingError::EnvironmentFromChildExposeNotFound { .. }
404 | RoutingError::EnvironmentFromChildInstanceNotFound { .. }
405 | RoutingError::OfferFromParentNotFound { .. }
406 | RoutingError::OfferFromSelfNotFound { .. }
407 | RoutingError::StorageFromParentNotFound { .. }
408 | RoutingError::OfferFromChildInstanceNotFound { .. }
409 | RoutingError::OfferFromCollectionNotFound { .. }
410 | RoutingError::OfferFromChildExposeNotFound { .. }
411 | RoutingError::CapabilityFromFrameworkNotFound { .. }
412 | RoutingError::CapabilityFromCapabilityNotFound { .. }
413 | RoutingError::CapabilityFromComponentManagerNotFound { .. }
414 | RoutingError::ConflictingDictionaryEntries { .. }
415 | RoutingError::ExposeFromSelfNotFound { .. }
416 | RoutingError::ExposeFromChildInstanceNotFound { .. }
417 | RoutingError::ExposeFromCollectionNotFound { .. }
418 | RoutingError::ExposeFromChildExposeNotFound { .. }
419 | RoutingError::ExposeFromFrameworkNotFound { .. }
420 | RoutingError::UseFromChildExposeNotFound { .. }
421 | RoutingError::UseFromRootExposeNotFound { .. }
422 | RoutingError::UnsupportedRouteSource { .. }
423 | RoutingError::UnsupportedCapabilityType { .. }
424 | RoutingError::EventsRoutingError(_)
425 | RoutingError::BedrockNotPresentInDictionary { .. }
426 | RoutingError::BedrockSourceDictionaryExposeNotFound { .. }
427 | RoutingError::BedrockSourceDictionaryCollision { .. }
428 | RoutingError::BedrockFailedToSend { .. }
429 | RoutingError::RouteSourceShutdown { .. }
430 | RoutingError::BedrockMissingCapabilityType { .. }
431 | RoutingError::BedrockWrongCapabilityType { .. }
432 | RoutingError::BedrockRemoteCapability { .. }
433 | RoutingError::BedrockNotCloneable { .. }
434 | RoutingError::AvailabilityRoutingError(_)
435 | RoutingError::PathTooLong { .. } => zx::Status::NOT_FOUND,
436 RoutingError::BedrockMemberAccessUnsupported { .. }
437 | RoutingError::NonDebugRoutesUnsupported { .. }
438 | RoutingError::DebugRoutesUnsupported { .. }
439 | RoutingError::DictionariesNotSupported { .. } => zx::Status::NOT_SUPPORTED,
440 RoutingError::ComponentInstanceError(err) => err.as_zx_status(),
441 RoutingError::RightsRoutingError(err) => err.as_zx_status(),
442 RoutingError::PolicyError(err) => err.as_zx_status(),
443 RoutingError::SourceCapabilityIsVoid { .. } => zx::Status::NOT_FOUND,
444 RoutingError::RouteUnexpectedDebug { .. }
445 | RoutingError::RouteUnexpectedUnavailable { .. }
446 | RoutingError::MissingPorcelainType { .. } => zx::Status::INTERNAL,
447 }
448 }
449}
450
451impl From<RoutingError> for ExtendedMoniker {
452 fn from(err: RoutingError) -> ExtendedMoniker {
453 match err {
454 RoutingError::BedrockRemoteCapability { moniker, .. }
455 | RoutingError::BedrockSourceDictionaryExposeNotFound { moniker, .. }
456 | RoutingError::CapabilityFromCapabilityNotFound { moniker, .. }
457 | RoutingError::CapabilityFromFrameworkNotFound { moniker, .. }
458 | RoutingError::ComponentNotInIdIndex { source_moniker: moniker, .. }
459 | RoutingError::DictionariesNotSupported { moniker, .. }
460 | RoutingError::EnvironmentFromChildExposeNotFound { moniker, .. }
461 | RoutingError::EnvironmentFromChildInstanceNotFound { moniker, .. }
462 | RoutingError::EnvironmentFromParentNotFound { moniker, .. }
463 | RoutingError::ExposeFromChildExposeNotFound { moniker, .. }
464 | RoutingError::ExposeFromChildInstanceNotFound { moniker, .. }
465 | RoutingError::ExposeFromCollectionNotFound { moniker, .. }
466 | RoutingError::ExposeFromFrameworkNotFound { moniker, .. }
467 | RoutingError::ExposeFromSelfNotFound { moniker, .. }
468 | RoutingError::OfferFromChildExposeNotFound { moniker, .. }
469 | RoutingError::OfferFromChildInstanceNotFound { moniker, .. }
470 | RoutingError::OfferFromCollectionNotFound { moniker, .. }
471 | RoutingError::OfferFromParentNotFound { moniker, .. }
472 | RoutingError::OfferFromSelfNotFound { moniker, .. }
473 | RoutingError::SourceCapabilityIsVoid { moniker, .. }
474 | RoutingError::StorageFromChildExposeNotFound { moniker, .. }
475 | RoutingError::StorageFromParentNotFound { moniker, .. }
476 | RoutingError::UseFromChildExposeNotFound { moniker, .. }
477 | RoutingError::UseFromChildInstanceNotFound { moniker, .. }
478 | RoutingError::UseFromEnvironmentNotFound { moniker, .. }
479 | RoutingError::UseFromParentNotFound { moniker, .. }
480 | RoutingError::UseFromRootEnvironmentNotAllowed { moniker, .. }
481 | RoutingError::DynamicDictionariesNotAllowed { moniker, .. }
482 | RoutingError::RouteSourceShutdown { moniker }
483 | RoutingError::UseFromSelfNotFound { moniker, .. }
484 | RoutingError::MissingPorcelainType { moniker, .. } => moniker.into(),
485 RoutingError::PathTooLong { moniker, .. } => moniker,
486
487 RoutingError::BedrockMemberAccessUnsupported { moniker }
488 | RoutingError::BedrockNotPresentInDictionary { moniker, .. }
489 | RoutingError::BedrockNotCloneable { moniker }
490 | RoutingError::BedrockSourceDictionaryCollision { moniker }
491 | RoutingError::BedrockFailedToSend { moniker, .. }
492 | RoutingError::BedrockMissingCapabilityType { moniker, .. }
493 | RoutingError::BedrockWrongCapabilityType { moniker, .. }
494 | RoutingError::ConflictingDictionaryEntries { moniker, .. }
495 | RoutingError::NonDebugRoutesUnsupported { moniker }
496 | RoutingError::DebugRoutesUnsupported { moniker }
497 | RoutingError::RouteUnexpectedDebug { moniker, .. }
498 | RoutingError::RouteUnexpectedUnavailable { moniker, .. }
499 | RoutingError::UnsupportedCapabilityType { moniker, .. }
500 | RoutingError::UnsupportedRouteSource { moniker, .. } => moniker,
501 RoutingError::AvailabilityRoutingError(err) => err.into(),
502 RoutingError::ComponentInstanceError(err) => err.into(),
503 RoutingError::EventsRoutingError(err) => err.into(),
504 RoutingError::PolicyError(err) => err.into(),
505 RoutingError::RightsRoutingError(err) => err.into(),
506
507 RoutingError::CapabilityFromComponentManagerNotFound { .. }
508 | RoutingError::OfferFromComponentManagerNotFound { .. }
509 | RoutingError::RegisterFromComponentManagerNotFound { .. }
510 | RoutingError::UseFromComponentManagerNotFound { .. }
511 | RoutingError::UseFromRootExposeNotFound { .. } => ExtendedMoniker::ComponentManager,
512 }
513 }
514}
515
516impl From<RoutingError> for RouterError {
517 fn from(value: RoutingError) -> Self {
518 Self::NotFound(Arc::new(value))
519 }
520}
521
522impl From<RouterError> for RoutingError {
523 fn from(value: RouterError) -> Self {
524 match value {
525 RouterError::NotFound(arc_dyn_explain) => {
526 arc_dyn_explain.downcast_for_test::<Self>().clone()
527 }
528 err => panic!("Cannot downcast {err} to RoutingError!"),
529 }
530 }
531}
532
533impl RoutingError {
534 pub fn as_fidl_error(&self) -> fcomponent::Error {
536 fcomponent::Error::ResourceUnavailable
537 }
538
539 pub fn storage_from_child_expose_not_found(
540 child_moniker: &ChildName,
541 moniker: &Moniker,
542 capability_id: impl Into<String>,
543 ) -> Self {
544 Self::StorageFromChildExposeNotFound {
545 child_moniker: child_moniker.clone(),
546 moniker: moniker.clone(),
547 capability_id: capability_id.into(),
548 }
549 }
550
551 pub fn use_from_component_manager_not_found(capability_id: impl Into<String>) -> Self {
552 Self::UseFromComponentManagerNotFound { capability_id: capability_id.into() }
553 }
554
555 pub fn register_from_component_manager_not_found(capability_id: impl Into<String>) -> Self {
556 Self::RegisterFromComponentManagerNotFound { capability_id: capability_id.into() }
557 }
558
559 pub fn offer_from_component_manager_not_found(capability_id: impl Into<String>) -> Self {
560 Self::OfferFromComponentManagerNotFound { capability_id: capability_id.into() }
561 }
562
563 pub fn use_from_parent_not_found(moniker: &Moniker, capability_id: impl Into<String>) -> Self {
564 Self::UseFromParentNotFound {
565 moniker: moniker.clone(),
566 capability_id: capability_id.into(),
567 }
568 }
569
570 pub fn use_from_self_not_found(moniker: &Moniker, capability_id: impl Into<String>) -> Self {
571 Self::UseFromSelfNotFound { moniker: moniker.clone(), capability_id: capability_id.into() }
572 }
573
574 pub fn use_from_child_instance_not_found(
575 child_moniker: &ChildName,
576 moniker: &Moniker,
577 capability_id: impl Into<String>,
578 ) -> Self {
579 Self::UseFromChildInstanceNotFound {
580 child_moniker: child_moniker.clone(),
581 moniker: moniker.clone(),
582 capability_id: capability_id.into(),
583 }
584 }
585
586 pub fn use_from_environment_not_found(
587 moniker: &Moniker,
588 capability_type: impl Into<String>,
589 capability_name: &Name,
590 ) -> Self {
591 Self::UseFromEnvironmentNotFound {
592 moniker: moniker.clone(),
593 capability_type: capability_type.into(),
594 capability_name: capability_name.clone(),
595 }
596 }
597
598 pub fn offer_from_parent_not_found(
599 moniker: &Moniker,
600 capability_id: impl Into<String>,
601 ) -> Self {
602 Self::OfferFromParentNotFound {
603 moniker: moniker.clone(),
604 capability_id: capability_id.into(),
605 }
606 }
607
608 pub fn offer_from_self_not_found(moniker: &Moniker, capability_id: impl Into<String>) -> Self {
609 Self::OfferFromSelfNotFound {
610 moniker: moniker.clone(),
611 capability_id: capability_id.into(),
612 }
613 }
614
615 pub fn storage_from_parent_not_found(
616 moniker: &Moniker,
617 capability_id: impl Into<String>,
618 ) -> Self {
619 Self::StorageFromParentNotFound {
620 moniker: moniker.clone(),
621 capability_id: capability_id.into(),
622 }
623 }
624
625 pub fn offer_from_child_instance_not_found(
626 child_moniker: &ChildName,
627 moniker: &Moniker,
628 capability_id: impl Into<String>,
629 ) -> Self {
630 Self::OfferFromChildInstanceNotFound {
631 child_moniker: child_moniker.clone(),
632 moniker: moniker.clone(),
633 capability_id: capability_id.into(),
634 }
635 }
636
637 pub fn offer_from_child_expose_not_found(
638 child_moniker: &ChildName,
639 moniker: &Moniker,
640 capability_id: impl Into<String>,
641 ) -> Self {
642 Self::OfferFromChildExposeNotFound {
643 child_moniker: child_moniker.clone(),
644 moniker: moniker.clone(),
645 capability_id: capability_id.into(),
646 }
647 }
648
649 pub fn use_from_child_expose_not_found(
650 child_moniker: &ChildName,
651 moniker: &Moniker,
652 capability_id: impl Into<String>,
653 ) -> Self {
654 Self::UseFromChildExposeNotFound {
655 child_moniker: child_moniker.clone(),
656 moniker: moniker.clone(),
657 capability_id: capability_id.into(),
658 }
659 }
660
661 pub fn expose_from_self_not_found(moniker: &Moniker, capability_id: impl Into<String>) -> Self {
662 Self::ExposeFromSelfNotFound {
663 moniker: moniker.clone(),
664 capability_id: capability_id.into(),
665 }
666 }
667
668 pub fn expose_from_child_instance_not_found(
669 child_moniker: &ChildName,
670 moniker: &Moniker,
671 capability_id: impl Into<String>,
672 ) -> Self {
673 Self::ExposeFromChildInstanceNotFound {
674 child_moniker: child_moniker.clone(),
675 moniker: moniker.clone(),
676 capability_id: capability_id.into(),
677 }
678 }
679
680 pub fn expose_from_child_expose_not_found(
681 child_moniker: &ChildName,
682 moniker: &Moniker,
683 capability_id: impl Into<String>,
684 ) -> Self {
685 Self::ExposeFromChildExposeNotFound {
686 child_moniker: child_moniker.clone(),
687 moniker: moniker.clone(),
688 capability_id: capability_id.into(),
689 }
690 }
691
692 pub fn capability_from_framework_not_found(
693 moniker: &Moniker,
694 capability_id: impl Into<String>,
695 ) -> Self {
696 Self::CapabilityFromFrameworkNotFound {
697 moniker: moniker.clone(),
698 capability_id: capability_id.into(),
699 }
700 }
701
702 pub fn capability_from_capability_not_found(
703 moniker: &Moniker,
704 capability_id: impl Into<String>,
705 ) -> Self {
706 Self::CapabilityFromCapabilityNotFound {
707 moniker: moniker.clone(),
708 capability_id: capability_id.into(),
709 }
710 }
711
712 pub fn capability_from_component_manager_not_found(capability_id: impl Into<String>) -> Self {
713 Self::CapabilityFromComponentManagerNotFound { capability_id: capability_id.into() }
714 }
715
716 pub fn expose_from_framework_not_found(
717 moniker: &Moniker,
718 capability_id: impl Into<String>,
719 ) -> Self {
720 Self::ExposeFromFrameworkNotFound {
721 moniker: moniker.clone(),
722 capability_id: capability_id.into(),
723 }
724 }
725
726 pub fn unsupported_route_source(
727 moniker: impl Into<ExtendedMoniker>,
728 source: impl Into<String>,
729 ) -> Self {
730 Self::UnsupportedRouteSource { source_type: source.into(), moniker: moniker.into() }
731 }
732
733 pub fn unsupported_capability_type(
734 moniker: impl Into<ExtendedMoniker>,
735 type_name: impl Into<CapabilityTypeName>,
736 ) -> Self {
737 Self::UnsupportedCapabilityType { type_name: type_name.into(), moniker: moniker.into() }
738 }
739}
740
741#[cfg_attr(feature = "serde", derive(Deserialize, Serialize), serde(rename_all = "snake_case"))]
743#[derive(Error, Debug, Clone, PartialEq)]
744pub enum EventsRoutingError {
745 #[error("filter is not a subset at `{moniker}`")]
746 InvalidFilter { moniker: ExtendedMoniker },
747
748 #[error("event routes must end at source with a filter declaration at `{moniker}`")]
749 MissingFilter { moniker: ExtendedMoniker },
750}
751
752impl From<EventsRoutingError> for ExtendedMoniker {
753 fn from(err: EventsRoutingError) -> ExtendedMoniker {
754 match err {
755 EventsRoutingError::InvalidFilter { moniker }
756 | EventsRoutingError::MissingFilter { moniker } => moniker,
757 }
758 }
759}
760
761#[cfg_attr(feature = "serde", derive(Deserialize, Serialize), serde(rename_all = "snake_case"))]
762#[derive(Debug, Error, Clone, PartialEq)]
763pub enum RightsRoutingError {
764 #[error(
765 "requested rights ({requested}) greater than provided rights ({provided}) at \"{moniker}\""
766 )]
767 Invalid { moniker: ExtendedMoniker, requested: Rights, provided: Rights },
768
769 #[error(
770 "directory routes must end at source with a rights declaration, it's missing at \"{moniker}\""
771 )]
772 MissingRightsSource { moniker: ExtendedMoniker },
773}
774
775impl RightsRoutingError {
776 pub fn as_zx_status(&self) -> zx::Status {
778 match self {
779 RightsRoutingError::Invalid { .. } => zx::Status::ACCESS_DENIED,
780 RightsRoutingError::MissingRightsSource { .. } => zx::Status::NOT_FOUND,
781 }
782 }
783}
784
785impl From<RightsRoutingError> for ExtendedMoniker {
786 fn from(err: RightsRoutingError) -> ExtendedMoniker {
787 match err {
788 RightsRoutingError::Invalid { moniker, .. }
789 | RightsRoutingError::MissingRightsSource { moniker } => moniker,
790 }
791 }
792}
793
794#[cfg_attr(feature = "serde", derive(Deserialize, Serialize), serde(rename_all = "snake_case"))]
795#[derive(Debug, Error, Clone, PartialEq)]
796pub enum AvailabilityRoutingError {
797 #[error(
798 "availability requested by the target has stronger guarantees than what \
799 is being provided at the source at `{moniker}`"
800 )]
801 TargetHasStrongerAvailability { moniker: ExtendedMoniker },
802
803 #[error("offer uses void source, but target requires the capability at `{moniker}`")]
804 OfferFromVoidToRequiredTarget { moniker: ExtendedMoniker },
805
806 #[error("expose uses void source, but target requires the capability at `{moniker}`")]
807 ExposeFromVoidToRequiredTarget { moniker: ExtendedMoniker },
808}
809
810impl From<availability::TargetHasStrongerAvailability> for AvailabilityRoutingError {
811 fn from(value: availability::TargetHasStrongerAvailability) -> Self {
812 let availability::TargetHasStrongerAvailability { moniker } = value;
813 AvailabilityRoutingError::TargetHasStrongerAvailability { moniker }
814 }
815}
816
817impl From<AvailabilityRoutingError> for ExtendedMoniker {
818 fn from(err: AvailabilityRoutingError) -> ExtendedMoniker {
819 match err {
820 AvailabilityRoutingError::ExposeFromVoidToRequiredTarget { moniker }
821 | AvailabilityRoutingError::OfferFromVoidToRequiredTarget { moniker }
822 | AvailabilityRoutingError::TargetHasStrongerAvailability { moniker } => moniker,
823 }
824 }
825}
826
827#[async_trait]
830pub trait ErrorReporter: Clone + Send + Sync + 'static {
831 async fn report(
832 &self,
833 request: &RouteRequestErrorInfo,
834 err: &RouterError,
835 route_target: sandbox::WeakInstanceToken,
836 );
837}
838
839pub struct RouteRequestErrorInfo {
841 capability_type: cm_rust::CapabilityTypeName,
842 name: cm_types::Name,
843 availability: cm_rust::Availability,
844}
845
846impl RouteRequestErrorInfo {
847 pub fn availability(&self) -> cm_rust::Availability {
848 self.availability
849 }
850
851 pub fn for_builtin(capability_type: CapabilityTypeName, name: &Name) -> Self {
852 Self { capability_type, name: name.clone(), availability: Availability::Required }
853 }
854}
855
856impl From<&cm_rust::UseDecl> for RouteRequestErrorInfo {
857 fn from(value: &cm_rust::UseDecl) -> Self {
858 RouteRequestErrorInfo {
859 capability_type: value.into(),
860 name: value.source_name().clone(),
861 availability: value.availability().clone(),
862 }
863 }
864}
865
866impl From<&cm_rust::UseConfigurationDecl> for RouteRequestErrorInfo {
867 fn from(value: &cm_rust::UseConfigurationDecl) -> Self {
868 RouteRequestErrorInfo {
869 capability_type: CapabilityTypeName::Config,
870 name: value.source_name().clone(),
871 availability: value.availability().clone(),
872 }
873 }
874}
875
876impl From<&cm_rust::UseEventStreamDecl> for RouteRequestErrorInfo {
877 fn from(value: &cm_rust::UseEventStreamDecl) -> Self {
878 RouteRequestErrorInfo {
879 capability_type: CapabilityTypeName::EventStream,
880 name: value.source_name.clone(),
881 availability: value.availability,
882 }
883 }
884}
885
886impl From<&cm_rust::ExposeDecl> for RouteRequestErrorInfo {
887 fn from(value: &cm_rust::ExposeDecl) -> Self {
888 RouteRequestErrorInfo {
889 capability_type: value.into(),
890 name: value.target_name().clone(),
891 availability: value.availability().clone(),
892 }
893 }
894}
895
896impl From<&cm_rust::OfferDecl> for RouteRequestErrorInfo {
897 fn from(value: &cm_rust::OfferDecl) -> Self {
898 RouteRequestErrorInfo {
899 capability_type: value.into(),
900 name: value.target_name().clone(),
901 availability: value.availability().clone(),
902 }
903 }
904}
905
906impl From<&cm_rust::ResolverRegistration> for RouteRequestErrorInfo {
907 fn from(value: &cm_rust::ResolverRegistration) -> Self {
908 RouteRequestErrorInfo {
909 capability_type: CapabilityTypeName::Resolver,
910 name: value.source_name().clone(),
911 availability: Availability::Required,
912 }
913 }
914}
915
916impl From<&cm_rust::RunnerRegistration> for RouteRequestErrorInfo {
917 fn from(value: &cm_rust::RunnerRegistration) -> Self {
918 RouteRequestErrorInfo {
919 capability_type: CapabilityTypeName::Runner,
920 name: value.source_name().clone(),
921 availability: Availability::Required,
922 }
923 }
924}
925
926impl From<&cm_rust::DebugRegistration> for RouteRequestErrorInfo {
927 fn from(value: &cm_rust::DebugRegistration) -> Self {
928 RouteRequestErrorInfo {
929 capability_type: CapabilityTypeName::Protocol,
930 name: value.source_name().clone(),
931 availability: Availability::Required,
932 }
933 }
934}
935
936impl From<&cm_rust::CapabilityDecl> for RouteRequestErrorInfo {
937 fn from(value: &cm_rust::CapabilityDecl) -> Self {
938 RouteRequestErrorInfo {
939 capability_type: value.into(),
940 name: value.name().clone(),
941 availability: Availability::Required,
942 }
943 }
944}
945
946impl std::fmt::Display for RouteRequestErrorInfo {
947 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
948 write!(f, "{} `{}`", self.capability_type, self.name)
949 }
950}