Better logs

This commit is contained in:
Daniel Lugo 2021-10-18 16:24:14 -04:00
parent 8eb7902e58
commit 4df681fae9

View file

@ -88,13 +88,17 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
try { try {
if (addr !== currentOrderAddr) { if (addr !== currentOrderAddr) {
logger.info( logger.info(
orderID,
`order address: ${addr} invalidated (current address: ${currentOrderAddr})` `order address: ${addr} invalidated (current address: ${currentOrderAddr})`
) )
return return
} }
if (!Schema.isOrder(order)) { if (!Schema.isOrder(order)) {
logger.info(`Expected an order instead got: ${JSON.stringify(order)}`) logger.info(
orderID,
`Expected an order instead got: ${JSON.stringify(order)}`
)
return return
} }
@ -129,19 +133,19 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
if (!isNumber(amount)) { if (!isNumber(amount)) {
throw new TypeError( throw new TypeError(
`Could not parse decrypted amount as a number, not a number?, decryptedAmount: ${decryptedAmount}` `${orderID} Could not parse decrypted amount as a number, not a number?, decryptedAmount: ${decryptedAmount}`
) )
} }
if (isNaN(amount)) { if (isNaN(amount)) {
throw new TypeError( throw new TypeError(
`Could not parse decrypted amount as a number, got NaN, decryptedAmount: ${decryptedAmount}` `${orderID} Could not parse decrypted amount as a number, got NaN, decryptedAmount: ${decryptedAmount}`
) )
} }
if (!isFinite(amount)) { if (!isFinite(amount)) {
throw new TypeError( throw new TypeError(
`Amount was correctly decrypted, but got a non finite number, decryptedAmount: ${decryptedAmount}` `${orderID} Amount was correctly decrypted, but got a non finite number, decryptedAmount: ${decryptedAmount}`
) )
} }
const mySecret = require('../../Mediator').getMySecret() const mySecret = require('../../Mediator').getMySecret()
@ -154,22 +158,22 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
*/ */
let serviceOrderContentSeedInfo = null //in case the service is of type 'torrentSeed' this is {seedUrl,seedToken}, can be omitted, in that case, it will be taken from env let serviceOrderContentSeedInfo = null //in case the service is of type 'torrentSeed' this is {seedUrl,seedToken}, can be omitted, in that case, it will be taken from env
if (order.targetType === 'service') { if (order.targetType === 'service') {
logger.info('General Service') logger.info(orderID, 'General Service')
const { ackInfo: serviceID } = order const { ackInfo: serviceID } = order
logger.info('ACK INFO') logger.info(orderID, 'ACK INFO')
logger.info(serviceID) logger.info(orderID, serviceID)
if (!Common.isPopulatedString(serviceID)) { if (!Common.isPopulatedString(serviceID)) {
throw new TypeError(`no serviceID provided to orderAck`) throw new TypeError(`${orderID} no serviceID provided to orderAck`)
} }
const selectedService = await getUser() const selectedService = await getUser()
.get(Key.OFFERED_SERVICES) .get(Key.OFFERED_SERVICES)
.get(serviceID) .get(serviceID)
.then() .then()
logger.info(selectedService) logger.info(orderID, selectedService)
if (!Common.isObj(selectedService)) { if (!Common.isObj(selectedService)) {
throw new TypeError( throw new TypeError(
`invalid serviceID provided to orderAck or service is not an object` `${orderID} invalid serviceID provided to orderAck or service is not an object`
) )
} }
@ -181,7 +185,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
} = /** @type {Record<string, any>} */ (selectedService) } = /** @type {Record<string, any>} */ (selectedService)
if (Number(amount) !== Number(servicePrice)) { if (Number(amount) !== Number(servicePrice)) {
throw new TypeError( throw new TypeError(
`service price mismatch ${amount} : ${servicePrice}` `${orderID} service price mismatch ${amount} : ${servicePrice}`
) )
} }
if (serviceType === 'torrentSeed') { if (serviceType === 'torrentSeed') {
@ -205,13 +209,13 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
const invoice = await _addInvoice(invoiceReq) const invoice = await _addInvoice(invoiceReq)
logger.info( logger.info(
'onOrders() -> Successfully created the invoice, will now encrypt it' `${orderID} onOrders() -> Successfully created the invoice, will now encrypt it`
) )
const encInvoice = await SEA.encrypt(invoice.payment_request, secret) const encInvoice = await SEA.encrypt(invoice.payment_request, secret)
logger.info( logger.info(
`onOrders() -> Will now place the encrypted invoice in order to response usergraph: ${addr}` `${orderID} onOrders() -> Will now place the encrypted invoice in order to response usergraph: ${addr}`
) )
const ackNode = Utils.gunID() const ackNode = Utils.gunID()
@ -236,7 +240,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
) { ) {
rej( rej(
new Error( new Error(
`Error saving encrypted invoice to order to response usergraph: ${ack}` `${orderID} Error saving encrypted invoice to order to response usergraph: ${ack}`
) )
) )
} else { } else {
@ -251,7 +255,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
* @param {Common.Schema.InvoiceWhenListed & {r_hash:Buffer,payment_addr:Buffer}} paidInvoice * @param {Common.Schema.InvoiceWhenListed & {r_hash:Buffer,payment_addr:Buffer}} paidInvoice
*/ */
const invoicePaidCb = async paidInvoice => { const invoicePaidCb = async paidInvoice => {
logger.info('INVOICE PAID') logger.info(orderID, 'INVOICE PAID')
let breakError = null let breakError = null
let orderMetadata //eslint-disable-line init-declarations let orderMetadata //eslint-disable-line init-declarations
const hashString = paidInvoice.r_hash.toString('hex') const hashString = paidInvoice.r_hash.toString('hex')
@ -267,7 +271,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
case 'tip': { case 'tip': {
const postID = ackInfo const postID = ackInfo
if (!Common.isPopulatedString(postID)) { if (!Common.isPopulatedString(postID)) {
breakError = 'invalid ackInfo provided for postID' breakError = orderID + ' invalid ackInfo provided for postID'
break //create the coordinate, but stop because of the invalid id break //create the coordinate, but stop because of the invalid id
} }
getUser() getUser()
@ -301,7 +305,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
) { ) {
rej( rej(
new Error( new Error(
`Error saving encrypted orderAck to order to response usergraph: ${ack}` `${orderID} Error saving encrypted orderAck to order to response usergraph: ${ack}`
) )
) )
} else { } else {
@ -317,16 +321,16 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
break break
} }
case 'contentReveal': { case 'contentReveal': {
logger.info('cONTENT REVEAL') logger.info(orderID, 'CONTENT REVEAL')
//assuming digital product that only requires to be unlocked //assuming digital product that only requires to be unlocked
const postID = ackInfo const postID = ackInfo
logger.info('ACK INFO') logger.info(orderID, 'ACK INFO')
logger.info(ackInfo) logger.info(ackInfo)
if (!Common.isPopulatedString(postID)) { if (!Common.isPopulatedString(postID)) {
breakError = 'invalid ackInfo provided for postID' breakError = 'invalid ackInfo provided for postID'
break //create the coordinate, but stop because of the invalid id break //create the coordinate, but stop because of the invalid id
} }
logger.info('IS STRING') logger.info(orderID, 'IS STRING')
const selectedPost = /** @type {Record<string, any>} */ (await getUser() const selectedPost = /** @type {Record<string, any>} */ (await getUser()
.get(Key.POSTS_NEW) .get(Key.POSTS_NEW)
.get(postID) .get(postID)
@ -337,7 +341,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
.get(Key.CONTENT_ITEMS) .get(Key.CONTENT_ITEMS)
.then()) .then())
logger.info('LOAD ok') logger.info(orderID, 'LOAD ok')
logger.info(selectedPost) logger.info(selectedPost)
if ( if (
!selectedPost || !selectedPost ||
@ -347,12 +351,12 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
breakError = 'ackInfo provided does not correspond to a valid post' breakError = 'ackInfo provided does not correspond to a valid post'
break //create the coordinate, but stop because of the invalid post break //create the coordinate, but stop because of the invalid post
} }
logger.info('IS POST') logger.info(orderID, 'IS POST')
/** /**
* @type {Record<string,string>} <contentID,decryptedRef> * @type {Record<string,string>} <contentID,decryptedRef>
*/ */
const contentsToSend = {} const contentsToSend = {}
logger.info('SECRET OK') logger.info(orderID, 'SECRET OK')
let privateFound = false let privateFound = false
await Common.Utils.asyncForEach( await Common.Utils.asyncForEach(
Object.entries(selectedPostContent), Object.entries(selectedPostContent),
@ -383,7 +387,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
type: 'orderAck', type: 'orderAck',
response: encrypted response: encrypted
} }
logger.info('RES READY') logger.info(orderID, 'RES READY')
await new Promise((res, rej) => { await new Promise((res, rej) => {
getUser() getUser()
@ -405,12 +409,12 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
} }
}) })
}) })
logger.info('RES SENT CONTENT') logger.info(orderID, 'RES SENT CONTENT')
orderMetadata = JSON.stringify(ackData) orderMetadata = JSON.stringify(ackData)
break break
} }
case 'torrentSeed': { case 'torrentSeed': {
logger.info('TORRENT') logger.info(orderID, 'TORRENT')
const numberOfTokens = Number(ackInfo) || 1 const numberOfTokens = Number(ackInfo) || 1
const seedInfo = selfContentToken() const seedInfo = selfContentToken()
if (!seedInfo && !serviceOrderContentSeedInfo) { if (!seedInfo && !serviceOrderContentSeedInfo) {
@ -427,7 +431,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
numberOfTokens, numberOfTokens,
seedInfoReady seedInfoReady
) )
logger.info('RES SEED OK') logger.info(orderID, 'RES SEED OK')
const ackData = { seedUrl, tokens, ackInfo } const ackData = { seedUrl, tokens, ackInfo }
const toSend = JSON.stringify(ackData) const toSend = JSON.stringify(ackData)
const encrypted = await SEA.encrypt(toSend, secret) const encrypted = await SEA.encrypt(toSend, secret)
@ -435,7 +439,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
type: 'orderAck', type: 'orderAck',
response: encrypted response: encrypted
} }
logger.info('RES SEED SENT') logger.info(orderID, 'RES SEED SENT')
await new Promise((res, rej) => { await new Promise((res, rej) => {
getUser() getUser()
.get(Key.ORDER_TO_RESPONSE) .get(Key.ORDER_TO_RESPONSE)
@ -456,7 +460,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
} }
}) })
}) })
logger.info('RES SENT SEED') logger.info(orderID, 'RES SENT SEED')
orderMetadata = JSON.stringify(ackData) orderMetadata = JSON.stringify(ackData)
break break
} }
@ -486,17 +490,18 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
throw new Error(breakError) throw new Error(breakError)
} }
} }
logger.info('Waiting for invoice to be paid for order ' + orderID) logger.info(orderID, 'Waiting for invoice to be paid for order ' + orderID)
new Promise(res => SchemaManager.addListenInvoice(invoice.r_hash, res)) new Promise(res => SchemaManager.addListenInvoice(invoice.r_hash, res))
.then(invoicePaidCb) .then(invoicePaidCb)
.catch(err => { .catch(err => {
logger.error( logger.error(
orderID,
`error inside onOrders, orderAddr: ${addr}, orderID: ${orderID}, order: ${JSON.stringify( `error inside onOrders, orderAddr: ${addr}, orderID: ${orderID}, order: ${JSON.stringify(
order order
)}` )}`
) )
logger.error(err) logger.error(orderID, err)
logger.info(err) logger.info(orderID, err)
/** @type {import('shock-common').Schema.OrderResponse} */ /** @type {import('shock-common').Schema.OrderResponse} */
const orderResponse = { const orderResponse = {
@ -515,6 +520,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
typeof ack.err !== 'object' typeof ack.err !== 'object'
) { ) {
logger.error( logger.error(
orderID,
`Error saving encrypted invoice to order to response usergraph: ${ack}` `Error saving encrypted invoice to order to response usergraph: ${ack}`
) )
} }
@ -522,12 +528,13 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
}) })
} catch (/** @type {any} */ err) { } catch (/** @type {any} */ err) {
logger.error( logger.error(
orderID,
`error inside onOrders, orderAddr: ${addr}, orderID: ${orderID}, order: ${JSON.stringify( `error inside onOrders, orderAddr: ${addr}, orderID: ${orderID}, order: ${JSON.stringify(
order order
)}` )}`
) )
logger.error(err) logger.error(orderID, err)
logger.info(err) logger.info(orderID, err)
/** @type {import('shock-common').Schema.OrderResponse} */ /** @type {import('shock-common').Schema.OrderResponse} */
const orderResponse = { const orderResponse = {
@ -546,6 +553,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
typeof ack.err !== 'object' typeof ack.err !== 'object'
) { ) {
logger.error( logger.error(
orderID,
`Error saving encrypted invoice to order to response usergraph: ${ack}` `Error saving encrypted invoice to order to response usergraph: ${ack}`
) )
} }