Skip to content

fix: propagate formatting errors in PlainTextLayout and TextLayout - #310

Merged
tisonkun merged 4 commits into
mainfrom
fix/text-layout-fmt-error
Dec 1, 2026
Merged

tisonkun merged 4 commits into
mainfrom
fix/text-layout-fmt-error

Conversation

@tisonkun

@tisonkun tisonkun commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Propagate fmt::Error and roll back partial buffer writes in PlainTextLayout and TextLayout instead of panicking on the caller thread.

Previously, writing into buf: &mut Vec<u8> through std::io::Write::write_fmt and .unwrap() panicked inside std::io::default_write_fmt whenever a user Display or Debug implementation returned Err(fmt::Error), and visitor errors left partially formatted bytes appended to buf.

Changes

  • logforth-core (PlainTextLayout) & logforth-layout-text (TextLayout):
    • Format text segments via a std::fmt::Write adapter over Vec<u8> (and propagate jiff timestamp printing errors in TextLayout) as Error values preserving .source().
    • Record buf.len() up front and truncate buf back to its starting length on any formatting or key-value/diagnostic visitor error.
    • Document # Errors contracts on PlainTextLayout and TextLayout, and update CHANGELOG.md.
  • Integration Tests:
    • Add FailingDisplay payload and key-value tests alongside FailingDiag rollback assertions for both PlainTextLayout and TextLayout, verifying untouched caller buffers and end-to-end routing to ErrorSink.

`PlainTextLayout` and `TextLayout` previously wrote into `Vec<u8>` via
`std::io::Write::write_fmt` with `.unwrap()`, causing `std::io` to panic
on the caller thread whenever a payload or structured value `Display` or
`Debug` implementation returned `Err(fmt::Error)`, and leaving partial
bytes in `buf` when visiting key-values or diagnostics failed.

Format record segments through `std::fmt::Write`, capture `buf`'s
starting length, truncate partial output on any timestamp, formatting,
or visitor error, and return `Err(Error)` with the underlying cause so
the pipeline forwards failures to `ErrorSink` without panicking.

Signed-off-by: tison <[email protected]>
Replace the inline closures in `PlainTextLayout::format` and
`TextLayout::format` with `do_format` methods, free error helper
functions, and `kv::Visitor` on `FmtBuf` so error propagation and buffer
rollback read top-to-bottom without nested closure indentation.

Signed-off-by: tison <[email protected]>
@tisonkun
tisonkun enabled auto-merge (squash) December 1, 2026 01:54
@tisonkun
tisonkun merged commit b586df6 into main Dec 1, 2026
9 checks passed
@tisonkun
tisonkun deleted the fix/text-layout-fmt-error branch December 1, 2026 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant