【问题标题】:Getting stack data from Error Objects in React Native - Expo - CRNA从 React Native 中的错误对象获取堆栈数据 - Expo - CRNA
【发布时间】:2019-01-17 12:32:42
【问题描述】:

我希望能够从 React Native 中的错误对象中获取堆栈数据(文件名和行号)。
如果我运行这样的事情:

const err = new Error('Message');
console.log(err);

我在控制台中得到一些有用的输出,如下所示:

Message
* App.js:21:16 in componentDidMount
- node_modules\react-proxy\modules\createPrototypeProxy.js:61:45 in proxiedComponentDidMount
- node_modules\react-native\Libraries\Renderer\ReactNativeRenderer-dev.js:10627:12 in commitLifeCycles
- ... 23 more stack frames from framework internals

我希望能够将该堆栈数据发送到其他地方以进行记录和修复,但我不知道如何访问显示文件名和生成错误的行的数据。
我怎样才能获得这些信息?
根据文档Error - JavaScript MDN,如果我尝试:

console.log(err.fileName)

或:

console.log(err.lineNumber)

我刚刚得到:

undefined

确认:

console.log(Object.keys(err));

导致:

Array []

虽然,神秘地:

console.log(err.message);

仍然返回:

Message

相关 - 如果我输入:

console.log(err.toString())

我明白了:

Error: Message

同样,如果我输入:

console.log('Comment', err);

我得到输出:

Comment [Error: Message]

所以虽然我可以看到堆栈数据在那里,但我似乎无法访问它或将它传递到其他地方。

【问题讨论】:

    标签: javascript react-native error-handling expo create-react-native-app


    【解决方案1】:

    查看https://github.com/marchingband/expo-error-log。 您需要注册自己的错误处理程序回调。 你可以使用一个ErrorBoundary,或者用其他一些方法来捕获错误,其中一些会捕获所有的JS错误,一些不会。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 2021-05-19
      • 2016-11-22
      相关资源
      最近更新 更多