better error handling
This commit is contained in:
parent
0734cb6cb9
commit
4e24f81f8a
2 changed files with 48 additions and 39 deletions
|
|
@ -1,4 +1,5 @@
|
|||
const Key = require('./key')
|
||||
|
||||
/**
|
||||
* @param {string} pub
|
||||
* @param {import('./SimpleGUN').GUNNode} gun
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ let currentOrderAddr = ''
|
|||
* @returns {(order: ListenerData, orderID: string) => void}
|
||||
*/
|
||||
const listenerForAddr = (addr, user, SEA) => async (order, orderID) => {
|
||||
try {
|
||||
if (addr !== currentOrderAddr) {
|
||||
return
|
||||
}
|
||||
|
|
@ -33,6 +34,12 @@ const listenerForAddr = (addr, user, SEA) => async (order, orderID) => {
|
|||
throw new Error(`Expected an order instead got: ${JSON.stringify(order)}`)
|
||||
}
|
||||
|
||||
const orderToResponse = user.get(Key.ORDER_TO_RESPONSE)
|
||||
|
||||
if (await orderToResponse.get(orderID).then()) {
|
||||
return
|
||||
}
|
||||
|
||||
const senderEpub = await Utils.pubToEpub(order.from)
|
||||
const secret = await SEA.secret(senderEpub, user._.sea)
|
||||
|
||||
|
|
@ -40,9 +47,9 @@ const listenerForAddr = (addr, user, SEA) => async (order, orderID) => {
|
|||
const memo = await SEA.decrypt(order.memo, secret)
|
||||
|
||||
/**
|
||||
* @type {[any , string]}
|
||||
* @type {string}
|
||||
*/
|
||||
const [err, invoice] = await new Promise(resolve => {
|
||||
const invoice = await new Promise((resolve, rej) => {
|
||||
const {
|
||||
services: { lightning }
|
||||
} = LightningServices
|
||||
|
|
@ -57,25 +64,26 @@ const listenerForAddr = (addr, user, SEA) => async (order, orderID) => {
|
|||
/** @type {any} */ error,
|
||||
/** @type {{ payment_request: string }} */ response
|
||||
) => {
|
||||
resolve([error, response.payment_request])
|
||||
if (error) {
|
||||
rej(error)
|
||||
} else {
|
||||
resolve(response.payment_request)
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
if (err) {
|
||||
console.log(new Error(err))
|
||||
}
|
||||
|
||||
const encInvoice = await SEA.encrypt(invoice, secret)
|
||||
|
||||
user
|
||||
.get(Key.ORDER_TO_RESPONSE)
|
||||
.get(orderID)
|
||||
.put(encInvoice, ack => {
|
||||
orderToResponse.get(orderID).put(encInvoice, ack => {
|
||||
if (ack.err) {
|
||||
console.error(`error saving order response: ${ack.err}`)
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('error inside onOrders:')
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue