Skip to content

Commit c52b3d9

Browse files
committed
Cleanup, simplify and improve our GitHub webhook error handling
1 parent 142d2f2 commit c52b3d9

File tree

1 file changed

+10
-34
lines changed

1 file changed

+10
-34
lines changed

src/github/webhook.rs

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,12 @@ impl fmt::Display for EventName {
9494
}
9595
}
9696

97-
#[derive(Debug)]
98-
pub struct WebhookError(
99-
#[allow(dead_code)] // Used in debug display
100-
anyhow::Error,
101-
);
102-
103-
impl From<anyhow::Error> for WebhookError {
104-
fn from(e: anyhow::Error) -> WebhookError {
105-
WebhookError(e)
106-
}
107-
}
108-
10997
pub fn deserialize_payload<T: serde::de::DeserializeOwned>(v: &str) -> anyhow::Result<T> {
11098
let mut deserializer = serde_json::Deserializer::from_str(&v);
11199
let res: Result<T, _> = serde_path_to_error::deserialize(&mut deserializer);
112100
match res {
113101
Ok(r) => Ok(r),
114-
Err(e) => {
115-
tracing::error!("failed to deserialize webhook payload: {v}");
116-
let ctx = format!("at {:?}", e.path());
117-
Err(e.into_inner()).context(ctx)
118-
}
102+
Err(e) => Err(anyhow::anyhow!("webhook payload: {v}").context(e)),
119103
}
120104
}
121105

@@ -176,7 +160,7 @@ pub async fn webhook(
176160
Ok(true) => ("processed request",).into_response(),
177161
Ok(false) => ("ignored request",).into_response(),
178162
Err(err) => {
179-
tracing::error!("failed to process payload: {:?}", err);
163+
tracing::error!("{err:?}");
180164
let body = format!("request failed: {:?}", err);
181165
(StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
182166
}
@@ -187,12 +171,11 @@ async fn process_payload(
187171
event: EventName,
188172
payload: &str,
189173
ctx: &crate::handlers::Context,
190-
) -> Result<bool, WebhookError> {
174+
) -> anyhow::Result<bool> {
191175
let event = match event {
192176
EventName::PullRequestReview => {
193177
let mut payload = deserialize_payload::<PullRequestReviewEvent>(payload)
194-
.context("PullRequestReview failed to deserialize")
195-
.map_err(anyhow::Error::from)?;
178+
.context("failed to deserialize to PullRequestReviewEvent")?;
196179

197180
log::info!("handling pull request review comment {:?}", payload);
198181
payload.pull_request.pull_request = Some(PullRequestDetails::new());
@@ -213,8 +196,7 @@ async fn process_payload(
213196
}
214197
EventName::PullRequestReviewComment => {
215198
let mut payload = deserialize_payload::<PullRequestReviewComment>(&payload)
216-
.context("PullRequestReview(Comment) failed to deserialize")
217-
.map_err(anyhow::Error::from)?;
199+
.context("failed to deserialize to PullRequestReviewComment")?;
218200

219201
payload.issue.pull_request = Some(PullRequestDetails::new());
220202

@@ -232,17 +214,15 @@ async fn process_payload(
232214
}
233215
EventName::IssueComment => {
234216
let payload = deserialize_payload::<IssueCommentEvent>(&payload)
235-
.context("IssueCommentEvent failed to deserialize")
236-
.map_err(anyhow::Error::from)?;
217+
.context("failed to deserialize IssueCommentEvent")?;
237218

238219
log::info!("handling issue comment {:?}", payload);
239220

240221
Event::IssueComment(payload)
241222
}
242223
EventName::Issue | EventName::PullRequest => {
243224
let mut payload = deserialize_payload::<IssuesEvent>(&payload)
244-
.context(format!("{:?} failed to deserialize", event))
245-
.map_err(anyhow::Error::from)?;
225+
.context("failed to deserialize IssuesEvent")?;
246226

247227
if matches!(event, EventName::PullRequest) {
248228
payload.issue.pull_request = Some(PullRequestDetails::new());
@@ -254,17 +234,15 @@ async fn process_payload(
254234
}
255235
EventName::Push => {
256236
let payload = deserialize_payload::<PushEvent>(&payload)
257-
.with_context(|| format!("{:?} failed to deserialize", event))
258-
.map_err(anyhow::Error::from)?;
237+
.context("failed to deserialize to PushEvent")?;
259238

260239
log::info!("handling push event {:?}", payload);
261240

262241
Event::Push(payload)
263242
}
264243
EventName::Create => {
265244
let payload = deserialize_payload::<CreateEvent>(&payload)
266-
.with_context(|| format!("{:?} failed to deserialize", event))
267-
.map_err(anyhow::Error::from)?;
245+
.context("failed to deserialize to CreateEvent")?;
268246

269247
log::info!("handling create event {:?}", payload);
270248

@@ -301,9 +279,7 @@ async fn process_payload(
301279
}
302280
}
303281
if other_error {
304-
Err(WebhookError(anyhow::anyhow!(
305-
"handling failed, error logged",
306-
)))
282+
Err(anyhow::anyhow!("handling failed, error logged"))
307283
} else {
308284
Ok(true)
309285
}

0 commit comments

Comments
 (0)