Moved message parsing logic outside try/catch

This commit is contained in:
Adhiraj Singh
2020-10-28 22:44:23 +05:30
parent 57c46e3122
commit 5d3048ce38

View File

@@ -299,11 +299,21 @@ export class WAConnection extends Base {
this.lastSeen = new Date(parseInt(timestamp)) this.lastSeen = new Date(parseInt(timestamp))
this.emit ('received-pong') this.emit ('received-pong')
} else { } else {
let messageTag: string
let json: any
try { try {
const [messageTag, json] = Utils.decryptWA (message, this.authInfo?.macKey, this.authInfo?.encKey, new Decoder()) const dec = Utils.decryptWA (message, this.authInfo?.macKey, this.authInfo?.encKey, new Decoder())
if (this.shouldLogMessages) this.messageLog.push ({ tag: messageTag, json: JSON.stringify(json), fromMe: false }) messageTag = dec[0]
json = dec[1]
} catch (error) {
this.logger.error ({ error }, `encountered error in decrypting message, closing: ${error}`)
if (!json) { return } if (this.state === 'open') this.unexpectedDisconnect (DisconnectReason.badSession)
else this.rejectPendingConnection (new Error(DisconnectReason.badSession))
}
if (this.shouldLogMessages) this.messageLog.push ({ tag: messageTag, json: JSON.stringify(json), fromMe: false })
if (!json) return
if (this.logger.level === 'trace') { if (this.logger.level === 'trace') {
this.logger.trace(messageTag + ', ' + JSON.stringify(json)) this.logger.trace(messageTag + ', ' + JSON.stringify(json))
@@ -340,12 +350,6 @@ export class WAConnection extends Base {
if (this.logger.level === 'debug') { if (this.logger.level === 'debug') {
this.logger.debug({ unhandled: true }, messageTag + ',' + JSON.stringify(json)) this.logger.debug({ unhandled: true }, messageTag + ',' + JSON.stringify(json))
} }
} catch (error) {
this.logger.error ({ error }, `encountered error in decrypting message, closing: ${error}`)
if (this.state === 'open') this.unexpectedDisconnect (DisconnectReason.badSession)
else this.rejectPendingConnection (new Error(DisconnectReason.badSession))
}
} }
} }
/** Send a keep alive request every X seconds, server updates & responds with last seen */ /** Send a keep alive request every X seconds, server updates & responds with last seen */