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 (discovery vs. transport vs.
7//! an RPC error envelope) and so `?` composes cleanly across the
8//! discovery/rpc/sse submodules before the outer `TuiEngine` method converts
9//! the final error into `anyhow::Error` (automatic via `anyhow`'s blanket
10//! `From<E: std::error::Error>` impl).
11//! What: one variant per failure class this client can observe: daemon
12//! discovery failure, an HTTP-transport-level failure (DNS/connect/decode),
13//! a non-2xx HTTP status without a JSON-RPC error envelope (e.g. `502`), a
14//! JSON-RPC error envelope from the daemon, and "no active session" (a
15//! caller-usage error, not a transport one — `handle_input` before `setup`
16//! completed).
17//! Test: exercised indirectly through every `tui_client` submodule's own
18//! tests and `tests/tui_client_engine.rs`.
19
20use serde_json::Value;
21
22use super::discovery::DiscoveryError;
23
24/// See module docs.
25#[derive(Debug, thiserror::Error)]
26pub enum EngineError {
27 /// No live daemon could be found (see [`DiscoveryError`] for the
28 /// specific reason — no candidate source, or a candidate that didn't
29 /// answer the liveness ping).
30 #[error(transparent)]
31 Discovery(#[from] DiscoveryError),
32
33 /// A `reqwest`-level failure: DNS/connect/TLS, a request timeout, or a
34 /// response-body decode failure. Carries the URL for context.
35 #[error("request to {url} failed: {source}")]
36 Transport {
37 url: String,
38 #[source]
39 source: reqwest::Error,
40 },
41
42 /// The daemon answered with a non-2xx HTTP status that carries no
43 /// JSON-RPC error envelope to parse (the SSE routes return a bare HTTP
44 /// status on failure, unlike `POST /rpc`, which always returns `200`
45 /// with the error carried in the envelope — see
46 /// `crate::serve::http::rpc_handler`'s docs).
47 #[error("request to {url} returned HTTP {status}")]
48 Status {
49 url: String,
50 status: reqwest::StatusCode,
51 },
52
53 /// The daemon closed an SSE stream cleanly (no transport error, no
54 /// non-2xx status) before this client observed a terminal event
55 /// (`SessionDone`/`SessionCancelled`) — and every reconnect attempt
56 /// (bounded by `SESSION_STREAM_MAX_RECONNECTS`) hit the same premature
57 /// close. Distinct from [`Self::Status`]/[`Self::Transport`]: nothing
58 /// about the individual HTTP request failed, only the STREAM never
59 /// reached a terminal state.
60 #[error("event stream for {url} closed repeatedly with no terminal session event observed")]
61 StreamClosed { url: String },
62
63 /// The daemon's `POST /rpc` response body didn't have the shape this
64 /// client expected (e.g. `session.create`'s result missing an `id`
65 /// field) — a daemon/client version-skew symptom, not a transport
66 /// failure.
67 #[error("malformed response from daemon: {0}")]
68 Malformed(String),
69
70 /// The daemon's JSON-RPC error envelope for a `POST /rpc` call
71 /// (verbatim: code + message + optional `data`).
72 #[error("daemon returned an error ({code}): {message}")]
73 Rpc {
74 code: i32,
75 message: String,
76 #[allow(dead_code)]
77 // surfaced for callers that want to inspect it; not read internally yet
78 data: Option<Value>,
79 },
80
81 /// `handle_input`/`cancel_session` was called before `setup` minted a
82 /// session (or after a session was never successfully created) — a
83 /// caller-sequencing error, never a transport failure.
84 #[error("no active tcode session — `setup` must complete before sending input")]
85 NoSession,
86}