Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an error field to Record #378

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/lib.rs
Expand Up @@ -728,6 +728,8 @@ pub struct Record<'a> {
module_path: Option<MaybeStaticStr<'a>>,
file: Option<MaybeStaticStr<'a>>,
line: Option<u32>,
#[cfg(feature = "std")]
error: Option<&'a (dyn std::error::Error + 'static)>,
#[cfg(feature = "kv_unstable")]
key_values: KeyValues<'a>,
}
Expand Down Expand Up @@ -816,6 +818,13 @@ impl<'a> Record<'a> {
self.line
}

/// The error associated with the message.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should mention it requires the std feature.

#[cfg(feature = "std")]
#[inline]
pub fn error(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.error
}

/// The structued key-value pairs associated with the message.
#[cfg(feature = "kv_unstable")]
#[inline]
Expand All @@ -837,6 +846,8 @@ impl<'a> Record<'a> {
module_path: self.module_path,
file: self.file,
line: self.line,
#[cfg(feature = "std")]
error: self.error,
key_values: self.key_values.clone(),
},
}
Expand Down Expand Up @@ -910,6 +921,8 @@ impl<'a> RecordBuilder<'a> {
module_path: None,
file: None,
line: None,
#[cfg(feature = "std")]
error: None,
#[cfg(feature = "kv_unstable")]
key_values: KeyValues(&Option::None::<(kv::Key, kv::Value)>),
},
Expand Down Expand Up @@ -979,6 +992,17 @@ impl<'a> RecordBuilder<'a> {
self
}

/// Set [`error`](struct.Record.html#method.error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

#[cfg(feature = "std")]
#[inline]
pub fn error(
&mut self,
error: Option<&'a (dyn std::error::Error + 'static)>,
) -> &mut RecordBuilder<'a> {
self.record.error = error;
self
}

/// Set [`key_values`](struct.Record.html#method.key_values)
#[cfg(feature = "kv_unstable")]
#[inline]
Expand Down Expand Up @@ -1649,4 +1673,17 @@ mod tests {

assert_eq!(2, visitor.seen_pairs);
}

#[test]
#[cfg(feature = "std")]
fn test_record_error_builder() {
use super::Record;
use std::error::Error;

let err: Box<dyn Error> = "something went wrong!".into();

let record = Record::builder().error(Some(&*err)).build();

assert!(record.error().is_some());
}
}