mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: handle stream:errors more gracefully
This commit is contained in:
@@ -5,8 +5,8 @@ import WebSocket from 'ws'
|
|||||||
import { proto } from '../../WAProto'
|
import { proto } from '../../WAProto'
|
||||||
import { DEF_CALLBACK_PREFIX, DEF_TAG_PREFIX, DEFAULT_ORIGIN, INITIAL_PREKEY_COUNT, MIN_PREKEY_COUNT } from '../Defaults'
|
import { DEF_CALLBACK_PREFIX, DEF_TAG_PREFIX, DEFAULT_ORIGIN, INITIAL_PREKEY_COUNT, MIN_PREKEY_COUNT } from '../Defaults'
|
||||||
import { AuthenticationCreds, BaileysEventEmitter, BaileysEventMap, DisconnectReason, SocketConfig } from '../Types'
|
import { AuthenticationCreds, BaileysEventEmitter, BaileysEventMap, DisconnectReason, SocketConfig } from '../Types'
|
||||||
import { addTransactionCapability, bindWaitForConnectionUpdate, configureSuccessfulPairing, Curve, generateLoginNode, generateMdTagPrefix, generateRegistrationNode, getNextPreKeysNode, makeNoiseHandler, printQRIfNecessaryListener, promiseTimeout, useSingleFileAuthState } from '../Utils'
|
import { addTransactionCapability, bindWaitForConnectionUpdate, configureSuccessfulPairing, Curve, generateLoginNode, generateMdTagPrefix, generateRegistrationNode, getErrorCodeFromStreamErrorReason, getNextPreKeysNode, makeNoiseHandler, printQRIfNecessaryListener, promiseTimeout, useSingleFileAuthState } from '../Utils'
|
||||||
import { assertNodeErrorFree, BinaryNode, encodeBinaryNode, getBinaryNodeChild, getBinaryNodeChildren, S_WHATSAPP_NET } from '../WABinary'
|
import { assertNodeErrorFree, BinaryNode, encodeBinaryNode, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildren, S_WHATSAPP_NET } from '../WABinary'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connects to WA servers and performs:
|
* Connects to WA servers and performs:
|
||||||
@@ -528,10 +528,15 @@ export const makeSocket = ({
|
|||||||
})
|
})
|
||||||
|
|
||||||
ws.on('CB:stream:error', (node: BinaryNode) => {
|
ws.on('CB:stream:error', (node: BinaryNode) => {
|
||||||
logger.error({ error: node }, 'stream errored out')
|
logger.error({ node }, 'stream errored out')
|
||||||
|
|
||||||
const statusCode = +(node.attrs.code || DisconnectReason.restartRequired)
|
const [reasonNode] = getAllBinaryNodeChildren(node)
|
||||||
end(new Boom('Stream Errored', { statusCode, data: node }))
|
const reason = reasonNode?.tag || 'unknown'
|
||||||
|
const statusCode = +(
|
||||||
|
node.attrs.code
|
||||||
|
|| getErrorCodeFromStreamErrorReason(reason)
|
||||||
|
)
|
||||||
|
end(new Boom(`Stream Errored (${reason})`, { statusCode, data: node }))
|
||||||
})
|
})
|
||||||
// stream fail, possible logout
|
// stream fail, possible logout
|
||||||
ws.on('CB:failure', (node: BinaryNode) => {
|
ws.on('CB:failure', (node: BinaryNode) => {
|
||||||
|
|||||||
@@ -252,7 +252,10 @@ const STATUS_MAP: { [_: string]: proto.WebMessageInfo.WebMessageInfoStatus } = {
|
|||||||
'read': proto.WebMessageInfo.WebMessageInfoStatus.READ,
|
'read': proto.WebMessageInfo.WebMessageInfoStatus.READ,
|
||||||
'read-self': proto.WebMessageInfo.WebMessageInfoStatus.READ
|
'read-self': proto.WebMessageInfo.WebMessageInfoStatus.READ
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Given a type of receipt, returns what the new status of the message should be
|
||||||
|
* @param type type from receipt
|
||||||
|
*/
|
||||||
export const getStatusFromReceiptType = (type: string | undefined) => {
|
export const getStatusFromReceiptType = (type: string | undefined) => {
|
||||||
const status = STATUS_MAP[type]
|
const status = STATUS_MAP[type]
|
||||||
if(typeof type === 'undefined') {
|
if(typeof type === 'undefined') {
|
||||||
@@ -261,3 +264,15 @@ export const getStatusFromReceiptType = (type: string | undefined) => {
|
|||||||
|
|
||||||
return status
|
return status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CODE_MAP: { [_: string]: DisconnectReason } = {
|
||||||
|
conflict: DisconnectReason.connectionReplaced
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user