refactor: remove redundant stream:error handling

This commit is contained in:
Adhiraj Singh
2022-04-25 13:27:25 +05:30
parent 9424d04f5d
commit fd9daf8ad3
3 changed files with 21 additions and 28 deletions

View File

@@ -6,6 +6,7 @@ import { Logger } from 'pino'
import { proto } from '../../WAProto'
import { version as baileysVersion } from '../Defaults/baileys-version.json'
import { CommonBaileysEventEmitter, ConnectionState, DisconnectReason, WAVersion } from '../Types'
import { BinaryNode, getAllBinaryNodeChildren } from '../WABinary'
const PLATFORM_MAP = {
'aix': 'AIX',
@@ -273,6 +274,17 @@ const CODE_MAP: { [_: string]: DisconnectReason } = {
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
* @param reason the string reason given, eg. "conflict"
*/
export const getErrorCodeFromStreamErrorReason = (reason: string) => {
return CODE_MAP[reason] || DisconnectReason.badSession
export const getErrorCodeFromStreamError = (node: BinaryNode) => {
const [reasonNode] = getAllBinaryNodeChildren(node)
let reason = reasonNode?.tag || 'unknown'
const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession)
if(statusCode === DisconnectReason.restartRequired) {
reason = 'restart required'
}
return {
reason,
statusCode
}
}