component_debug/cli/
format.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::lifecycle::{ActionError, CreateError, DestroyError, ResolveError, StartError};
6use anyhow::{format_err, Error};
7use cm_types::Name;
8use moniker::Moniker;
9
10static LIFECYCLE_ERROR_HELP: &'static str =
11    "To learn more, see https://fuchsia.dev/go/components/run-errors";
12static FIND_LIST_OR_SHOW: &'static str =
13    "Use the `list` or `show` subcommand to find the correct instance.";
14static TOOL_INCOMPATIBILITY: &'static str =
15    "This is most likely due to an incompatibility between this tool and the target.";
16static CHECK_TARGET_LOGS: &'static str =
17    "Check target logs for error details printed by component_manager.";
18
19/// Format an ActionError into an error message that is suitable for a CLI tool.
20pub fn format_action_error(moniker: &Moniker, err: ActionError) -> Error {
21    match err {
22        ActionError::InstanceNotFound => format_err!(
23            "\nError: The instance {} does not exist.\n{}\n{}\n",
24            moniker,
25            FIND_LIST_OR_SHOW,
26            LIFECYCLE_ERROR_HELP
27        ),
28        ActionError::InstanceNotResolved => format_err!(
29            "\nError: The instance {moniker} has not been resolved.\n{}\n{}\n",
30            CHECK_TARGET_LOGS,
31            LIFECYCLE_ERROR_HELP
32        ),
33        ActionError::BadMoniker => format_err!(
34            "\nError: Component manager cannot parse the moniker `{}`. {}\n",
35            moniker,
36            TOOL_INCOMPATIBILITY
37        ),
38        ActionError::Internal => format_err!(
39            "\nError: Component manager encountered an internal error.\n{}\n{}\n",
40            CHECK_TARGET_LOGS,
41            LIFECYCLE_ERROR_HELP
42        ),
43        ActionError::UnknownError => format_err!(
44            "\nError: Component manager returned an unknown error.\n{}\n{}\n{}\n",
45            TOOL_INCOMPATIBILITY,
46            CHECK_TARGET_LOGS,
47            LIFECYCLE_ERROR_HELP
48        ),
49        ActionError::Fidl(e) => format_err!(
50            "\nError: FIDL error communicating with LifecycleController ({:?}).\n{}\n{}\n",
51            e,
52            CHECK_TARGET_LOGS,
53            LIFECYCLE_ERROR_HELP
54        ),
55    }
56}
57
58/// Format a CreateError into an error message that is suitable for a CLI tool.
59pub fn format_create_error(
60    moniker: &Moniker,
61    parent: &Moniker,
62    collection: &Name,
63    err: CreateError,
64) -> Error {
65    match err {
66        CreateError::InstanceAlreadyExists => format_err!("\nError: {} already exists.\nUse the `show` subcommand to get information about the instance.\n{}\n", moniker, LIFECYCLE_ERROR_HELP),
67        CreateError::CollectionNotFound => format_err!("\nError: The parent {} does not have a collection `{}`.\nCheck the manifest of {} for a collection with this name.\n", parent, collection, parent),
68        CreateError::BadChildDecl => format_err!("\nError: Component manager cannot parse the child decl. {}\n", TOOL_INCOMPATIBILITY),
69        CreateError::ActionError(e) => format_action_error(parent, e)
70    }
71}
72
73/// Format a DestroyError into an error message that is suitable for a CLI tool.
74pub fn format_destroy_error(moniker: &Moniker, err: DestroyError) -> Error {
75    match err {
76        DestroyError::BadChildRef => format_err!(
77            "\nError: Component manager cannot parse the child reference. {}\n",
78            TOOL_INCOMPATIBILITY
79        ),
80        DestroyError::ActionError(e) => format_action_error(moniker, e),
81    }
82}
83
84/// Format a ResolveError into an error message that is suitable for a CLI tool.
85pub fn format_resolve_error(moniker: &Moniker, err: ResolveError) -> Error {
86    match err {
87        ResolveError::PackageNotFound => format_err!("\nError: The package associated with the instance {} could not be found.\nEnsure that your package server is running and the package is added to it.\n", moniker),
88        ResolveError::ManifestNotFound => format_err!("\nError: The manifest associated with the instance {} could not be found.\nEnsure that your package contains the manifest.\n", moniker),
89        ResolveError::ActionError(e) => format_action_error(moniker, e)
90    }
91}
92
93/// Format a StartError into an error message that is suitable for a CLI tool.
94pub fn format_start_error(moniker: &Moniker, err: StartError) -> Error {
95    match err {
96        StartError::PackageNotFound => format_err!("\nError: The package associated with the instance {} could not be found.\nEnsure that your package server is running and the package is added to it.\n", moniker),
97        StartError::ManifestNotFound => format_err!("\nError: The manifest associated with the instance {} could not be found.\nEnsure that your package contains the manifest.\n", moniker),
98        StartError::ActionError(e) => format_action_error(moniker, e)
99    }
100}