momocow / node-cq-websocket

A Node SDK for developing QQ chatbots based on WebSocket, which is depending on CoolQ and CQHTTP API plugin.

Home Page:https://cq-websocket.js.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

自動重連時的 UnhandledPromiseRejectionWarning

momocow opened this issue · comments

找出哪個環節有 Promise 沒有處理好, 看起來會是 websocket 的東西。

const CQWebsocket = require('../..')

const bot = new CQWebsocket()

bot.on('socket.connecting', function (wsType, attempts) {
  console.log('[%s] 嘗試第 %d 次連線 _(:з」∠)_', wsType, attempts)
}).on('socket.connect', function (wsType, sock, attempts) {
  console.log('[%s] 第 %d 次連線嘗試成功 ヽ(✿゚▽゚)ノ', wsType, attempts)
}).on('socket.failed', function (wsType, attempts) {
  console.log('[%s] 第 %d 次連線嘗試失敗 。・゚・(つд`゚)・゚・', wsType, attempts)
})

bot.connect()
[/event] 第 1 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)
(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

(node:10652) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

[/api] 第 1 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

[/event] 第 2 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)

[/api] 第 2 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)

[/event] 第 3 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)

[/api] 第 3 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)

[/event] 第 4 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 7)

[/api] 第 4 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 8)

[/event] 第 5 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 9)

[/api] 第 5 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 10)

[/event] 第 6 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 11)

[/api] 第 6 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 12)

[/event] 第 7 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 13)

[/api] 第 7 次連線嘗試失敗 。・゚・(つд`゚)・゚・

(node:10652) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)

(node:10652) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 14)

結果是我自己的問題...

拋出 Promise 錯誤, 是預期的行為
此行為乃默認 socket.error 監聽器所為。

添加以下監聽器可以修正。

bot.on('socket.error', function (wsType, err) {
  console.log('[%s] 錯誤: %O', wsType, err)
})
[/event] 第 1 次連線嘗試失敗 。・゚・(つд`゚)・゚・
[/event] 錯誤: { Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6700 }
[/api] 第 1 次連線嘗試失敗 。・゚・(つд`゚)・゚・
[/api] 錯誤: { Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6700 }
[/event] 第 2 次連線嘗試失敗 。・゚・(つд`゚)・゚・
[/event] 錯誤: { Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6700 }
[/api] 第 2 次連線嘗試失敗 。・゚・(つд`゚)・゚・
[/api] 錯誤: { Error: connect ECONNREFUSED 127.0.0.1:6700
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1162:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6700 }

沒事了 沒事了 。・゚・(つд`゚)・゚・

已增加 socket.error 發生時的錯誤提醒, 訊息如下

You should listen on "socket.error" yourself to avoid those unhandled promise warnings.