我正在尝试使用JSON.stringify将对象转换为字符串,但是我得到了空对象
console.log('typeof',typeof e,' e value is',e, 'JSON stringify is',JSON.stringify(e))
我尝试打印时的错误消息
typeof 对象的值是错误:错误:发生了网络错误(例如超时,连接中断或主机不可达)。 JSON字符串化为 {}
答案 0 :(得分:2)
您的对象e
是一个错误对象。当您尝试对字符串进行分类时,您会在Chrome和node中得到{}
。 Safari显示了更多信息。
let e = new Error("hello")
console.log(typeof e)
console.log(JSON.stringify(e))
您可以使用以下方法测试错误:
let e = new Error("Some error happened")
if (e instanceof Error) {
console.log("Error:", e.message)
}