Merge pull request #424 from shocknet/bug/unhandled-rejections

Added try/catch to async code to prevent unhandled rejections
This commit is contained in:
CapDog 2021-07-18 09:24:50 -04:00 committed by GitHub
commit f1a3bb2b73

View file

@ -49,6 +49,7 @@ module.exports = async (
mySocketsEvents, mySocketsEvents,
{ serverPort, CA, CA_KEY, usetls } { serverPort, CA, CA_KEY, usetls }
) => { ) => {
try {
const { timeout5 } = require('../services/gunDB/contact-api/utils') const { timeout5 } = require('../services/gunDB/contact-api/utils')
const Http = Axios.create({ const Http = Axios.create({
@ -650,9 +651,9 @@ module.exports = async (
process.env.TRUSTED_KEYS === 'true' || !process.env.TRUSTED_KEYS process.env.TRUSTED_KEYS === 'true' || !process.env.TRUSTED_KEYS
const trustedKeys = await Storage.get('trustedPKs') const trustedKeys = await Storage.get('trustedPKs')
// Falls back to true if trusted keys is disabled in .env // Falls back to true if trusted keys is disabled in .env
const [isKeyTrusted = !trustedKeysEnabled] = (trustedKeys || []).filter( const [isKeyTrusted = !trustedKeysEnabled] = (
trustedKey => trustedKey === publicKey trustedKeys || []
) ).filter(trustedKey => trustedKey === publicKey)
const walletUnlocked = health.LNDStatus.walletStatus === 'unlocked' const walletUnlocked = health.LNDStatus.walletStatus === 'unlocked'
const { authorization = '' } = req.headers const { authorization = '' } = req.headers
@ -1483,7 +1484,10 @@ module.exports = async (
}) })
} else { } else {
res.status(500) res.status(500)
res.json({ errorMessage: health.LNDStatus.message, success: false }) res.json({
errorMessage: health.LNDStatus.message,
success: false
})
} }
} else { } else {
// logger.debug("ListInvoices:", response); // logger.debug("ListInvoices:", response);
@ -1589,7 +1593,9 @@ module.exports = async (
app.post('/api/lnd/decodePayReq', (req, res) => { app.post('/api/lnd/decodePayReq', (req, res) => {
const { lightning } = LightningServices.services const { lightning } = LightningServices.services
const { payReq } = req.body const { payReq } = req.body
lightning.decodePayReq({ pay_req: payReq }, async (err, paymentRequest) => { lightning.decodePayReq(
{ pay_req: payReq },
async (err, paymentRequest) => {
if (err) { if (err) {
logger.debug('DecodePayReq Error:', err) logger.debug('DecodePayReq Error:', err)
const health = await checkHealth() const health = await checkHealth()
@ -1606,7 +1612,8 @@ module.exports = async (
decodedRequest: paymentRequest decodedRequest: paymentRequest
}) })
} }
}) }
)
}) })
app.get('/api/lnd/channelbalance', (req, res) => { app.get('/api/lnd/channelbalance', (req, res) => {
@ -1634,7 +1641,12 @@ module.exports = async (
app.post('/api/lnd/openchannel', (req, res) => { app.post('/api/lnd/openchannel', (req, res) => {
const { lightning } = LightningServices.services const { lightning } = LightningServices.services
const { pubkey, channelCapacity, channelPushAmount, satPerByte } = req.body const {
pubkey,
channelCapacity,
channelPushAmount,
satPerByte
} = req.body
const openChannelRequest = { const openChannelRequest = {
node_pubkey: Buffer.from(pubkey, 'hex'), node_pubkey: Buffer.from(pubkey, 'hex'),
@ -1729,7 +1741,8 @@ module.exports = async (
if (!feeLimit) { if (!feeLimit) {
return res.status(400).json({ return res.status(400).json({
errorMessage: 'please provide a "feeLimit" to the send payment request' errorMessage:
'please provide a "feeLimit" to the send payment request'
}) })
} }
@ -1738,7 +1751,8 @@ module.exports = async (
const { dest, amt, finalCltvDelta = 40 } = req.body const { dest, amt, finalCltvDelta = 40 } = req.body
if (!dest || !amt) { if (!dest || !amt) {
return res.status(400).json({ return res.status(400).json({
errorMessage: 'please provide "dest" and "amt" for keysend payments' errorMessage:
'please provide "dest" and "amt" for keysend payments'
}) })
} }
@ -1901,7 +1915,10 @@ module.exports = async (
app.post('/api/lnd/verifymessage', (req, res) => { app.post('/api/lnd/verifymessage', (req, res) => {
const { lightning } = LightningServices.services const { lightning } = LightningServices.services
lightning.verifyMessage( lightning.verifyMessage(
{ msg: Buffer.from(req.body.msg, 'utf8'), signature: req.body.signature }, {
msg: Buffer.from(req.body.msg, 'utf8'),
signature: req.body.signature
},
async (err, response) => { async (err, response) => {
if (err) { if (err) {
logger.debug('VerifyMessage Error:', err) logger.debug('VerifyMessage Error:', err)
@ -2144,7 +2161,9 @@ module.exports = async (
logger.info('Error in Chats poll:') logger.info('Error in Chats poll:')
logger.error(err) logger.error(err)
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: typeof err === 'string' ? err : err.message errorMessage: typeof err === 'string' ? err : err.message
}) })
@ -2167,7 +2186,9 @@ module.exports = async (
logger.info('Error in Display Name poll:') logger.info('Error in Display Name poll:')
logger.error(err) logger.error(err)
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: typeof err === 'string' ? err : err.message errorMessage: typeof err === 'string' ? err : err.message
}) })
@ -2187,7 +2208,9 @@ module.exports = async (
logger.info('Error in Handshake Address poll:') logger.info('Error in Handshake Address poll:')
logger.error(err) logger.error(err)
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: typeof err === 'string' ? err : err.message errorMessage: typeof err === 'string' ? err : err.message
}) })
@ -2206,7 +2229,9 @@ module.exports = async (
logger.info('Error in BIO poll:') logger.info('Error in BIO poll:')
logger.error(err) logger.error(err)
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: typeof err === 'string' ? err : err.message errorMessage: typeof err === 'string' ? err : err.message
}) })
@ -2803,7 +2828,10 @@ module.exports = async (
} }
} }
ap.get(`/api/gun/${GunEvent.ON_RECEIVED_REQUESTS}`, apiGunRequestsReceivedGet) ap.get(
`/api/gun/${GunEvent.ON_RECEIVED_REQUESTS}`,
apiGunRequestsReceivedGet
)
ap.get(`/api/gun/${GunEvent.ON_SENT_REQUESTS}`, apiGunRequestsSentGet) ap.get(`/api/gun/${GunEvent.ON_SENT_REQUESTS}`, apiGunRequestsSentGet)
ap.get(`/api/gun/requests/received`, apiGunRequestsReceivedGet) ap.get(`/api/gun/requests/received`, apiGunRequestsReceivedGet)
ap.get(`/api/gun/requests/sent`, apiGunRequestsSentGet) ap.get(`/api/gun/requests/sent`, apiGunRequestsSentGet)
@ -2933,7 +2961,9 @@ module.exports = async (
} }
}) })
ap.get(`/api/gun/dev/handshakeNodes/:handshakeAddress`, async (req, res) => { ap.get(
`/api/gun/dev/handshakeNodes/:handshakeAddress`,
async (req, res) => {
try { try {
const { tryAndWait } = require('../services/gunDB/contact-api/utils') const { tryAndWait } = require('../services/gunDB/contact-api/utils')
@ -2968,7 +2998,8 @@ module.exports = async (
errorMessage: err.message errorMessage: err.message
}) })
} }
}) }
)
ap.get(`/api/gun/dev/user/:publicKey`, async (req, res) => { ap.get(`/api/gun/dev/user/:publicKey`, async (req, res) => {
try { try {
@ -3146,7 +3177,9 @@ module.exports = async (
logger.error('error in rpc once') logger.error('error in rpc once')
logger.error(err) logger.error(err)
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: err.message errorMessage: err.message
}) })
@ -3170,7 +3203,9 @@ module.exports = async (
logger.error('error in rpc load') logger.error('error in rpc load')
logger.error(err) logger.error(err)
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: err.message errorMessage: err.message
}) })
@ -3194,7 +3229,9 @@ module.exports = async (
logger.error('error in rpc once user') logger.error('error in rpc once user')
logger.error(err) logger.error(err)
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: err.message errorMessage: err.message
}) })
@ -3218,7 +3255,9 @@ module.exports = async (
logger.error('error in rpc load user') logger.error('error in rpc load user')
logger.error(err) logger.error(err)
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: err.message errorMessage: err.message
}) })
@ -3256,7 +3295,9 @@ module.exports = async (
}) })
} catch (err) { } catch (err) {
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: err.message errorMessage: err.message
}) })
@ -3304,7 +3345,9 @@ module.exports = async (
}) })
} catch (err) { } catch (err) {
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: err.message errorMessage: err.message
}) })
@ -3323,7 +3366,9 @@ module.exports = async (
}) })
} catch (err) { } catch (err) {
res res
.status(err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500) .status(
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
)
.json({ .json({
errorMessage: err.message errorMessage: err.message
}) })
@ -3405,4 +3450,7 @@ module.exports = async (
}) })
} }
}) })
} catch (err) {
logger.warn('Unhandled rejection:', err)
}
} }