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')
|
const Key = require('./key')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} pub
|
* @param {string} pub
|
||||||
* @param {import('./SimpleGUN').GUNNode} gun
|
* @param {import('./SimpleGUN').GUNNode} gun
|
||||||
|
|
|
||||||
|
|
@ -25,57 +25,65 @@ let currentOrderAddr = ''
|
||||||
* @returns {(order: ListenerData, orderID: string) => void}
|
* @returns {(order: ListenerData, orderID: string) => void}
|
||||||
*/
|
*/
|
||||||
const listenerForAddr = (addr, user, SEA) => async (order, orderID) => {
|
const listenerForAddr = (addr, user, SEA) => async (order, orderID) => {
|
||||||
if (addr !== currentOrderAddr) {
|
try {
|
||||||
return
|
if (addr !== currentOrderAddr) {
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (!Schema.isOrder(order)) {
|
if (!Schema.isOrder(order)) {
|
||||||
throw new Error(`Expected an order instead got: ${JSON.stringify(order)}`)
|
throw new Error(`Expected an order instead got: ${JSON.stringify(order)}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const senderEpub = await Utils.pubToEpub(order.from)
|
const orderToResponse = user.get(Key.ORDER_TO_RESPONSE)
|
||||||
const secret = await SEA.secret(senderEpub, user._.sea)
|
|
||||||
|
|
||||||
const amount = Number(await SEA.decrypt(order.amount, secret))
|
if (await orderToResponse.get(orderID).then()) {
|
||||||
const memo = await SEA.decrypt(order.memo, secret)
|
return
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
const senderEpub = await Utils.pubToEpub(order.from)
|
||||||
* @type {[any , string]}
|
const secret = await SEA.secret(senderEpub, user._.sea)
|
||||||
*/
|
|
||||||
const [err, invoice] = await new Promise(resolve => {
|
|
||||||
const {
|
|
||||||
services: { lightning }
|
|
||||||
} = LightningServices
|
|
||||||
|
|
||||||
lightning.addInvoice(
|
const amount = Number(await SEA.decrypt(order.amount, secret))
|
||||||
{
|
const memo = await SEA.decrypt(order.memo, secret)
|
||||||
expiry: 36000,
|
|
||||||
memo,
|
|
||||||
value: amount
|
|
||||||
},
|
|
||||||
(
|
|
||||||
/** @type {any} */ error,
|
|
||||||
/** @type {{ payment_request: string }} */ response
|
|
||||||
) => {
|
|
||||||
resolve([error, response.payment_request])
|
|
||||||
}
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
if (err) {
|
/**
|
||||||
console.log(new Error(err))
|
* @type {string}
|
||||||
}
|
*/
|
||||||
|
const invoice = await new Promise((resolve, rej) => {
|
||||||
|
const {
|
||||||
|
services: { lightning }
|
||||||
|
} = LightningServices
|
||||||
|
|
||||||
const encInvoice = await SEA.encrypt(invoice, secret)
|
lightning.addInvoice(
|
||||||
|
{
|
||||||
|
expiry: 36000,
|
||||||
|
memo,
|
||||||
|
value: amount
|
||||||
|
},
|
||||||
|
(
|
||||||
|
/** @type {any} */ error,
|
||||||
|
/** @type {{ payment_request: string }} */ response
|
||||||
|
) => {
|
||||||
|
if (error) {
|
||||||
|
rej(error)
|
||||||
|
} else {
|
||||||
|
resolve(response.payment_request)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
user
|
const encInvoice = await SEA.encrypt(invoice, secret)
|
||||||
.get(Key.ORDER_TO_RESPONSE)
|
|
||||||
.get(orderID)
|
orderToResponse.get(orderID).put(encInvoice, ack => {
|
||||||
.put(encInvoice, ack => {
|
|
||||||
if (ack.err) {
|
if (ack.err) {
|
||||||
console.error(`error saving order response: ${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