From e9b2182cdcfc123683ae565ad1dec268bd2df5e0 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Thu, 23 May 2019 20:05:29 +0200 Subject: [PATCH] routing/payment_lifecycle: extract error handling into method --- routing/payment_lifecycle.go | 58 ++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/routing/payment_lifecycle.go b/routing/payment_lifecycle.go index 9fef6337..28f106ff 100644 --- a/routing/payment_lifecycle.go +++ b/routing/payment_lifecycle.go @@ -131,18 +131,15 @@ func (p *paymentLifecycle) resumePayment() ([32]byte, *route.Route, error) { log.Errorf("Failed sending attempt %d for payment "+ "%x to switch: %v", paymentID, p.payment.PaymentHash, err) - // We must inspect the error to know whether it was - // critical or not, to decide whether we should - // continue trying. - finalOutcome := p.router.processSendError( - p.paySession, route, err, - ) - if finalOutcome { + // We must inspect the error to know whether it + // was critical or not, to decide whether we + // should continue trying. + if err := p.handleSendError(route, err); err != nil { return [32]byte{}, nil, err } - // We make another payment attempt. - p.lastError = err + // Error was handled successfully, make a new + // payment attempt. continue } @@ -206,22 +203,15 @@ func (p *paymentLifecycle) resumePayment() ([32]byte, *route.Route, error) { log.Errorf("Attempt to send payment %x failed: %v", p.payment.PaymentHash, result.Error) - finalOutcome := p.router.processSendError( - p.paySession, route, result.Error, - ) - - if finalOutcome { - log.Errorf("Payment %x failed with "+ - "final outcome: %v", - p.payment.PaymentHash, result.Error) - - // Terminal state, return the error we - // encountered. - return [32]byte{}, nil, result.Error + // We must inspect the error to know whether it was + // critical or not, to decide whether we should + // continue trying. + if err := p.handleSendError(route, result.Error); err != nil { + return [32]byte{}, nil, err } - // We make another payment attempt. - p.lastError = result.Error + // Error was handled successfully, make a new payment + // attempt. continue } @@ -235,3 +225,25 @@ func (p *paymentLifecycle) resumePayment() ([32]byte, *route.Route, error) { } } + +// handleSendError inspects the given error and determines whether we should +// make another payment attempt. +func (p *paymentLifecycle) handleSendError(rt *route.Route, + sendErr error) error { + + finalOutcome := p.router.processSendError( + p.paySession, rt, sendErr, + ) + + if finalOutcome { + log.Errorf("Payment %x failed with final outcome: %v", + p.payment.PaymentHash, sendErr) + + // Terminal state, return the error we encountered. + return sendErr + } + + // We get ready to make another payment attempt. + p.lastError = sendErr + return nil +}