2017-07-07 55 views
0

我正在使用ES2017的await關鍵字和util.promisify()使用https.get()awaitPromisfying https.get後無'message'錯誤.get

測試連接到HTTPS用下面的代碼:

const util = require('util'), 
    https = require('https'), 
    httpsGet = util.promisify(https.get); 

var start = async function(){ 
    try { 
     var res = await httpsGet('https://github.com') 
    } catch(err) { 
     console.log('>>>>>>>CAUGHT AN ERROR>>>>>>', err.message) 
    } 
    console.log('FINISHED') 
} 

start() 

返回:

>>>>>>>CAUGHT AN ERROR>>>>>> undefined 

我懷疑我的代碼是壞了,但我不知道怎麼樣。 我怎樣才能得到一個真正的錯誤?

更新:根據要求,只是記錄ERR不err.message:

>>>>>>>CAUGHT AN ERROR - RAW ERR>>>>>> IncomingMessage { 
    _readableState: 
    ReadableState { 
    objectMode: false, 
    highWaterMark: 16384, 
    buffer: BufferList { head: null, tail: null, length: 0 }, 
    length: 0, 
    pipes: null, 
    pipesCount: 0, 
    flowing: null, 
    ended: false, 
    endEmitted: false, 
    reading: false, 
+1

它顯示的東西,如果你記錄'err'而不是'err.message'?我認爲錯誤對象有記錄時自動調用的'toString'方法 – Kaddath

+0

如果你像'(JSON.stringify(err))那樣記錄日誌' –

+0

@Kaddath在上面添加記錄'err'的結果, – mikemaccana

回答

3

https.getdoes not have an err option回調。這是一個流 - 錯誤通過.on(error)處理。因此,它不具有err回調並不能與util.promisify()

使用正在顯示一個err的唯一原因是,res是回調的最後一個項目 - 這promisify預計將是一個錯誤。所以這裏拋出的err實際上是一個res Stream。