trusty_code/tui_client/error.rs
1//! [`EngineError`]: [`crate::tui_client::CodeEngine`]'s unified error type.
2//!
3//! Why: `CodeEngine`'s `TuiEngine` methods return `anyhow::Result` (the
4//! trait's signature, `crates/trusty-code-tui/src/engine.rs`), but the engine's
5//! own internal helpers need a concrete, matchable error type — both so
6//! tests can assert on WHICH failure occurred and so `?` composes cleanly
7//! across the client's submodules before the outer `TuiEngine` method converts
8//! the final error into `anyhow::Error` (automatic via `anyhow`'s blanket
9//! `From<E: std::error::Error>` impl).
10//! What: one variant per failure class this client can observe over the
11//! daemon's Unix socket (#6637): a transport-level failure, a JSON-RPC error
12//! envelope, a response whose shape this client did not expect, an event
13//! stream that ended without a terminal session event, and "no active session"
14//! (a caller-usage error, not a transport one — `handle_input` before `setup`
15//! completed).
16//!
17//! The HTTP-shaped variants are gone with the transport. `Discovery` went with
18//! `TCODE_DAEMON_URL` and the `http_addr` file — the socket path is derived,
19//! not discovered. `Status` went with HTTP status codes, which a framed
20//! JSON-RPC response does not have: every daemon-side refusal now arrives as
21//! [`EngineError::Rpc`] carrying a code, and every transport failure as
22//! [`EngineError::Transport`].
23//!
24//! Test: exercised indirectly through every `tui_client` submodule's own
25//! tests and `tests/tui_client_engine.rs`.
26
27use std::path::PathBuf;
28
29use serde_json::Value;
30use trusty_common::uds::UdsRpcError;
31
32/// See module docs.
33#[derive(Debug, thiserror::Error)]
34pub enum EngineError {
35 /// The socket could not be dialled, written to, or read from — including
36 /// the hardening checks `connect_hardened` runs before a byte is written.
37 ///
38 /// `Box`ed because [`UdsRpcError`] carries a `PathBuf` and a boxed
39 /// response of its own, and clippy's `result_large_err` reads the unboxed
40 /// enum as too large to return by value.
41 #[error("rpc over {} failed: {source}", socket.display())]
42 Transport {
43 /// Socket that failed.
44 socket: PathBuf,
45 /// Why it failed.
46 #[source]
47 source: Box<UdsRpcError>,
48 },
49
50 /// No daemon is answering on the socket, and this client will not start
51 /// one — the caller (`tcode tui`'s auto-spawn) decides that.
52 #[error("no tcode daemon is answering on {}", socket.display())]
53 NoDaemon {
54 /// Socket nothing answered on.
55 socket: PathBuf,
56 },
57
58 /// The daemon closed an event stream before this client observed a
59 /// terminal event (`SessionDone`/`SessionCancelled`) — and every reconnect
60 /// attempt (bounded by `SESSION_STREAM_MAX_RECONNECTS`) hit the same
61 /// premature close. Distinct from [`Self::Transport`]: nothing about the
62 /// individual dial failed, only the STREAM never reached a terminal state.
63 #[error("event stream on {} closed repeatedly with no terminal session event observed", socket.display())]
64 StreamClosed {
65 /// Socket whose stream kept ending early.
66 socket: PathBuf,
67 },
68
69 /// The daemon's response body didn't have the shape this client expected
70 /// (e.g. `session.create`'s result missing an `id` field) — a
71 /// daemon/client version-skew symptom, not a transport failure.
72 #[error("malformed response from daemon: {0}")]
73 Malformed(String),
74
75 /// The daemon's JSON-RPC error envelope (verbatim: code + message +
76 /// optional `data`).
77 #[error("daemon returned an error ({code}): {message}")]
78 Rpc {
79 /// JSON-RPC error code.
80 code: i32,
81 /// The daemon's own message.
82 message: String,
83 #[allow(dead_code)]
84 // surfaced for callers that want to inspect it; not read internally yet
85 /// The error's optional structured payload.
86 data: Option<Value>,
87 },
88
89 /// `handle_input`/`cancel_session` was called before `setup` minted a
90 /// session (or after a session was never successfully created) — a
91 /// caller-sequencing error, never a transport failure.
92 #[error("no active tcode session — `setup` must complete before sending input")]
93 NoSession,
94}