Skip to content

Commit

Permalink
Don't log failed forwards as sender errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Dec 9, 2024
1 parent 56038a8 commit cbf24cd
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions wallets/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { withTimeout } from '@/lib/time'
export function useWalletPayment () {
const wallets = useSendWallets()
const sendPayment = useSendPayment()
const loggerFactory = useWalletLoggerFactory()
const invoiceHelper = useInvoice()

return useCallback(async (invoice, { waitFor, updateOnFallback }) => {
Expand All @@ -27,9 +28,12 @@ export function useWalletPayment () {

for (let i = 0; i < wallets.length; i++) {
const wallet = wallets[i]
const logger = loggerFactory(wallet)

const { bolt11 } = latestInvoice
const controller = invoiceController(latestInvoice, invoiceHelper.isInvoice)

const walletPromise = sendPayment(wallet, latestInvoice)
const walletPromise = sendPayment(wallet, logger, latestInvoice)
const pollPromise = controller.wait(waitFor)

try {
Expand All @@ -41,10 +45,12 @@ export function useWalletPayment () {
})
} catch (err) {
let paymentError = err
const message = `payment failed: ${paymentError.reason ?? paymentError.message}`

if (!(paymentError instanceof WalletError)) {
// payment failed for some reason unrelated to wallets (ie invoice expired or was canceled).
// bail out of attempting wallets.
logger.error(message, { bolt11 })
throw paymentError
}

Expand All @@ -64,7 +70,10 @@ export function useWalletPayment () {

if (paymentError instanceof WalletReceiverError) {
// if payment failed because of the receiver, use the same wallet again.
logger.info('failed to forward payment to receiver, retrying with new invoice', { bolt11 })
i -= 1
} else if (paymentError instanceof WalletPaymentError) {
logger.error(message, { bolt11 })
}

if (paymentError instanceof WalletPaymentError) {
Expand Down Expand Up @@ -130,11 +139,7 @@ function invoiceController (inv, isInvoice) {
}

function useSendPayment () {
const factory = useWalletLoggerFactory()

return useCallback(async (wallet, invoice) => {
const logger = factory(wallet)

return useCallback(async (wallet, logger, invoice) => {
if (!wallet.config.enabled) {
throw new WalletNotEnabledError(wallet.def.name)
}
Expand All @@ -150,10 +155,9 @@ function useSendPayment () {
const preimage = await wallet.def.sendPayment(bolt11, wallet.config, { logger })
logger.ok(`↗ payment sent: ${formatSats(satsRequested)}`, { bolt11, preimage })
} catch (err) {
// TODO: avoid logging confusing payment error if receiver failed and we canceled the invoice
// we don't want to log the error here since we want to handle receiver errors separately
const message = err.message || err.toString?.()
logger.error(`payment failed: ${message}`, { bolt11 })
throw new WalletSenderError(wallet.def.name, invoice, message)
}
}, [factory])
}, [])
}

0 comments on commit cbf24cd

Please sign in to comment.