I would like to be able to get the stack data (fileName and lineNumber) from an Error object in React Native.
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:
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.