Skip to content

refactor!: unify Error and ErrorSink without anyhow - #246

Open
tisonkun wants to merge 2 commits into
mainfrom
refactor/error-sink
Open

tisonkun wants to merge 2 commits into
mainfrom
refactor/error-sink

Conversation

@tisonkun

Copy link
Copy Markdown
Contributor

Problem

  1. logforth-core depended on anyhow solely for Error::with_source, leaking anyhow::Error into its public signature while returning an unboxed 56-byte Error struct by value across Visitor::visit, Diagnostic::visit, Layout::format, and Append::append.
  2. Runtime pipeline errors followed three disconnected paths:
    • Logger bypassed Trap, wrote multi-line dumps directly to standard error, and panicked if standard error was unavailable.
    • Async forwarded worker errors to Trap, whose fmt::Debug supertrait prevented passing closures directly.
    • File returned Result for writes and flushes while routing rollover housekeeping and Drop flush errors to a separate FileBuilder::trap.

Solution

  1. Zero-dependency, pointer-sized Error:
    • Box Error's payload (Cow<'static, str> message + optional Box<dyn std::error::Error + Send + Sync + 'static> source) so logforth-core has zero mandatory dependencies and Result<(), Error> fits in a single word (8 bytes on 64-bit targets).
    • Add Error::message(), walk the full .source() chain in Debug, allow with_source to replace an existing cause without panicking, and remove unused with_context, sources, from_io_error, and from_fmt_error helpers in favor of descriptive operation messages at each call site.
  2. Unified error::ErrorSink at execution roots:
    • Replace trap::Trap and BestEffortTrap with a cloneable error::ErrorSink value supporting ErrorSink::stderr() (default, best-effort without panicking), ErrorSink::black_hole(), and ErrorSink::new(...) / closure From conversions.
    • Wire .error_sink(...) into LoggerBuilder, LogStarterBuilder, LogStarterStdStreamBuilder, LogStarterTestingBuilder, and AsyncBuilder, removing the hardcoded standard-error panic path in Logger.
    • Remove FileBuilder::trap; when log rotation or archive cleanup fails, RollingFileWriter still writes the current record to the active file and returns the rollover error through Append::append to the owning execution root.

Validation

  • cargo x test
  • cargo x lint
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --workspace --all-features
  • cargo +1.91.0 test -p logforth-core
  • cargo tree -p logforth-core --edges normal (zero dependencies)

`logforth-core` previously depended on `anyhow` solely for `Error::with_source`, leaking `anyhow::Error` into its public signature while returning an unboxed 56-byte `Error` struct across hot `Visitor::visit` and `Append::append` paths.

Box the internal error representation into a pointer-sized payload carrying a `Cow<'static, str>` message and an optional `Box<dyn std::error::Error + Send + Sync + 'static>` cause:
- Drop the mandatory `anyhow` dependency from `logforth-core` while preserving `Into` compatibility for standard errors, strings, and boxed causes.
- Shrink `Result<(), Error>` to a single word on the stack, add `Error::message()`, walk the full `.source()` chain in `Debug`, and allow `with_source` to replace causes without panicking.
- Remove unused `with_context`, `sources`, `from_io_error`, and `from_fmt_error` helpers, replacing generic I/O wrappers with descriptive operation messages at each appender call site.
- Expose `logforth_core::error` and `logforth::error` modules directly.

Signed-off-by: tison <wander4096@gmail.com>
Runtime pipeline failures previously followed three disconnected paths: `Logger` wrote multi-line dumps directly to standard error and panicked if standard error was unavailable, `Async` forwarded worker failures to `Trap`, and `File` mixed `Result` returns with a separate `FileBuilder::trap` for rollover housekeeping and `Drop`. In addition, requiring `fmt::Debug` on `Trap` prevented passing closures directly.

Unify internal runtime error handling around a cloneable `error::ErrorSink` value:
- Replace `trap::Trap` and `BestEffortTrap` with `error::ErrorSink` providing `ErrorSink::stderr()` (default, best-effort without panicking), `ErrorSink::black_hole()`, and `ErrorSink::new(...)` / closure `From` conversions.
- Wire `.error_sink(...)` into `LoggerBuilder`, `LogStarterBuilder`, `LogStarterStdStreamBuilder`, `LogStarterTestingBuilder`, and `AsyncBuilder`, removing the hardcoded standard-error panic path in `Logger`.
- Remove `FileBuilder::trap` and `RollingFileWriter`'s `Drop` callback; when log rotation or archive cleanup fails, finish writing the current record to the active file and return the rollover error through `Append::append` to the owning execution root.

Signed-off-by: tison <wander4096@gmail.com>
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