Skip to content

Allow sending arbitrary custom metadata #6 #7

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
52 changes: 41 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{thread, fmt, panic, error};
use std::borrow::ToOwned;
use std::sync::Arc;
use backtrace::Backtrace;
use serde_json::value::Value as JsonValue;

/// Report an error. Any type that implements `error::Error` is accepted.
#[macro_export]
Expand Down Expand Up @@ -236,7 +237,10 @@ pub struct ReportErrorBuilder<'a> {

/// The title shown in the dashboard for this report.
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>
title: Option<String>,

/// Custom arbitrary metadata.
metadata: Option<JsonValue>
}

impl<'a> ReportErrorBuilder<'a> {
Expand Down Expand Up @@ -274,6 +278,9 @@ impl<'a> ReportErrorBuilder<'a> {
/// Set the security level of the report. `Level::ERROR` is the default value.
add_generic_field!(with_level, level, Into<Level>);

/// Set arbitrary custom metadata to be attached to the error report.
add_field!(with_metadata, metadata, JsonValue);

/// Set the title to show in the dashboard for this report.
add_generic_field!(with_title, title, Into<String>);

Expand Down Expand Up @@ -307,7 +314,10 @@ impl<'a> ToString for ReportErrorBuilder<'a> {
.unwrap_or(Level::ERROR)
.to_string(),
"language": "rust",
"title": self.title
"title": self.title,
"custom": self.metadata
.to_owned()
.unwrap_or_else(|| json!({}))
}
}).to_string()
}
Expand All @@ -321,13 +331,19 @@ pub struct ReportMessageBuilder<'a> {
message: &'a str,

/// The severity level of the error. `Level::ERROR` is the default value.
level: Option<Level>
level: Option<Level>,

/// Custom arbitrary metadata.
metadata: Option<JsonValue>
}

impl<'a> ReportMessageBuilder<'a> {
/// Set the security level of the report. `Level::ERROR` is the default value
add_generic_field!(with_level, level, Into<Level>);

/// Set arbitrary custom metadata to be attached to the error report.
add_field!(with_metadata, metadata, JsonValue);

/// Send the message to Rollbar.
pub fn send(&mut self) -> thread::JoinHandle<Option<ResponseStatus>> {
let client = self.report_builder.client;
Expand Down Expand Up @@ -358,7 +374,10 @@ impl<'a> ToString for ReportMessageBuilder<'a> {
"level": self.level
.to_owned()
.unwrap_or(Level::INFO)
.to_string()
.to_string(),
"custom": self.metadata
.to_owned()
.unwrap_or_else(|| json!({}))
}
}).to_string()
}
Expand Down Expand Up @@ -392,7 +411,8 @@ impl<'a> ReportBuilder<'a> {
report_builder: self,
trace: trace,
level: None,
title: Some(message.to_owned())
title: Some(message.to_owned()),
metadata: None
}
}

Expand All @@ -406,7 +426,8 @@ impl<'a> ReportBuilder<'a> {
report_builder: self,
trace: trace,
level: None,
title: Some(format!("{}", error))
title: Some(format!("{}", error)),
metadata: None
}
}

Expand All @@ -422,7 +443,8 @@ impl<'a> ReportBuilder<'a> {
report_builder: self,
trace: trace,
level: None,
title: Some(message)
title: Some(message),
metadata: None
}
}

Expand All @@ -431,7 +453,8 @@ impl<'a> ReportBuilder<'a> {
ReportMessageBuilder {
report_builder: self,
message: message,
level: None
level: None,
metadata: None
}
}

Expand Down Expand Up @@ -647,6 +670,7 @@ mod tests {
}
}
},
"custom": {},
"level": "info",
"language": "rust",
"title": "attempt to divide by zero"
Expand All @@ -670,9 +694,8 @@ mod tests {
.get_mut(0).unwrap()
.get_mut("lineno").unwrap() = line_number.to_owned();


normalize_frames!(payload, expected_payload, 1);
assert_eq!(expected_payload.to_string(), payload.to_string());
assert_eq!(expected_payload, payload);
}

#[test]
Expand Down Expand Up @@ -714,6 +737,7 @@ mod tests {
}
}
},
"custom": {},
"level": "warning",
"language": "rust",
"title": "w"
Expand All @@ -722,7 +746,7 @@ mod tests {

let mut payload: Value = serde_json::from_str(&*payload).unwrap();
normalize_frames!(payload, expected_payload, 2);
assert_eq!(expected_payload.to_string(), payload.to_string());
assert_eq!(expected_payload, payload);
}
}
}
Expand All @@ -734,6 +758,9 @@ mod tests {
let payload = client.build_report()
.from_message("hai")
.with_level("warning")
.with_metadata(json!({
"foo": "bar"
}))
.to_string();

let expected_payload = json!({
Expand All @@ -745,6 +772,9 @@ mod tests {
"body": "hai"
}
},
"custom": {
"foo": "bar"
},
"level": "warning"
}
}).to_string();
Expand Down