Skip to main content

CommandError

Struct CommandError 

Source
pub struct CommandError {
    pub message: String,
    pub code: String,
    pub fix: String,
    pub next_actions: Vec<NextAction>,
}
Expand description

Error payload returned from a command handler.

Fields§

§message: String§code: String§fix: String§next_actions: Vec<NextAction>

Implementations§

Source§

impl CommandError

Source

pub fn new( message: impl Into<String>, code: impl Into<String>, fix: impl Into<String>, ) -> Self

Examples found in repository?
examples/ops.rs (lines 52-56)
7fn main() {
8    let cli = AgentCli::new("ops", "Agent-native operations CLI")
9        .version(env!("CARGO_PKG_VERSION"))
10        .root_field("health", json!({ "server": "ok", "worker": "ok" }))
11        .command(
12            Command::new("status", "Show system health")
13                .usage("ops status")
14                .handler(|_req, _ctx| {
15                    Ok(CommandOutput::new(json!({
16                        "healthy": true,
17                        "queue_depth": 0
18                    }))
19                    .next_action(NextAction::new("ops status", "Re-check status"))
20                    .next_action(
21                        NextAction::new("ops logs <source> [--lines <lines>]", "Inspect logs")
22                            .with_param(
23                                "source",
24                                ActionParam::new()
25                                    .enum_values(["worker", "errors", "server"])
26                                    .default("worker"),
27                            )
28                            .with_param(
29                                "lines",
30                                ActionParam::new()
31                                    .description("Number of lines to show")
32                                    .default(20),
33                            ),
34                    ))
35                }),
36        )
37        .command(
38            Command::new("logs", "View logs with context-safe truncation")
39                .usage("ops logs <source> [--lines <lines>] [--follow]")
40                .handler(|req, _ctx| {
41                    let source = req.arg(0).unwrap_or("worker");
42                    let lines = req
43                        .flag("lines")
44                        .and_then(|value| value.parse::<usize>().ok())
45                        .unwrap_or(20);
46
47                    let fake_logs = (0..120)
48                        .map(|idx| format!("[{source}] line-{idx}"))
49                        .collect::<Vec<_>>();
50                    let payload =
51                        truncate_lines_with_file(fake_logs, lines, "ops-logs").map_err(|_| {
52                            CommandError::new(
53                                "failed to write full log output",
54                                "LOG_WRITE_FAILED",
55                                "Check disk permissions and retry.",
56                            )
57                        })?;
58
59                    Ok(
60                        CommandOutput::new(json!(payload)).next_action(NextAction::new(
61                            "ops logs <source> [--lines <lines>] [--follow]",
62                            "Adjust line count or follow logs",
63                        )),
64                    )
65                }),
66        );
67
68    let run = cli.run_env();
69    println!("{}", run.to_json());
70    std::process::exit(run.exit_code());
71}
Source

pub fn next_action(self, action: NextAction) -> Self

Source

pub fn next_actions(self, actions: Vec<NextAction>) -> Self

Trait Implementations§

Source§

impl Clone for CommandError

Source§

fn clone(&self) -> CommandError

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CommandError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for CommandError

Source§

fn eq(&self, other: &CommandError) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for CommandError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.