vah*_*san 6 javascript error-handling android react-native
我正在尝试将JavaScript代码中未处理的异常发送给Crashlytics。我添加了以下代码来捕获任何未处理的JavaScript异常。但是,此代码(console.log或Crashlytics事件)不会因React组件内引发的任何错误而执行。例如,如果throw new ReferenceError()
在主组件的render方法中添加一个内部,我会看到红色的错误框,但是就我的自定义错误处理而言,什么也没有发生。
import { Platform } from 'react-native';
import StackTrace from 'stacktrace-js';
import { Crashlytics } from 'react-native-fabric';
const originalHandler = global.ErrorUtils.getGlobalHandler();
function errorHandler(e, isFatal) {
StackTrace.fromError(e, { offline: true }).then((x) => {
Crashlytics.recordCustomExceptionName(e.message, e.message, x.map(row => (Object.assign({}, row, {
fileName: `${row.fileName}:${row.lineNumber || 0}:${row.columnNumber || 0}`,
}))));
});
console.log('Error caught', e);
// And then re-throw the exception with the original handler
if (originalHandler) {
if (Platform.OS === 'ios') {
originalHandler(e, isFatal);
} else {
// On Android, throwing the original exception immediately results in the
// recordCustomExceptionName() not finishing before the app crashes and therefore not logged
// Add a delay to give it time to log the custom JS exception before crashing the app.
// The user facing effect of this delay is that separate JS errors will appear as separate
// issues in the Crashlytics dashboard.
setTimeout(() => {
originalHandler(e, isFatal);
}, 3000);
}
}
}
global.ErrorUtils.setGlobalHandler(errorHandler);
Run Code Online (Sandbox Code Playgroud)
另外,我注意到,如果我将同一应用程序与签名发行的APK捆绑在一起,则该应用程序不会崩溃。而是显示一个空白屏幕。我尝试了没有自定义错误处理程序的情况,但仍然不会崩溃。如果我在组件内抛出完全相同的错误,则新创建的React Native应用APK会在发布模式下崩溃。这使我认为问题可能出在我添加的任何库上。我已经把我包括package.json
在这里了。目前,我看到的唯一方法是从一个空白项目开始,并逐个添加每个库,直到看到问题为止,但是如果问题出在其他地方,那将是浪费时间。因此,如果有人可以给我提示或建议,以找出根本原因,请多加赞赏。
{
"name": "MyReactNativeApp",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest --coverage",
"postinstall": "react-native-schemes-manager all"
},
"xcodeSchemes": {
"Debug": [
"Debug"
],
"Release": [
"Staging"
]
},
"dependencies": {
"axios": "^0.18.0",
"i18next": "^11.3.1",
"lodash": "^4.17.10",
"moment": "^2.22.1",
"react": "16.3.1",
"react-i18next": "^7.6.1",
"react-native": "0.55.3",
"react-native-config": "^0.11.5",
"react-native-elements": "^0.19.1",
"react-native-fabric": "^0.5.1",
"react-native-sensitive-info": "^5.1.0",
"react-native-vector-icons": "^4.6.0",
"react-navigation": "^2.0.1",
"react-navigation-redux-helpers": "^1.0.6",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"redux-persist": "^5.9.1",
"redux-persist-sensitive-storage": "^1.0.0",
"redux-saga": "^0.16.0",
"reselect": "^3.0.1",
"stacktrace-js": "^2.0.0"
},
"devDependencies": {
"babel-eslint": "^8.2.3",
"babel-jest": "22.4.3",
"babel-preset-react-native": "4.0.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-to-json": "^3.3.3",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-flowtype": "^2.46.3",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"eslint-plugin-react-native": "^3.2.1",
"flow-bin": "^0.71.0",
"jest": "22.4.3",
"react-dom": "^16.3.2",
"react-native-schemes-manager": "^1.0.4",
"react-test-renderer": "16.3.1",
"reactotron-react-native": "^1.14.0",
"reactotron-redux": "^1.13.0",
"reactotron-redux-saga": "^1.13.0",
"redux-devtools-extension": "^2.13.2",
"redux-mock-store": "^1.5.1"
},
"jest": {
"preset": "react-native",
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"setupFiles": [
"<rootDir>/jest/setup.js"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/jest/assetsTransformer.js",
"\\.(css|less)$": "<rootDir>/jest/assetsTransformer.js"
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
550 次 |
最近记录: |