0
我正在使用ES2017的await
關鍵字和util.promisify()
使用https.get()
和await
。Promisfying 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,
它顯示的東西,如果你記錄'err'而不是'err.message'?我認爲錯誤對象有記錄時自動調用的'toString'方法 – Kaddath
如果你像'(JSON.stringify(err))那樣記錄日誌' –
@Kaddath在上面添加記錄'err'的結果, – mikemaccana