首頁  >  問答  >  主體

增強 Node.js 中的錯誤訊息輸出關閉監聽器

我有以下處理http請求的程式碼。

request.connection.addListener('close', function(had_error) {
   console.log(had_error);
   ...

我想列印更多錯誤訊息,但它只列印“true”。

P粉517475670P粉517475670171 天前324

全部回覆(1)我來回復

  • P粉662614213

    P粉6626142132024-04-04 08:08:06

    使用錯誤事件來追蹤錯誤。它會傳回包含完整詳細資訊的 Error 物件。

    request.connection.addListener('error', function(error) {
        console.log(error.reason|| error.message);
    });

    由於連線已從 v15 開始被棄用,如果您使用的是最新版本,也可以使用它

    request.on('error',function(error){
        console.log(error.reason|| error.message);
    });

    回覆
    0
  • 取消回覆