From 7a21b06a80c8ac8d0587e74d50cee42459aa695f Mon Sep 17 00:00:00 2001 From: Rafa Moreno Date: Fri, 27 Nov 2026 18:51:42 +0900 Subject: [PATCH 1/2] Keep healthy CLI clusters available on regional proxy 403s --- src/client.rs | 35 ++++++++++++++++++++++++++--------- src/client/discovery.rs | 20 ++++++++++---------- tests/regional_discovery.rs | 33 +++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 23 deletions(-) diff --git a/src/client.rs b/src/client.rs index 4ac708d..f05c388 100644 --- a/src/client.rs +++ b/src/client.rs @@ -284,17 +284,34 @@ fn check_status(resp: Response) -> Result<()> { Err(format_server_error(&text, status.as_u16())) } +// RawTree API errors carry both a machine-readable code and a message. +// A private deployment's proxy may also respond with HTTP 403. +#[derive(Debug)] +struct ApiResponseError(String); + +impl std::fmt::Display for ApiResponseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.0) + } +} + fn format_server_error(body: &str, status: u16) -> anyhow::Error { - if let Ok(json) = serde_json::from_str::(body) { - let message = json["message"].as_str().unwrap_or("Unknown error"); - let hint = json["hint"].as_str().unwrap_or(""); - if hint.is_empty() { - anyhow::anyhow!("Server error ({}): {}", status, message) - } else { - anyhow::anyhow!("Server error ({}): {}\nHint: {}", status, message, hint) - } + let Ok(json) = serde_json::from_str::(body) else { + return anyhow::anyhow!("Server error ({status}): {body}"); + }; + let code = json["error"].as_str().filter(|s| !s.is_empty()); + let detail = json["message"].as_str().filter(|s| !s.is_empty()); + let mut message = format!( + "Server error ({status}): {}", + detail.or(code).unwrap_or("Unknown error") + ); + if let Some(hint) = json["hint"].as_str().filter(|hint| !hint.is_empty()) { + message.push_str(&format!("\nHint: {hint}")); + } + if code.is_some() && detail.is_some() { + anyhow::Error::msg(ApiResponseError(message)) } else { - anyhow::anyhow!("Server error ({}): {}", status, body) + anyhow::Error::msg(message) } } diff --git a/src/client/discovery.rs b/src/client/discovery.rs index 782e50c..7156682 100644 --- a/src/client/discovery.rs +++ b/src/client/discovery.rs @@ -2,7 +2,7 @@ use super::{ handle_response, regions::Region, routing::{before_deadline, valid_routing}, - ApiClient, + ApiClient, ApiResponseError, }; use anyhow::{Context, Result}; use reqwest::{blocking::RequestBuilder, Method, StatusCode}; @@ -59,16 +59,16 @@ fn load_catalogs(requests: Vec, deadline: Instant) -> Result { let status = response.status(); - let result = handle_response(response); - if matches!( - status, - StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN - ) { - return Err(result.err().unwrap_or_else(|| { - anyhow::anyhow!("Access denied ({status})") - })); + match handle_response(response) { + Err(error) + if status == StatusCode::UNAUTHORIZED + || (status == StatusCode::FORBIDDEN + && error.is::()) => + { + return Err(error); + } + result => result, } - result } Err(error) => Err(error), }; diff --git a/tests/regional_discovery.rs b/tests/regional_discovery.rs index 8b2ff8f..2cf2450 100644 --- a/tests/regional_discovery.rs +++ b/tests/regional_discovery.rs @@ -10,6 +10,7 @@ use std::time::{Duration, Instant}; #[derive(Clone, Copy)] enum Failure { Http(u16), + Proxy(u16, &'static str), Disconnect, Stall, SlowBody, @@ -73,7 +74,10 @@ fn run(args: &[&str], failure: Failure, all_fail: bool) -> (Output, Vec, }]})); } else { match failure { - Failure::Http(status) => reply(&mut socket, status, json!({"message":"Regional failure"})), + Failure::Http(status) => reply(&mut socket, status, json!({"error":"forbidden","message":"Regional failure","hint":"Check access."})), + Failure::Proxy(status, body) => { + write!(socket, "HTTP/1.1 {status} Test\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).unwrap(); + } Failure::Disconnect => (), Failure::Stall => thread::sleep(Duration::from_secs(6)), Failure::SlowBody => { @@ -121,7 +125,13 @@ fn run(args: &[&str], failure: Failure, all_fail: bool) -> (Output, Vec, #[test] fn table_list_survives_http_and_connection_failures_in_other_regions() { - for failure in [Failure::Http(503), Failure::Disconnect] { + for failure in [ + Failure::Http(503), + Failure::Proxy(403, "Forbidden"), + Failure::Proxy(403, r#"{"message":"Forbidden"}"#), + Failure::Proxy(403, r#"{"error":"Forbidden"}"#), + Failure::Disconnect, + ] { let (output, requests, _) = run(&["--json", "table", "list"], failure, false); assert!( output.status.success(), @@ -169,7 +179,7 @@ fn cluster_list_exposes_partial_catalog_in_json_and_warns_in_text() { } else { vec!["cluster", "list"] }; - let (output, _, _) = run(&args, Failure::Http(503), false); + let (output, _, _) = run(&args, Failure::Proxy(403, "{}"), false); assert!( output.status.success(), "{}", @@ -208,6 +218,21 @@ fn authorization_and_total_outages_fail_without_executing_table_request() { } } +#[test] +fn raw_http_denials_remain_errors_for_expired_credentials_or_a_total_outage() { + for (status, all_fail) in [(401, false), (403, true)] { + let (output, _, _) = run( + &["--json", "cluster", "list"], + Failure::Proxy(status, ""), + all_fail, + ); + assert!(!output.status.success()); + assert!( + String::from_utf8_lossy(&output.stderr).contains(&format!("Server error ({status})")) + ); + } +} + #[test] fn missing_cluster_in_partial_catalog_never_falls_back_to_gateway() { let (output, requests, _) = run( @@ -234,7 +259,7 @@ fn management_commands_report_outages_for_missing_targets_in_partial_catalogs() if command == "update" { args.extend(["--idle-timeout-minutes", "60"]); } - let (output, requests, _) = run(&args, Failure::Http(503), false); + let (output, requests, _) = run(&args, Failure::Proxy(403, "{}"), false); assert!(!output.status.success(), "{command} {target}"); assert!(output.stdout.is_empty()); let error: Value = serde_json::from_slice(&output.stderr).unwrap(); From b8882726047dfd5cef266f3dc8b37975e50f3ba6 Mon Sep 17 00:00:00 2001 From: Rafa Moreno Date: Fri, 27 Nov 2026 19:51:41 +0900 Subject: [PATCH 2/2] Bump CLI patch version to 0.8.6 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 456b006..6a4b815 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1550,7 +1550,7 @@ dependencies = [ [[package]] name = "rawtree-cli" -version = "0.8.5" +version = "0.8.6" dependencies = [ "anyhow", "axoupdater", diff --git a/Cargo.toml b/Cargo.toml index fb18af1..6e226ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rawtree-cli" -version = "0.8.5" +version = "0.8.6" edition = "2021" repository = "https://github.com/rawtreedb/rawtree-cli"