pub fn format_connection_error(error: &anyhow::Error) -> String {
format!("{error:#}")
}
#[cfg(test)]
mod tests {
use super::*;
use anyhow::anyhow;
#[test]
fn includes_all_context_layers() {
let root = anyhow!("authentication failed for user 'alice' on '10.0.0.5:22'");
let with_channel =
root.context("Failed to open direct-tcpip channel to destination 10.0.0.5:22");
let with_hop = with_channel.context("Failed to connect to jump host bastion (hop 2)");
let with_outer =
with_hop.context("Failed to establish jump host connection to 10.0.0.5:22");
let formatted = format_connection_error(&with_outer);
assert!(
formatted.contains("Failed to establish jump host connection to 10.0.0.5:22"),
"missing outer context layer in: {formatted}"
);
assert!(
formatted.contains("Failed to connect to jump host bastion (hop 2)"),
"missing intermediate hop layer in: {formatted}"
);
assert!(
formatted.contains("Failed to open direct-tcpip channel to destination 10.0.0.5:22"),
"missing channel-open layer in: {formatted}"
);
assert!(
formatted.contains("authentication failed for user 'alice' on '10.0.0.5:22'"),
"missing innermost root cause in: {formatted}"
);
}
#[test]
fn distinguishes_different_failure_hops() {
let first_jump_root = anyhow!("connection refused");
let first_jump = first_jump_root.context("Failed to connect to first jump host: bastion1");
let dest_auth_root = anyhow!("permission denied (publickey)");
let dest_auth = dest_auth_root
.context("Failed to authenticate to destination '10.0.0.5:22' as user 'alice'");
let first_jump_msg = format_connection_error(&first_jump);
let dest_auth_msg = format_connection_error(&dest_auth);
assert_ne!(first_jump_msg, dest_auth_msg);
assert!(first_jump_msg.contains("connection refused"));
assert!(dest_auth_msg.contains("permission denied (publickey)"));
}
#[test]
fn single_layer_error_still_formats() {
let error = anyhow!("simple failure");
assert_eq!(format_connection_error(&error), "simple failure");
}
}