Skip to content

Commit

Permalink
feat: Improved embedded sources api
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Mar 24, 2018
1 parent 0840187 commit d100bf1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/protocol/v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ pub struct TemplateInfo {
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
pub struct EmbeddedSources {
/// The sources of the lines leading up to the current line.
#[serde(rename = "pre_context", skip_serializing_if = "Option::is_none")]
pub pre_lines: Option<Vec<String>>,
#[serde(rename = "pre_context", skip_serializing_if = "Vec::is_empty")]
pub pre_lines: Vec<String>,
/// The current line as source.
#[serde(rename = "context_line", skip_serializing_if = "Option::is_none")]
pub current_line: Option<String>,
/// The sources of the lines after the current line.
#[serde(rename = "post_context", skip_serializing_if = "Option::is_none")]
pub post_lines: Option<Vec<String>>,
#[serde(rename = "post_context", skip_serializing_if = "Vec::is_empty")]
pub post_lines: Vec<String>,
}

/// Represents a stacktrace.
Expand Down
47 changes: 47 additions & 0 deletions tests/test_protocol_v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,50 @@ fn test_minimal_exception_stacktrace() {
\"lineno\":1}]}}]}}"
);
}

#[test]
fn test_slightly_larger_exception_stacktrace() {
let event: v7::Event = v7::Event {
exceptions: vec![v7::Exception {
ty: "DivisionByZero".into(),
value: Some("integer division or modulo by zero".into()),
stacktrace: Some(v7::Stacktrace {
frames: vec![
v7::Frame {
function: Some("main".into()),
location: v7::FileLocation {
filename: Some("hello.py".into()),
line: Some(7),
column: Some(42),
..Default::default()
},
source: v7::EmbeddedSources {
pre_lines: vec!["foo".into(), "bar".into()],
current_line: Some("hey hey hey".into()),
post_lines: vec!["foo".into(), "bar".into()],
},
in_app: Some(true),
vars: {
let mut m = HashMap::new();
m.insert("var".into(), "value".into());
m
},
..Default::default()
},
],
..Default::default()
}),
}],
..Default::default()
};

assert_eq!(
serde_json::to_string(&event).unwrap(),
"{\"exception\":{\"values\":[{\"type\":\"DivisionByZero\",\"value\":\
\"integer division or modulo by zero\",\"stacktrace\":{\"frames\":\
[{\"function\":\"main\",\"filename\":\"hello.py\",\"lineno\":7,\
\"colno\":42,\"pre_context\":[\"foo\",\"bar\"],\"context_line\":\
\"hey hey hey\",\"post_context\":[\"foo\",\"bar\"],\"in_app\":true,\
\"vars\":{\"var\":\"value\"}}]}}]}}"
);
}

0 comments on commit d100bf1

Please sign in to comment.