From 0e2afd0772fc3729e4076385f85c9faf0df953e9 Mon Sep 17 00:00:00 2001 From: Tamipes Date: Fri, 29 May 2026 11:07:59 +0200 Subject: [PATCH] feat: polish the OpaqueError get_span_trace function looks --- src/kube_cache.rs | 2 +- src/main.rs | 8 ++++---- src/opaque_error.rs | 29 +++++++++++++++++++---------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/kube_cache.rs b/src/kube_cache.rs index 95e34d9..c8be2e5 100644 --- a/src/kube_cache.rs +++ b/src/kube_cache.rs @@ -189,7 +189,7 @@ impl MinecraftAPI for McApi { drop(guard); if let Err(err) = server.stop().await { tracing::error!( - trace = err.get_span_trace(), + trace = %err.print_span_trace(), err = err.context, msg = "failed to stop server" ); diff --git a/src/main.rs b/src/main.rs index 6a8322d..80c5a3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,26 +61,26 @@ async fn main() { match e.level { tracing::Level::ERROR => tracing::error!( // addr = format!("{}:{}", addr.ip().to_string(), addr.port().to_string()), - trace = format!("{}", e.get_span_trace()), + trace = %e.print_span_trace(), err = format!("{}", e.context), "Client disconnected" ), tracing::Level::WARN => tracing::warn!( // addr = format!("{}:{}", addr.ip().to_string(), addr.port().to_string()), - trace = format!("{}", e.get_span_trace()), + trace = %e.print_span_trace(), err = format!("{}", e.context), "Client disconnected" ), tracing::Level::INFO => tracing::info!( // addr = format!("{}:{}", addr.ip().to_string(), addr.port().to_string()), - trace = format!("{}", e.get_span_trace()), + trace = %e.print_span_trace(), err = format!("{}", e.context), "Client disconnected" ), _ => { tracing::error!( // addr = format!("{}:{}", addr.ip().to_string(), addr.port().to_string()), - trace = format!("{}", e.get_span_trace()), + trace = %e.print_span_trace(), err = format!("{}", e.context), actual_level = ?e.level, "Client disconnected (bad level)" diff --git a/src/opaque_error.rs b/src/opaque_error.rs index 626936f..6db5a7a 100644 --- a/src/opaque_error.rs +++ b/src/opaque_error.rs @@ -1,4 +1,7 @@ -use std::{error::Error, fmt}; +use std::{ + error::Error, + fmt::{self, Debug}, +}; use tracing::Level; use tracing_error::SpanTrace; @@ -50,7 +53,7 @@ impl OpaqueError { kind: Some(kind.to_string()), } } - pub fn get_span_trace(&self) -> String { + pub fn print_span_trace(&self) -> FancyTrace { let mut vec: Vec<(&str, String)> = Vec::new(); self.span_trace.with_spans(|metadata, _fields| { @@ -64,16 +67,14 @@ impl OpaqueError { } acc.push_str(x.0); - acc.push_str("{"); - acc.push_str(&x.1); - acc.push_str("}"); - + if !x.1.is_empty() { + acc.push_str("{"); + acc.push_str(&x.1); + acc.push_str("}"); + } acc }); - match String::from_utf8(strip_ansi_escapes::strip(str.clone())) { - Ok(x) => x, - Err(_) => str, - } + FancyTrace { str } } pub fn set_level(mut self, lvl: Level) -> Self { self.level = lvl; @@ -86,6 +87,14 @@ impl OpaqueError { } } } +pub struct FancyTrace { + str: String, +} +impl fmt::Display for FancyTrace { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.str) + } +} impl From for OpaqueError { fn from(value: String) -> Self {