首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript console.log错误:如何查看错误的真实对象

Javascript console.log错误:如何查看错误的真实对象
EN

Stack Overflow用户
提问于 2022-02-03 08:10:11
回答 1查看 2.1K关注 0票数 5

当console.logging错误时,浏览器打印的输出样式与正常对象不同。如何迫使Google / Firefox打印错误类的真实对象,而不是样式化的不太有用的“错误”输出?

例如,我知道对象包含e.messagee.response,您永远无法从浏览器日志输出中扣除它们。

代码语言:javascript
复制
api
  .post('/post/create', formData)
  .then((res) => {
  })
  .catch((e) => {
    // doesn't print the error object
    // doesn't print the error object
    // doesn't print the error object
    console.log(e)

    // UPDATE, destructuring does print the full Error object
    // UPDATE, destructuring does print the full Error object
    // UPDATE, destructuring does print the full Error object
    console.log('full error object', {e})
  })

更新后,批准答案。现在我得到了完整的错误对象。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-03 09:17:25

简单解决方案-将错误对象记录为普通对象的属性

代码语言:javascript
复制
try {
    throw Error("Some error text");
}
catch( error) {
    console.log( {error});
}

这允许您以任何其他方式检查和展开错误对象的结构。最好在浏览器中执行,因为代码段不提供检查工具。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70967772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档