mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: better mapping for WS errors
This commit is contained in:
@@ -97,7 +97,7 @@ export const makeSocket = ({
|
|||||||
|
|
||||||
const result = promiseTimeout<any>(connectTimeoutMs, (resolve, reject) => {
|
const result = promiseTimeout<any>(connectTimeoutMs, (resolve, reject) => {
|
||||||
onOpen = (data: any) => resolve(data)
|
onOpen = (data: any) => resolve(data)
|
||||||
onClose = reject
|
onClose = mapWebSocketError(reject)
|
||||||
ws.on('frame', onOpen)
|
ws.on('frame', onOpen)
|
||||||
ws.on('close', onClose)
|
ws.on('close', onClose)
|
||||||
ws.on('error', onClose)
|
ws.on('error', onClose)
|
||||||
@@ -335,7 +335,7 @@ export const makeSocket = ({
|
|||||||
let onClose: (err: Error) => void
|
let onClose: (err: Error) => void
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
onOpen = () => resolve(undefined)
|
onOpen = () => resolve(undefined)
|
||||||
onClose = reject
|
onClose = mapWebSocketError(reject)
|
||||||
ws.on('open', onOpen)
|
ws.on('open', onOpen)
|
||||||
ws.on('close', onClose)
|
ws.on('close', onClose)
|
||||||
ws.on('error', onClose)
|
ws.on('error', onClose)
|
||||||
@@ -433,12 +433,7 @@ export const makeSocket = ({
|
|||||||
end(err)
|
end(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
ws.on('error', error => end(
|
ws.on('error', mapWebSocketError(end))
|
||||||
new Boom(
|
|
||||||
`WebSocket Error (${error.message})`,
|
|
||||||
{ statusCode: getCodeFromWSError(error), data: error }
|
|
||||||
)
|
|
||||||
))
|
|
||||||
ws.on('close', () => end(new Boom('Connection Terminated', { statusCode: DisconnectReason.connectionClosed })))
|
ws.on('close', () => end(new Boom('Connection Terminated', { statusCode: DisconnectReason.connectionClosed })))
|
||||||
// the server terminated the connection
|
// the server terminated the connection
|
||||||
ws.on('CB:xmlstreamend', () => end(new Boom('Connection Terminated by Server', { statusCode: DisconnectReason.connectionClosed })))
|
ws.on('CB:xmlstreamend', () => end(new Boom('Connection Terminated by Server', { statusCode: DisconnectReason.connectionClosed })))
|
||||||
@@ -604,4 +599,19 @@ export const makeSocket = ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* map the websocket error to the right type
|
||||||
|
* so it can be retried by the caller
|
||||||
|
* */
|
||||||
|
function mapWebSocketError(handler: (err: Error) => void) {
|
||||||
|
return (error: Error) => {
|
||||||
|
handler(
|
||||||
|
new Boom(
|
||||||
|
`WebSocket Error (${error.message})`,
|
||||||
|
{ statusCode: getCodeFromWSError(error), data: error }
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export type Socket = ReturnType<typeof makeSocket>
|
export type Socket = ReturnType<typeof makeSocket>
|
||||||
|
|||||||
@@ -353,7 +353,10 @@ export const getCodeFromWSError = (error: Error) => {
|
|||||||
if(!Number.isNaN(code) && code >= 400) {
|
if(!Number.isNaN(code) && code >= 400) {
|
||||||
statusCode = code
|
statusCode = code
|
||||||
}
|
}
|
||||||
} else if((error as any).code?.startsWith('E')) { // handle ETIMEOUT, ENOTFOUND etc
|
} else if(
|
||||||
|
(error as any).code?.startsWith('E')
|
||||||
|
|| error?.message?.includes('timed out')
|
||||||
|
) { // handle ETIMEOUT, ENOTFOUND etc
|
||||||
statusCode = 408
|
statusCode = 408
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user