@@ -413,7 +413,7 @@ export default class WebhookCannon {
413
413
responseBody = await resp . text ( ) ;
414
414
statusCode = resp . status ;
415
415
416
- if ( resp . status >= 200 && resp . status < 300 ) {
416
+ if ( isSuccess ( resp ) ) {
417
417
// 2xx requests are cool. all is good
418
418
logger . info ( `webhook ${ webhook . id } fired successfully` ) ;
419
419
return true ;
@@ -449,7 +449,7 @@ export default class WebhookCannon {
449
449
) ;
450
450
} catch ( e ) {
451
451
console . log (
452
- `Unable to store response of webhook ${ webhook . id } url: ${ webhook . url } `
452
+ `Unable to store response of webhook ${ webhook . id } url: ${ webhook . url } error: ${ e } `
453
453
) ;
454
454
}
455
455
await this . storeTriggerStatus (
@@ -585,6 +585,10 @@ export default class WebhookCannon {
585
585
}
586
586
}
587
587
588
+ function isSuccess ( resp : Response ) {
589
+ return resp ?. status >= 200 && resp ?. status < 300 ;
590
+ }
591
+
588
592
async function storeResponse (
589
593
webhook : DBWebhook ,
590
594
eventId : string ,
@@ -596,9 +600,12 @@ async function storeResponse(
596
600
params
597
601
) : Promise < WebhookLog > {
598
602
const hrDuration = process . hrtime ( startTime ) ;
599
- const encodedResponseBody = Buffer . from (
600
- responseBody . substring ( 0 , 1024 )
601
- ) . toString ( "base64" ) ;
603
+ let encodedResponseBody = "" ;
604
+ if ( responseBody ) {
605
+ encodedResponseBody = Buffer . from ( responseBody . substring ( 0 , 1024 ) ) . toString (
606
+ "base64"
607
+ ) ;
608
+ }
602
609
603
610
const webhookLog = {
604
611
id : uuid ( ) ,
@@ -608,11 +615,12 @@ async function storeResponse(
608
615
userId : webhook . userId ,
609
616
createdAt : Date . now ( ) ,
610
617
duration : hrDuration [ 0 ] + hrDuration [ 1 ] / 1e9 ,
618
+ success : isSuccess ( resp ) ,
611
619
response : {
612
620
body : encodedResponseBody ,
613
- redirected : resp . redirected ,
614
- status : resp . status ,
615
- statusText : resp . statusText ,
621
+ redirected : resp ? .redirected ,
622
+ status : resp ? .status ,
623
+ statusText : resp ? .statusText ,
616
624
} ,
617
625
request : {
618
626
url : webhook . url ,
@@ -694,7 +702,9 @@ export async function storeTriggerStatus(
694
702
) : Promise < void > {
695
703
try {
696
704
let status : DBWebhook [ "status" ] = { lastTriggeredAt : triggerTime } ;
697
- let encodedResponseBody = Buffer . from ( responseBody ) . toString ( "base64" ) ;
705
+ let encodedResponseBody = responseBody
706
+ ? Buffer . from ( responseBody ) . toString ( "base64" )
707
+ : "" ;
698
708
if ( statusCode >= 300 || ! statusCode ) {
699
709
status = {
700
710
...status ,
0 commit comments