Skip to content

Commit 951f80d

Browse files
committed
fix: report a clear cli error when no herdr server is running
socket cli commands surfaced a raw io::Error debug string (`Error: Os { code: 2, ... }`) when nothing was listening on the api socket, which read like a bad --cwd path. map dead-socket connect failures to a `server_not_running` json error carrying the resolved socket path, printed once at the edge that surfaces the error; recovering callers (plugin offline registry fallback, agent start polling) recognize the marker and keep their existing behavior. refs #1941
1 parent 26a7bc8 commit 951f80d

6 files changed

Lines changed: 175 additions & 18 deletions

File tree

src/cli.rs

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod plugin;
1818
mod protocol_guard;
1919
mod runtime;
2020
mod server;
21+
mod server_not_running;
2122
mod spec;
2223
mod status;
2324
mod tab;
@@ -745,7 +746,7 @@ pub(super) fn send_request(request: &Request) -> std::io::Result<serde_json::Val
745746
ensure_server_protocol_compatible(&client, &request.id)?;
746747
client
747748
.request_value(request)
748-
.map_err(api_client_error_to_io)
749+
.map_err(|err| map_server_not_running_or_io(err, &request.id, &client))
749750
}
750751

751752
pub(super) fn send_request_unchecked(request: &Request) -> std::io::Result<serde_json::Value> {
@@ -755,7 +756,9 @@ pub(super) fn send_request_unchecked(request: &Request) -> std::io::Result<serde
755756
}
756757

757758
fn ensure_server_protocol_compatible(client: &ApiClient, request_id: &str) -> std::io::Result<()> {
758-
let status = client.status().map_err(api_client_error_to_io)?;
759+
let status = client
760+
.status()
761+
.map_err(|err| map_server_not_running_or_io(err, request_id, client))?;
759762
let server_protocol = status
760763
.protocol
761764
.ok_or_else(|| std::io::Error::other("server ping did not include a protocol version"))?;
@@ -778,6 +781,49 @@ pub(crate) fn protocol_mismatch_was_reported(err: &std::io::Error) -> bool {
778781
protocol_guard::was_reported(err)
779782
}
780783

784+
pub(crate) fn server_not_running_was_reported(err: &std::io::Error) -> bool {
785+
server_not_running::was_reported(err)
786+
}
787+
788+
/// Returns the `ErrorResponse` carried by a `server_not_running` marker, if any,
789+
/// so the edge that surfaces the error can print it exactly once (deferred
790+
/// printing: recovering callers like plugin offline fallback print nothing).
791+
pub(crate) fn server_not_running_reported_response(
792+
err: &std::io::Error,
793+
) -> Option<&crate::api::schema::ErrorResponse> {
794+
server_not_running::reported_response(err)
795+
}
796+
797+
/// True when an io::Error indicates nothing is listening on the API socket.
798+
/// Classify by `ErrorKind` only: Windows named pipes surface different raw
799+
/// errno values than Unix domain sockets but the same error kinds.
800+
pub(super) fn server_not_running_error(err: &std::io::Error) -> bool {
801+
matches!(
802+
err.kind(),
803+
std::io::ErrorKind::NotFound | std::io::ErrorKind::ConnectionRefused
804+
)
805+
}
806+
807+
/// Maps an `ApiClientError` from a socket command into the io::Error that
808+
/// bubbles up to `main`. A dead-server connect failure is reported as a
809+
/// friendly `server_not_running` JSON error plus a recognizable marker; all
810+
/// other errors fall through unchanged so existing handling is preserved.
811+
fn map_server_not_running_or_io(
812+
err: ApiClientError,
813+
request_id: &str,
814+
client: &ApiClient,
815+
) -> std::io::Error {
816+
match err {
817+
ApiClientError::Io(io_err) if server_not_running_error(&io_err) => {
818+
server_not_running::reported_error(server_not_running::response(
819+
request_id,
820+
&client.socket_path(),
821+
))
822+
}
823+
err => api_client_error_to_io(err),
824+
}
825+
}
826+
781827
fn api_client_error_to_io(err: ApiClientError) -> std::io::Error {
782828
match err {
783829
ApiClientError::Io(err) => err,
@@ -1036,4 +1082,42 @@ mod tests {
10361082
"env must use KEY=VALUE"
10371083
);
10381084
}
1085+
1086+
#[test]
1087+
fn maps_dead_server_connect_failure_to_friendly_error() {
1088+
use crate::api::client::{ApiClient, ApiClientError};
1089+
1090+
let client = ApiClient::local();
1091+
let socket = client.socket_path().display().to_string();
1092+
1093+
// The helper does NOT print; it returns a recognizable marker carrying
1094+
// the ErrorResponse so the surfacing edge can print it exactly once.
1095+
let mapped = super::map_server_not_running_or_io(
1096+
ApiClientError::Io(std::io::Error::from(std::io::ErrorKind::NotFound)),
1097+
"cli:workspace:create",
1098+
&client,
1099+
);
1100+
1101+
let response = super::server_not_running::reported_response(&mapped)
1102+
.expect("dead-server connect failure should carry a server_not_running response");
1103+
assert_eq!(response.id, "cli:workspace:create");
1104+
assert_eq!(response.error.code, "server_not_running");
1105+
assert!(response.error.message.contains(&socket));
1106+
1107+
// The mapping is recognizable without string matching.
1108+
assert!(super::server_not_running::was_reported(&mapped));
1109+
}
1110+
1111+
#[test]
1112+
fn classifier_ignores_unrelated_io_kinds() {
1113+
use crate::api::client::{ApiClient, ApiClientError};
1114+
1115+
let client = ApiClient::local();
1116+
let mapped = super::map_server_not_running_or_io(
1117+
ApiClientError::Io(std::io::Error::from(std::io::ErrorKind::TimedOut)),
1118+
"cli:workspace:create",
1119+
&client,
1120+
);
1121+
assert!(!super::server_not_running::was_reported(&mapped));
1122+
}
10391123
}

src/cli/agent.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,13 @@ fn print_agent_transport_error(
577577
if super::protocol_mismatch_was_reported(&err) {
578578
return Ok(1);
579579
}
580+
// A dead-server marker reaches here from `send_request` in the agent
581+
// startup path; surface its deferred response exactly once instead of
582+
// printing a second, generic transport-error line.
583+
if let Some(response) = super::server_not_running_reported_response(&err) {
584+
let value = serde_json::to_value(response).map_err(std::io::Error::other)?;
585+
return super::print_response(&value);
586+
}
580587
super::print_response(&cli_agent_error(request_id, code, err.to_string()))
581588
}
582589

src/cli/plugin.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,14 +1618,19 @@ fn current_unix_ms() -> u64 {
16181618
}
16191619

16201620
fn is_connection_error(err: &std::io::Error) -> bool {
1621-
matches!(
1622-
err.kind(),
1623-
std::io::ErrorKind::NotFound
1624-
| std::io::ErrorKind::ConnectionRefused
1625-
| std::io::ErrorKind::ConnectionAborted
1626-
| std::io::ErrorKind::ConnectionReset
1627-
| std::io::ErrorKind::BrokenPipe
1628-
)
1621+
// A `server_not_running` marker is a connect failure for recovery purposes:
1622+
// treating it as a connection error lets plugin commands fall back to the
1623+
// offline registry. The marker carries (but does not print) a friendly
1624+
// response, so recovering here prints nothing.
1625+
super::server_not_running_was_reported(err)
1626+
|| matches!(
1627+
err.kind(),
1628+
std::io::ErrorKind::NotFound
1629+
| std::io::ErrorKind::ConnectionRefused
1630+
| std::io::ErrorKind::ConnectionAborted
1631+
| std::io::ErrorKind::ConnectionReset
1632+
| std::io::ErrorKind::BrokenPipe
1633+
)
16291634
}
16301635

16311636
fn print_plugin_response(method: Method) -> std::io::Result<i32> {

src/cli/server_not_running.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::fmt;
2+
use std::path::Path;
3+
4+
use crate::api::schema::{ErrorBody, ErrorResponse};
5+
6+
/// Marker error signalling a dead API socket. Carries the `ErrorResponse` that
7+
/// should be printed at the edge that finally surfaces the error, so callers
8+
/// that recover (e.g. plugin offline fallback) print nothing. Mirrors
9+
/// `ProtocolMismatchReported`, except printing is deferred because several CLI
10+
/// commands recover from a dead server instead of reporting it.
11+
#[derive(Debug)]
12+
pub(super) struct ServerNotRunningReported {
13+
pub(super) response: ErrorResponse,
14+
}
15+
16+
impl fmt::Display for ServerNotRunningReported {
17+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18+
// Delegate to the carried message so pre-existing paths that
19+
// stringify transport errors still show the actionable text.
20+
f.write_str(&self.response.error.message)
21+
}
22+
}
23+
24+
impl std::error::Error for ServerNotRunningReported {}
25+
26+
/// Builds the friendly `server_not_running` ErrorResponse shown when no
27+
/// server is listening on the resolved API socket.
28+
pub(super) fn response(request_id: &str, socket_path: &Path) -> ErrorResponse {
29+
ErrorResponse {
30+
id: request_id.to_string(),
31+
error: ErrorBody {
32+
code: "server_not_running".into(),
33+
message: format!(
34+
"no herdr server is running at {}; run `herdr` to start one",
35+
socket_path.display()
36+
),
37+
},
38+
}
39+
}
40+
41+
/// Wraps the response in the recognizable marker WITHOUT printing. The caller
42+
/// that ultimately surfaces the error prints the carried response (see
43+
/// `reported_response`); recovering callers simply drop it.
44+
pub(super) fn reported_error(response: ErrorResponse) -> std::io::Error {
45+
std::io::Error::other(ServerNotRunningReported { response })
46+
}
47+
48+
pub(super) fn was_reported(err: &std::io::Error) -> bool {
49+
err.get_ref()
50+
.and_then(|source| source.downcast_ref::<ServerNotRunningReported>())
51+
.is_some()
52+
}
53+
54+
/// Returns the `ErrorResponse` carried by a `server_not_running` marker, if any,
55+
/// so the surfacing edge can print it exactly once.
56+
pub(super) fn reported_response(err: &std::io::Error) -> Option<&ErrorResponse> {
57+
err.get_ref()
58+
.and_then(|source| source.downcast_ref::<ServerNotRunningReported>())
59+
.map(|reported| &reported.response)
60+
}

src/cli/status.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,13 @@ fn read_server_runtime_status() -> std::io::Result<ServerRuntimeStatus> {
159159
protocol: status.protocol,
160160
capabilities: status.capabilities,
161161
}),
162-
Err(ApiClientError::Io(err)) if server_not_running_error(&err) => {
162+
Err(ApiClientError::Io(err)) if super::server_not_running_error(&err) => {
163163
Ok(ServerRuntimeStatus::NotRunning)
164164
}
165165
Err(err) => Err(api_client_error_to_io(err)),
166166
}
167167
}
168168

169-
fn server_not_running_error(err: &std::io::Error) -> bool {
170-
matches!(
171-
err.kind(),
172-
std::io::ErrorKind::NotFound | std::io::ErrorKind::ConnectionRefused
173-
)
174-
}
175-
176169
fn api_client_error_to_io(err: ApiClientError) -> std::io::Error {
177170
match err {
178171
ApiClientError::Io(err) => err,

src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,14 @@ fn main() -> io::Result<()> {
497497
Ok(cli::CommandOutcome::Handled(code)) => std::process::exit(code),
498498
Ok(cli::CommandOutcome::NotCli) => {}
499499
Err(err) if cli::protocol_mismatch_was_reported(&err) => std::process::exit(1),
500+
Err(err) if cli::server_not_running_was_reported(&err) => {
501+
if let Some(response) = cli::server_not_running_reported_response(&err) {
502+
if let Ok(json) = serde_json::to_string(response) {
503+
eprintln!("{json}");
504+
}
505+
}
506+
std::process::exit(1);
507+
}
500508
Err(err) => return Err(err),
501509
}
502510

0 commit comments

Comments
 (0)