简体   繁体   English

从 React Native 中的错误对象获取堆栈数据 - Expo - CRNA

[英]Getting stack data from Error Objects in React Native - Expo - CRNA

I would like to be able to get the stack data (fileName and lineNumber) from an Error object in React Native.我希望能够从 React Native 中的 Error 对象获取堆栈数据(fileName 和 lineNumber)。
If I run something like this:如果我运行这样的东西:

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

I get some helpful output in my console like this:我在控制台中得到一些有用的输出,如下所示:

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

I would like to be able to send that stack data off elsewhere for logging and fixing, but I can't figure out how to access the data which shows the filename and the line where the error was generated.我希望能够将该堆栈数据发送到其他地方以进行记录和修复,但我无法弄清楚如何访问显示文件名和生成错误所在行的数据。
How can I get that information?我怎样才能得到这些信息?
As per the documentation Error - JavaScript MDN , if I try:根据文档Error - JavaScript MDN ,如果我尝试:

console.log(err.fileName)

or:或者:

console.log(err.lineNumber)

I just get:我只是得到:

undefined

Which is confirmed by:这是通过以下方式确认的:

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

resulting in:导致:

Array []

Although, mysteriously:虽然,神秘地:

console.log(err.message);

still returns:仍然返回:

Message

Related - if I enter:相关 - 如果我输入:

console.log(err.toString())

I get:我得到:

Error: Message

Similarly, if I enter:同样,如果我输入:

console.log('Comment', err);

I get as output:我得到作为输出:

Comment [Error: Message]

So while I can see the stack data is there, I don't seem to be able to access it or pass it somewhere else.因此,虽然我可以看到堆栈数据在那里,但我似乎无法访问它或将其传递到其他地方。

check out https://github.com/marchingband/expo-error-log .查看https://github.com/marchingband/expo-error-log You'll need to register your own Error Handler callback.您需要注册自己的错误处理程序回调。 You can use an ErrorBoundary, or capture the errors in a number of other ways, some of which will capture all the JS errors, some of which will not.您可以使用 ErrorBoundary,或以多种其他方式捕获错误,其中一些将捕获所有 JS 错误,而另一些则不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM