strophe / strophejs

Strophe.js is an XMPP library for JavaScript
http://strophe.im/strophejs
MIT License
1.46k stars 361 forks source link

Error handling on websocket stream error message #706

Closed Luiz-Monad closed 4 months ago

Luiz-Monad commented 5 months ago

Why am I getting an undefined error when I receive a stream:error IQ message ?

This is the relevant line: https://github.com/strophe/strophejs/blob/fd5916e7bd2c7b5a75e74f895270a16b26d34b1f/src/websocket.js#L99

And it only happens when the connection is already open, not when connecting as evidenced by onMessage processing on my call stack. https://github.com/strophe/strophejs/blob/fd5916e7bd2c7b5a75e74f895270a16b26d34b1f/src/websocket.js#L483

It seems someone had changed it from

for (let i = 0; i < error.childNodes.length; i++) {
            const e = error.childNodes[i];

to

       for (let i = 0; i < error.childNodes.length; i++) {
            const e = error.children[i];

as part of some refactoring. (children is not valid as per https://github.com/xmldom/xmldom/issues/410)

I am using the ejabberd server and it was sending a "spurious" stream:error because of a misconfiguration, but I need to be able to catch the error instead of it going as unhandled exception to the nodejs handler.

[edit: added the complete callstack]

[1] [16:23:32.982] ERROR (server): UNHANDLED 
[1]     err: {
[1]       "type": "TypeError",
[1]       "message": "Cannot read properties of undefined (reading '0')",
[1]       "stack":
[1]           TypeError: Cannot read properties of undefined (reading '0')
[1]               at Websocket._checkStreamError (\lib\server.js:5627:37)
[1]               at Websocket._onMessage (\lib\server.js:6015:18)
[1]               at socket.onmessage (\lib\server.js:5810:45)
[1]               at callListener (\node_modules\ws\lib\event-target.js:290:14)
[1]               at WebSocket.onMessage (\node_modules\ws\lib\event-target.js:209:9)
[1]               at WebSocket.emit (node:events:519:28)
[1]               at WebSocket.emit (node:domain:488:12)
[1]               at Receiver.receiverOnMessage (\node_modules\ws\lib\websocket.js:1209:20)
[1]               at Receiver.emit (node:events:519:28)
[1]               at Receiver.emit (node:domain:488:12)
[1]     }