Skip to content

Commit 247d419

Browse files
Path to deserialization error
1 parent 81a9187 commit 247d419

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ serde_derive = "1.0.59"
2222
serde_json = "1.0"
2323
toml = "0.5.3"
2424
url = "2.1.0"
25+
serde_path_to_error = "0.1.5"
2526

2627
[dependencies.chrono]
2728
features = ["serde"]

src/error.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,51 @@ pub enum DashError {
1212
Reqwest(reqwest::Error),
1313
Io(io::Error),
1414
Serde(serde_json::error::Error),
15+
SerdePath(serde_path_to_error::Error<serde_json::error::Error>),
1516
R2d2(diesel::r2d2::PoolError),
1617
DieselError(diesel::result::Error),
1718
Template(handlebars::RenderError),
1819
Misc(Option<String>),
1920
}
2021

2122
impl From<handlebars::RenderError> for DashError {
22-
fn from(e: handlebars::RenderError) -> Self { DashError::Template(e) }
23+
fn from(e: handlebars::RenderError) -> Self {
24+
DashError::Template(e)
25+
}
2326
}
2427

2528
impl From<reqwest::Error> for DashError {
26-
fn from(e: reqwest::Error) -> Self { DashError::Reqwest(e) }
29+
fn from(e: reqwest::Error) -> Self {
30+
DashError::Reqwest(e)
31+
}
2732
}
2833

2934
impl From<io::Error> for DashError {
30-
fn from(e: io::Error) -> Self { DashError::Io(e) }
35+
fn from(e: io::Error) -> Self {
36+
DashError::Io(e)
37+
}
3138
}
3239

3340
impl From<serde_json::error::Error> for DashError {
34-
fn from(e: serde_json::error::Error) -> Self { DashError::Serde(e) }
41+
fn from(e: serde_json::error::Error) -> Self {
42+
DashError::Serde(e)
43+
}
44+
}
45+
46+
impl From<serde_path_to_error::Error<serde_json::error::Error>> for DashError {
47+
fn from(e: serde_path_to_error::Error<serde_json::error::Error>) -> Self {
48+
DashError::SerdePath(e)
49+
}
3550
}
3651

3752
impl From<diesel::r2d2::PoolError> for DashError {
38-
fn from(e: diesel::r2d2::PoolError) -> Self { DashError::R2d2(e) }
53+
fn from(e: diesel::r2d2::PoolError) -> Self {
54+
DashError::R2d2(e)
55+
}
3956
}
4057

4158
impl From<diesel::result::Error> for DashError {
42-
fn from(e: diesel::result::Error) -> Self { DashError::DieselError(e) }
59+
fn from(e: diesel::result::Error) -> Self {
60+
DashError::DieselError(e)
61+
}
4362
}

src/github/webhooks.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,19 @@ fn authenticate(secret: &str, payload: &str, signature: &str) -> Result<(), ()>
110110
}
111111
}
112112

113+
macro_rules! from_json {
114+
($b:expr) => {{
115+
let body = $b;
116+
let jd = &mut serde_json::Deserializer::from_str(&body);
117+
serde_path_to_error::deserialize(jd)
118+
}};
119+
}
120+
113121
fn parse_event(event_name: &str, body: &str) -> DashResult<Payload> {
114122
match event_name {
115-
"issue_comment" => Ok(Payload::IssueComment(serde_json::from_str(body)?)),
116-
"issues" => Ok(Payload::Issues(serde_json::from_str(body)?)),
117-
"pull_request" => Ok(Payload::PullRequest(serde_json::from_str(body)?)),
123+
"issue_comment" => Ok(Payload::IssueComment(from_json!(body)?)),
124+
"issues" => Ok(Payload::Issues(from_json!(body)?)),
125+
"pull_request" => Ok(Payload::PullRequest(from_json!(body)?)),
118126

119127
"commit_comment"
120128
| "create"

0 commit comments

Comments
 (0)