Skip to content

Commit ab4d8d5

Browse files
committed
Rewrite inner Display
1 parent 0507018 commit ab4d8d5

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/inner.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,24 +220,40 @@ impl<C: 'static, C2: 'static> UniErrorInner<dyn UniKindCodes<Code = C, Code2 = C
220220

221221
impl<K: UniKind + ?Sized> Display for UniErrorInner<K> {
222222
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
223-
if let Some(context) = &self.context {
224-
write!(f, "{}", context)?;
223+
let mut context_written = false;
224+
let mut kind_context_written = false;
225+
226+
// *** Context ***
227+
if let Some(context) = &self.context
228+
&& !context.is_empty()
229+
{
230+
f.write_str(context)?;
231+
context_written = true;
225232
}
226233

227234
let cause = self.cause.as_ref().map(|inner| Cause::from_inner(inner));
228-
let context = self.kind.context(cause);
229-
if let Some(context) = context.as_ref() {
230-
if self.context.is_some() {
231-
write!(f, ": ")?;
235+
236+
// *** Kind Context ***
237+
if let Some(kind_context) = self.kind.context(cause).as_ref()
238+
&& !kind_context.is_empty()
239+
{
240+
if context_written {
241+
f.write_str(": ")?;
232242
}
233-
write!(f, "{}", context)?;
243+
f.write_str(kind_context)?;
244+
kind_context_written = true;
234245
}
235246

247+
// *** Cause ***
236248
if let Some(cause) = &self.prev_cause() {
237-
if self.context.is_some() || context.is_some() {
238-
write!(f, ": ")?;
249+
let cause = cause.to_string();
250+
251+
if !cause.is_empty() {
252+
if context_written || kind_context_written {
253+
f.write_str(": ")?;
254+
}
255+
f.write_str(&cause)?;
239256
}
240-
write!(f, "{}", cause)?;
241257
}
242258

243259
Ok(())

0 commit comments

Comments
 (0)