有谁知道什么是捕获所有未捕获的异常(全局)的最佳方法,以便我可以将崩溃报告发送回服务器?我似乎无法在反应原生文档或github上找到任何信息.
Col*_*say 25
您可以覆盖React Native用于开发的异常日志记录:
ErrorUtils.setGlobalHandler(function() {
// your handler here
});
Run Code Online (Sandbox Code Playgroud)
然后,您可能需要编写一些您向JS公开的Obj-C,具体取决于您的具体要求.
Sud*_*Plz 13
我就是这样做的:
第1步:我们拦截反应原生错误处理程序,如下所示:
//intercept react-native error handling
if (ErrorUtils._globalHandler) {
this.defaultHandler = ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler() || ErrorUtils._globalHandler;
ErrorUtils.setGlobalHandler(this.wrapGlobalHandler); //feed errors directly to our wrapGlobalHandler function
}
Run Code Online (Sandbox Code Playgroud)
第2步:现在,wrapGlobalHandler只要有未处理的错误,我们就会被调用.所以在这个函数中做任何你想要的错误.
然后做一些错误:
async function wrapGlobalHandler(error, isFatal){
const stack = parseErrorStack(error);
//do anything with the error here
this.defaultHandler(error, isFatal); //after you're finished, call the defaultHandler so that react-native also gets the error
}
Run Code Online (Sandbox Code Playgroud)
完整代码:
import stacktraceParser from 'stacktrace-parser';
const parseErrorStack = (error) => {
if (!error || !error.stack) {
return [];
}
return Array.isArray(error.stack) ? error.stack :
stacktraceParser.parse(error.stack);
};
// intercept react-native error handling
if (ErrorUtils._globalHandler) {
this.defaultHandler = (ErrorUtils.getGlobalHandler
&& ErrorUtils.getGlobalHandler())
|| ErrorUtils._globalHandler;
ErrorUtils.setGlobalHandler(this.wrapGlobalHandler); // feed errors directly to our wrapGlobalHandler function
}
async function wrapGlobalHandler(error, isFatal) {
const stack = parseErrorStack(error);
//do anything with the error here
this.defaultHandler(error, isFatal); //after you're finished, call the defaultHandler so that react-native also gets the error
}
Run Code Online (Sandbox Code Playgroud)
而已!
您可以尝试https://github.com/master-atul/react-native-exception-handler。
一个react native模块,允许您注册一个全局错误处理程序,该处理程序可以捕获致命/非致命未捕获异常。该模块有助于防止RN Apps突然崩溃,而不会向用户显示优美的消息。
| 归档时间: |
|
| 查看次数: |
20015 次 |
| 最近记录: |