Skip to content
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ an I/O error occurred trying to open /some/path: file not found
be logged directly:

```rust
// Using the adapter constructor

// explicit key
info!(
log, "something happened"; "my-key" => InlineErrorChain::new(&err),
Expand All @@ -52,6 +54,18 @@ info!(
info!(
log, "something happened"; InlineErrorChain::new(&err),
);

// Using the blanket method implementation

// explicit key
info!(
log, "something happened"; "my-key" => err.as_inline_error_chain(),
);

// key omitted; will log with the key "error"
info!(
log, "something happened"; err.as_inline_error_chain(),
);
```

With the `derive` feature enabled, error types can `#[derive(SlogInlineError)]`
Expand Down
3 changes: 2 additions & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use slog::info;
use slog::o;
use slog::Drain;
use slog::Logger;
use slog_error_chain::AsInlineErrorChain;
use slog_error_chain::InlineErrorChain;
use std::io;
use std::path::PathBuf;
Expand Down Expand Up @@ -37,6 +38,6 @@ fn main() {
);
info!(
log, "logging error with InlineErrorChain, implicit key";
InlineErrorChain::new(&err),
err.as_inline_error_chain(),
);
}
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ impl fmt::Display for InlineErrorChain<'_> {
}
}

/// A trait for conveniently constructing [`InlineErrorChain`]s from [`Error`]s.
pub trait AsInlineErrorChain: Error {
/// A convenient wrapper around [`InlineErrorChain::new`].
fn as_inline_error_chain<'a>(&'a self) -> InlineErrorChain<'a>;
}

/// Implementation for all [`Error`] types
impl<E: Error> AsInlineErrorChain for E {
fn as_inline_error_chain<'a>(&'a self) -> InlineErrorChain<'a> {
InlineErrorChain::new(self)
}
}

#[cfg(test)]
mod tests {
use std::io;
Expand Down Expand Up @@ -108,5 +121,11 @@ mod tests {
InlineErrorChain::new(&err).to_string(),
"error b: error a: test error"
);

// Confirm AsInlineErrorChain works as intended
assert_eq!(
err.as_inline_error_chain().to_string(),
"error b: error a: test error"
);
}
}
16 changes: 16 additions & 0 deletions src/nested_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ impl SerdeValue for ArrayErrorChain<'_> {
}
}

/// A trait for conveniently constructing [`ArrayErrorChain`]s from [`Error`]s.
pub trait AsArrayErrorChain: Error {
/// A convenient wrapper around [`ArrayErrorChain::new`].
fn as_array_error_chain<'a>(&'a self) -> ArrayErrorChain<'a>;
}

/// Implementation for all [`Error`] types
impl<E: Error> AsArrayErrorChain for E {
fn as_array_error_chain<'a>(&'a self) -> ArrayErrorChain<'a> {
ArrayErrorChain::new(self)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -277,6 +290,9 @@ mod tests {
let chain = ArrayErrorChain::new(&err);
assert_eq!(chain.to_string(), "test error");

// Check AsArrayErrorChain implementation
assert_eq!(err.as_array_error_chain().to_string(), "test error");

let mut out = StringSerializer::default();
chain.serialize_fallback("unused", &mut out).unwrap();
assert_eq!(out.0, "test error");
Expand Down