@@ -94,28 +94,12 @@ impl fmt::Display for EventName {
94
94
}
95
95
}
96
96
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
-
109
97
pub fn deserialize_payload < T : serde:: de:: DeserializeOwned > ( v : & str ) -> anyhow:: Result < T > {
110
98
let mut deserializer = serde_json:: Deserializer :: from_str ( & v) ;
111
99
let res: Result < T , _ > = serde_path_to_error:: deserialize ( & mut deserializer) ;
112
100
match res {
113
101
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) ) ,
119
103
}
120
104
}
121
105
@@ -176,7 +160,7 @@ pub async fn webhook(
176
160
Ok ( true ) => ( "processed request" , ) . into_response ( ) ,
177
161
Ok ( false ) => ( "ignored request" , ) . into_response ( ) ,
178
162
Err ( err) => {
179
- tracing:: error!( "failed to process payload: { :?}" , err ) ;
163
+ tracing:: error!( "{err :?}" ) ;
180
164
let body = format ! ( "request failed: {:?}" , err) ;
181
165
( StatusCode :: INTERNAL_SERVER_ERROR , body) . into_response ( )
182
166
}
@@ -187,12 +171,11 @@ async fn process_payload(
187
171
event : EventName ,
188
172
payload : & str ,
189
173
ctx : & crate :: handlers:: Context ,
190
- ) -> Result < bool , WebhookError > {
174
+ ) -> anyhow :: Result < bool > {
191
175
let event = match event {
192
176
EventName :: PullRequestReview => {
193
177
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" ) ?;
196
179
197
180
log:: info!( "handling pull request review comment {:?}" , payload) ;
198
181
payload. pull_request . pull_request = Some ( PullRequestDetails :: new ( ) ) ;
@@ -213,8 +196,7 @@ async fn process_payload(
213
196
}
214
197
EventName :: PullRequestReviewComment => {
215
198
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" ) ?;
218
200
219
201
payload. issue . pull_request = Some ( PullRequestDetails :: new ( ) ) ;
220
202
@@ -232,17 +214,15 @@ async fn process_payload(
232
214
}
233
215
EventName :: IssueComment => {
234
216
let payload = deserialize_payload :: < IssueCommentEvent > ( & payload)
235
- . context ( "IssueCommentEvent failed to deserialize" )
236
- . map_err ( anyhow:: Error :: from) ?;
217
+ . context ( "failed to deserialize IssueCommentEvent" ) ?;
237
218
238
219
log:: info!( "handling issue comment {:?}" , payload) ;
239
220
240
221
Event :: IssueComment ( payload)
241
222
}
242
223
EventName :: Issue | EventName :: PullRequest => {
243
224
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" ) ?;
246
226
247
227
if matches ! ( event, EventName :: PullRequest ) {
248
228
payload. issue . pull_request = Some ( PullRequestDetails :: new ( ) ) ;
@@ -254,17 +234,15 @@ async fn process_payload(
254
234
}
255
235
EventName :: Push => {
256
236
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" ) ?;
259
238
260
239
log:: info!( "handling push event {:?}" , payload) ;
261
240
262
241
Event :: Push ( payload)
263
242
}
264
243
EventName :: Create => {
265
244
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" ) ?;
268
246
269
247
log:: info!( "handling create event {:?}" , payload) ;
270
248
@@ -301,9 +279,7 @@ async fn process_payload(
301
279
}
302
280
}
303
281
if other_error {
304
- Err ( WebhookError ( anyhow:: anyhow!(
305
- "handling failed, error logged" ,
306
- ) ) )
282
+ Err ( anyhow:: anyhow!( "handling failed, error logged" ) )
307
283
} else {
308
284
Ok ( true )
309
285
}
0 commit comments