Skip to content

Commit 176cfea

Browse files
committed
Improve logging in event handler
While we cannot get rid of the panics here, we can make sure the errors are logged before we (explicitly) panic.
1 parent 10cc6e8 commit 176cfea

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

src/event.rs

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,12 @@ where
311311
&temporary_channel_id,
312312
&counterparty_node_id,
313313
)
314-
.expect(
315-
"Failed to force close channel after funding generation failed",
316-
);
314+
.unwrap_or_else(|e| {
315+
log_error!(self.logger, "Failed to force close channel after funding generation failed: {:?}", e);
316+
panic!(
317+
"Failed to force close channel after funding generation failed"
318+
);
319+
});
317320
}
318321
}
319322
}
@@ -341,7 +344,10 @@ where
341344
status: Some(PaymentStatus::Failed),
342345
..PaymentDetailsUpdate::new(payment_hash)
343346
};
344-
self.payment_store.update(&update).expect("Failed to access payment store");
347+
self.payment_store.update(&update).unwrap_or_else(|e| {
348+
log_error!(self.logger, "Failed to access payment store: {}", e);
349+
panic!("Failed to access payment store");
350+
});
345351
return;
346352
}
347353
}
@@ -379,7 +385,10 @@ where
379385
status: Some(PaymentStatus::Failed),
380386
..PaymentDetailsUpdate::new(payment_hash)
381387
};
382-
self.payment_store.update(&update).expect("Failed to access payment store");
388+
self.payment_store.update(&update).unwrap_or_else(|e| {
389+
log_error!(self.logger, "Failed to access payment store: {}", e);
390+
panic!("Failed to access payment store");
391+
});
383392
}
384393
}
385394
LdkEvent::PaymentClaimed {
@@ -459,15 +468,19 @@ where
459468

460469
self.event_queue
461470
.add_event(Event::PaymentReceived { payment_hash, amount_msat })
462-
.expect("Failed to push to event queue");
471+
.unwrap_or_else(|e| {
472+
log_error!(self.logger, "Failed to push to event queue: {}", e);
473+
panic!("Failed to push to event queue");
474+
});
463475
}
464476
LdkEvent::PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => {
465477
if let Some(mut payment) = self.payment_store.get(&payment_hash) {
466478
payment.preimage = Some(payment_preimage);
467479
payment.status = PaymentStatus::Succeeded;
468-
self.payment_store
469-
.insert(payment.clone())
470-
.expect("Failed to access payment store");
480+
self.payment_store.insert(payment.clone()).unwrap_or_else(|e| {
481+
log_error!(self.logger, "Failed to access payment store: {}", e);
482+
panic!("Failed to access payment store");
483+
});
471484
log_info!(
472485
self.logger,
473486
"Successfully sent payment of {}msat{} from \
@@ -484,7 +497,10 @@ where
484497
}
485498
self.event_queue
486499
.add_event(Event::PaymentSuccessful { payment_hash })
487-
.expect("Failed to push to event queue");
500+
.unwrap_or_else(|e| {
501+
log_error!(self.logger, "Failed to push to event queue: {}", e);
502+
panic!("Failed to push to event queue");
503+
});
488504
}
489505
LdkEvent::PaymentFailed { payment_hash, .. } => {
490506
log_info!(
@@ -497,10 +513,16 @@ where
497513
status: Some(PaymentStatus::Failed),
498514
..PaymentDetailsUpdate::new(payment_hash)
499515
};
500-
self.payment_store.update(&update).expect("Failed to access payment store");
501-
self.event_queue
502-
.add_event(Event::PaymentFailed { payment_hash })
503-
.expect("Failed to push to event queue");
516+
self.payment_store.update(&update).unwrap_or_else(|e| {
517+
log_error!(self.logger, "Failed to access payment store: {}", e);
518+
panic!("Failed to access payment store");
519+
});
520+
self.event_queue.add_event(Event::PaymentFailed { payment_hash }).unwrap_or_else(
521+
|e| {
522+
log_error!(self.logger, "Failed to push to event queue: {}", e);
523+
panic!("Failed to push to event queue");
524+
},
525+
);
504526
}
505527

506528
LdkEvent::PaymentPathSuccessful { .. } => {}
@@ -526,8 +548,11 @@ where
526548
}
527549
LdkEvent::SpendableOutputs { outputs } => {
528550
// TODO: We should eventually remember the outputs and supply them to the wallet's coin selection, once BDK allows us to do so.
529-
let destination_address =
530-
self.wallet.get_new_address().expect("Failed to get destination address");
551+
let destination_address = self.wallet.get_new_address().unwrap_or_else(|e| {
552+
log_error!(self.logger, "Failed to get destination address: {}", e);
553+
panic!("Failed to get destination address");
554+
});
555+
531556
let output_descriptors = &outputs.iter().collect::<Vec<_>>();
532557
let tx_feerate =
533558
self.wallet.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
@@ -676,7 +701,10 @@ where
676701
counterparty_node_id,
677702
funding_txo,
678703
})
679-
.expect("Failed to push to event queue");
704+
.unwrap_or_else(|e| {
705+
log_error!(self.logger, "Failed to push to event queue: {}", e);
706+
panic!("Failed to push to event queue");
707+
});
680708
}
681709
LdkEvent::ChannelReady {
682710
channel_id, user_channel_id, counterparty_node_id, ..
@@ -692,7 +720,10 @@ where
692720
channel_id: ChannelId(channel_id),
693721
user_channel_id: UserChannelId(user_channel_id),
694722
})
695-
.expect("Failed to push to event queue");
723+
.unwrap_or_else(|e| {
724+
log_error!(self.logger, "Failed to push to event queue: {}", e);
725+
panic!("Failed to push to event queue");
726+
});
696727
}
697728
LdkEvent::ChannelClosed { channel_id, reason, user_channel_id } => {
698729
log_info!(
@@ -706,7 +737,10 @@ where
706737
channel_id: ChannelId(channel_id),
707738
user_channel_id: UserChannelId(user_channel_id),
708739
})
709-
.expect("Failed to push to event queue");
740+
.unwrap_or_else(|e| {
741+
log_error!(self.logger, "Failed to push to event queue: {}", e);
742+
panic!("Failed to push to event queue");
743+
});
710744
}
711745
LdkEvent::DiscardFunding { .. } => {}
712746
LdkEvent::HTLCIntercepted { .. } => {}

0 commit comments

Comments
 (0)