native 捕获异步错误

This commit is contained in:
wkj 2024-05-20 13:34:42 +08:00
parent 3e0a731420
commit 11b88c67be
4 changed files with 55 additions and 29 deletions

View File

@ -1,18 +0,0 @@
/**
* App也必须输入访问的目标域名domain和system的关系来判定appId
*/
const env = process.env.NODE_ENV;
const URL = {
// 服务器地址数组和application的domain中要保持一致以确定application
development: 'localhost',
staging: 'test.com',
production: 'test.com',
};
const host = URL[env];
export {
host,
};

View File

@ -8,6 +8,7 @@ import { setJSExceptionHandler } from 'react-native-exception-handler';
import features from './initialize';
import App from './App';
import { name as appName } from './app.json';
import {setPromiseUnCatchHandler} from './promiseTracker';
import { handler as exceptionHandler } from '@project/exceptionHandler';
import {
@ -15,9 +16,13 @@ import {
} from 'oak-frontend-base/es/platforms/native/features';
// to 详化
setJSExceptionHandler((exception) => {
exceptionHandler(exception, features);
});
setJSExceptionHandler(exception => {
exceptionHandler(exception, features);
}, true);
setPromiseUnCatchHandler((id, exception) => {
exceptionHandler(exception, features);
}, true);
function Root() {
features.navigator.setNamespace('/frontend');

View File

@ -0,0 +1,46 @@
export type PromiseUnCatchHandler = (id: string, error: Error) => void;
const noop: PromiseUnCatchHandler = () => { };
/**
*
* @param {Function} customHandler
* @param {Boolean} allowedInDevMode
* @returns
*/
export const setPromiseUnCatchHandler = (customHandler: PromiseUnCatchHandler = noop, allowedInDevMode = false) => {
if (typeof customHandler !== 'function' || typeof allowedInDevMode !== 'boolean') {
console.log('setPromiseUnCatchHandler is called with wrong argument types.. first argument should be callback function and second argument is optional should be a boolean');
console.log('Not setting the JS handler .. please fix setPromiseUnCatchHandler call');
return;
}
const customRejectionTrackingOptions = {
allRejections: true,
onUnhandled: customHandler,
onHandled: (id: string) => {
const warning =
`Promise Rejection Handled (id: ${id})\n` +
'This means you can ignore any previous messages of the form ' +
`"Possible Unhandled Promise Rejection (id: ${id}):"`;
console.warn(warning);
},
};
// @ts-ignore
const __DEV__ = process.env.NODE_ENV === 'development';
const allowed = allowedInDevMode ? true : !__DEV__;
if (allowed) {
// @ts-ignore
if (global?.HermesInternal?.hasPromise?.()) {
// @ts-ignore
global.HermesInternal?.enablePromiseRejectionTracker?.(
customRejectionTrackingOptions,
);
} else {
require('promise/setimmediate/rejection-tracking').enable(
customRejectionTrackingOptions,
);
}
} else {
console.log('Skipping setPromiseUnCatchHandler: Reason: In DEV mode and allowedInDevMode = false');
}
};

View File

@ -5,16 +5,9 @@
const accountInfo = wx.getAccountInfoSync();
const envVersion = accountInfo.miniProgram.envVersion; //develop\trial\release
const URL = { // 服务器地址数组和domain中要保持一致以界定application
develop: 'localhost',
trial: 'dev.oak-app-id.com',
release: 'www.oak-app-id.com',
};
const host = URL[envVersion];
const sdkVersion = '2.25.1'; // 小程序运行所需要的sdk最低版本
export {
host,
envVersion,
sdkVersion,
};