routing: add exitWithErr to handle error logging

This commit is contained in:
yyforyongyu
2022-06-27 05:07:21 +08:00
committed by Olaoluwa Osuntokun
parent e5840f6216
commit c412ab5ccb

View File

@@ -69,7 +69,6 @@ func (p *paymentLifecycle) resumePayment() ([32]byte, *route.Route, error) {
// If we had any existing attempts outstanding, we'll start by spinning
// up goroutines that'll collect their results and deliver them to the
// lifecycle loop below.
// Fetch the latest payment from db.
payment, err := p.router.cfg.Control.FetchPayment(p.identifier)
if err != nil {
return [32]byte{}, nil, err
@@ -84,6 +83,13 @@ func (p *paymentLifecycle) resumePayment() ([32]byte, *route.Route, error) {
shardHandler.collectResultAsync(&a.HTLCAttemptInfo)
}
// exitWithErr is a helper closure that logs and returns an error.
exitWithErr := func(err error) ([32]byte, *route.Route, error) {
log.Errorf("Payment %v with status=%v failed: %v",
p.identifier, payment.Status, err)
return [32]byte{}, nil, err
}
// We'll continue until either our payment succeeds, or we encounter a
// critical error during path finding.
lifecycle:
@@ -91,7 +97,7 @@ lifecycle:
// Start by quickly checking if there are any outcomes already
// available to handle before we reevaluate our state.
if err := shardHandler.checkShards(); err != nil {
return [32]byte{}, nil, err
return exitWithErr(err)
}
// We update the payment state on every iteration. Since the
@@ -103,7 +109,7 @@ lifecycle:
// Fetch the latest payment from db.
payment, err := p.router.cfg.Control.FetchPayment(p.identifier)
if err != nil {
return [32]byte{}, nil, err
return exitWithErr(err)
}
ps := payment.State
@@ -140,7 +146,7 @@ lifecycle:
}
// Payment failed.
return [32]byte{}, nil, *payment.FailureReason
return exitWithErr(*payment.FailureReason)
}
// If we either reached a terminal error condition (but had
@@ -148,7 +154,7 @@ lifecycle:
// we'll wait for a shard outcome.
wait, err := payment.NeedWaitAttempts()
if err != nil {
return [32]byte{}, nil, err
return exitWithErr(err)
}
if wait {
@@ -156,7 +162,7 @@ lifecycle:
// outcome to be available before re-evaluating our
// state.
if err := shardHandler.waitForShard(); err != nil {
return [32]byte{}, nil, err
return exitWithErr(err)
}
continue lifecycle
}
@@ -179,13 +185,13 @@ lifecycle:
p.identifier, channeldb.FailureReasonTimeout,
)
if saveErr != nil {
return [32]byte{}, nil, saveErr
return exitWithErr(saveErr)
}
continue lifecycle
case <-p.router.quit:
return [32]byte{}, nil, ErrRouterShuttingDown
return exitWithErr(ErrRouterShuttingDown)
// Fall through if we haven't hit our time limit.
default:
@@ -203,7 +209,7 @@ lifecycle:
routeErr, ok := err.(noRouteError)
if !ok {
return [32]byte{}, nil, err
return exitWithErr(err)
}
// There is no route to try, and we have no active
@@ -219,7 +225,7 @@ lifecycle:
p.identifier, failureCode,
)
if saveErr != nil {
return [32]byte{}, nil, saveErr
return exitWithErr(saveErr)
}
continue lifecycle
@@ -228,7 +234,7 @@ lifecycle:
// We still have active shards, we'll wait for an
// outcome to be available before retrying.
if err := shardHandler.waitForShard(); err != nil {
return [32]byte{}, nil, err
return exitWithErr(err)
}
continue lifecycle
}
@@ -253,7 +259,7 @@ lifecycle:
continue lifecycle
case err != nil:
return [32]byte{}, nil, err
return exitWithErr(err)
}
// If we encountered a non-critical error when launching the
@@ -270,7 +276,7 @@ lifecycle:
attempt, outcome.err,
)
if err != nil {
return [32]byte{}, nil, err
return exitWithErr(err)
}
// Error was handled successfully, continue to make a