Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
* `Async` appender's `flush` method is now blocking until all buffered logs are flushed by worker threads. Any errors during flushing will be propagated back to the `flush` caller.
* `Record::payload` is now `std::fmt::Arguments` instead of `Cow<'static, str>`.
* `RecordOwned::as_record` has been removed; use `RecordOwned::with` instead. (This is a limitation of Rust as described [here](https://github.com/rust-lang/rust/issues/92698#issuecomment-3311144848).)
* `logforth_core::Error::with_source` now set the optional source field instead of append a sources list.

## [0.29.1] 2025-11-03

Expand Down
40 changes: 19 additions & 21 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::io;
/// The error struct of logforth.
pub struct Error {
message: String,
sources: Vec<anyhow::Error>,
source: Option<anyhow::Error>,
context: Vec<(&'static str, String)>,
}

Expand All @@ -40,15 +40,8 @@ impl fmt::Display for Error {
write!(f, " }}")?;
}

if !self.sources.is_empty() {
write!(f, ", sources: [")?;
for (i, source) in self.sources.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{source}")?;
}
write!(f, "]")?;
if let Some(source) = &self.source {
write!(f, ", source: {source}")?;
}

Ok(())
Expand All @@ -62,7 +55,7 @@ impl fmt::Debug for Error {
let mut de = f.debug_struct("Error");
de.field("message", &self.message);
de.field("context", &self.context);
de.field("sources", &self.sources);
de.field("sources", &self.source);
return de.finish();
}

Expand All @@ -76,12 +69,11 @@ impl fmt::Debug for Error {
writeln!(f, " {k}: {v}")?;
}
}
if !self.sources.is_empty() {

if let Some(source) = &self.source {
writeln!(f)?;
writeln!(f, "Sources:")?;
for source in self.sources.iter() {
writeln!(f, " {source:#}")?;
}
writeln!(f, "Source:")?;
writeln!(f, " {source:#}")?;
}

Ok(())
Expand All @@ -90,7 +82,7 @@ impl fmt::Debug for Error {

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.sources.first().map(|v| v.as_ref())
self.source.as_ref().map(|v| v.as_ref())
}
}

Expand All @@ -99,7 +91,7 @@ impl Error {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
sources: vec![],
source: None,
context: vec![],
}
}
Expand All @@ -110,15 +102,21 @@ impl Error {
self
}

/// Add one more source in error.
/// Set source for error.
///
/// # Panics
///
/// If the source has been set, we will raise a panic here.
pub fn with_source(mut self, src: impl Into<anyhow::Error>) -> Self {
self.sources.push(src.into());
assert!(self.source.is_none(), "the source error has been set");

self.source = Some(src.into());
self
}

/// Return an iterator over all sources of this error.
pub fn sources(&self) -> impl ExactSizeIterator<Item = &(dyn std::error::Error + 'static)> {
self.sources.iter().map(|v| v.as_ref())
self.source.iter().map(|v| v.as_ref())
}

/// Default constructor for [`Error`] from [`io::Error`].
Expand Down