Skip to content

Commit

Permalink
Replace concrete error types with Box<dyn Error> for downstreams
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Oct 7, 2023
1 parent 0901547 commit 3c93ca6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 17 deletions.
5 changes: 3 additions & 2 deletions spdlog/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Provides error types.

use std::{
error::Error as StdError,
fmt::{self, Display},
io, result,
};
Expand Down Expand Up @@ -29,14 +30,14 @@ pub enum Error {
///
/// [`Sink`]: crate::sink::Sink
#[error("write record error: {0}")]
WriteRecord(io::Error),
WriteRecord(Box<dyn StdError>),

/// The variant returned by [`Sink`]s when an error occurs in flushing the
/// buffer.
///
/// [`Sink`]: crate::sink::Sink
#[error("flush buffer error: {0}")]
FlushBuffer(io::Error),
FlushBuffer(Box<dyn StdError>),

/// The variant returned by [`Sink`]s when an error occurs in creating a
/// directory.
Expand Down
9 changes: 6 additions & 3 deletions spdlog/src/sink/file_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ impl Sink for FileSink {
self.file
.lock()
.write_all(string_buf.as_bytes())
.map_err(Error::WriteRecord)?;
.map_err(|err| Error::WriteRecord(err.into()))?;

Ok(())
}

fn flush(&self) -> Result<()> {
self.file.lock().flush().map_err(Error::FlushBuffer)
self.file
.lock()
.flush()
.map_err(|err| Error::FlushBuffer(err.into()))
}

helper::common_impl!(@Sink: common_impl);
Expand All @@ -92,7 +95,7 @@ impl Drop for FileSink {
fn drop(&mut self) {
if let Err(err) = self.file.lock().flush() {
self.common_impl
.non_returnable_error("FileSink", Error::FlushBuffer(err))
.non_returnable_error("FileSink", Error::FlushBuffer(err.into()))
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion spdlog/src/sink/journald_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ impl Sink for JournaldSink {
None => [None, None],
};

journal_send(kvs.iter().chain(srcloc_kvs.iter().flatten())).map_err(Error::WriteRecord)
journal_send(kvs.iter().chain(srcloc_kvs.iter().flatten()))
.map_err(|err| Error::WriteRecord(err.into()))
}

fn flush(&self) -> Result<()> {
Expand Down
14 changes: 9 additions & 5 deletions spdlog/src/sink/rotating_file_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ impl Rotator for RotatorFileSize {
.as_mut()
.unwrap()
.write_all(string_buf.as_bytes())
.map_err(Error::WriteRecord)
.map_err(|err| Error::WriteRecord(err.into()))
}

fn flush(&self) -> Result<()> {
Expand All @@ -484,13 +484,13 @@ impl Rotator for RotatorFileSize {
.as_mut()
.unwrap()
.flush()
.map_err(Error::FlushBuffer)
.map_err(|err| Error::FlushBuffer(err.into()))
}

fn drop_flush(&mut self) -> Result<()> {
let mut inner = self.inner.lock();
if let Some(file) = inner.file.as_mut() {
file.flush().map_err(Error::FlushBuffer)
file.flush().map_err(|err| Error::FlushBuffer(err.into()))
} else {
Ok(())
}
Expand Down Expand Up @@ -681,7 +681,7 @@ impl Rotator for RotatorTimePoint {
inner
.file
.write_all(string_buf.as_bytes())
.map_err(Error::WriteRecord)?;
.map_err(|err| Error::WriteRecord(err.into()))?;

if should_rotate && inner.file_paths.is_some() {
self.push_new_remove_old(file_path.unwrap(), &mut inner)?;
Expand All @@ -691,7 +691,11 @@ impl Rotator for RotatorTimePoint {
}

fn flush(&self) -> Result<()> {
self.inner.lock().file.flush().map_err(Error::FlushBuffer)
self.inner
.lock()
.file
.flush()
.map_err(|err| Error::FlushBuffer(err.into()))
}
}

Expand Down
9 changes: 6 additions & 3 deletions spdlog/src/sink/std_stream_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,22 @@ impl Sink for StdStreamSink {
}
Ok(())
})()
.map_err(Error::WriteRecord)?;
.map_err(|err: io::Error| Error::WriteRecord(err.into()))?;

// stderr is not buffered, so we don't need to flush it.
// https://doc.rust-lang.org/std/io/fn.stderr.html
if let StdStreamDest::Stdout(_) = dest {
dest.flush().map_err(Error::FlushBuffer)?;
dest.flush().map_err(|err| Error::FlushBuffer(err.into()))?;
}

Ok(())
}

fn flush(&self) -> Result<()> {
self.dest.lock().flush().map_err(Error::FlushBuffer)
self.dest
.lock()
.flush()
.map_err(|err| Error::FlushBuffer(err.into()))
}

helper::common_impl!(@Sink: common_impl);
Expand Down
11 changes: 8 additions & 3 deletions spdlog/src/sink/write_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ where

self.lock_target()
.write_all(string_buf.as_bytes())
.map_err(Error::WriteRecord)?;
.map_err(|err| Error::WriteRecord(err.into()))?;

Ok(())
}

fn flush(&self) -> Result<()> {
self.lock_target().flush().map_err(Error::FlushBuffer)
self.lock_target()
.flush()
.map_err(|err| Error::FlushBuffer(err.into()))
}

helper::common_impl!(@Sink: common_impl);
Expand All @@ -114,7 +116,10 @@ where
W: Write + Send,
{
fn drop(&mut self) {
let flush_result = self.lock_target().flush().map_err(Error::FlushBuffer);
let flush_result = self
.lock_target()
.flush()
.map_err(|err| Error::FlushBuffer(err.into()));
if let Err(err) = flush_result {
self.common_impl.non_returnable_error("WriteSink", err)
}
Expand Down

0 comments on commit 3c93ca6

Please sign in to comment.