Skip to content

Commit 62b48fe

Browse files
authored
Merge pull request #1822 from didi/feat-polyfill-UnhandledRejection
feat: onUnhandledRejection 支持core-js promise polyfill
2 parents 4c7e190 + 36ec118 commit 62b48fe

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

packages/core/src/platform/env/index.ios.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isFunction, isNumber, isString } from '@mpxjs/utils'
12
import { createI18n } from '../builtInMixins/i18nMixin'
23

34
export function init (Mpx) {
@@ -30,21 +31,30 @@ function initGlobalErrorHandling () {
3031
})
3132
}
3233

34+
function onUnhandledRejection (event) {
35+
if (global.__mpxAppCbs && global.__mpxAppCbs.rejection && global.__mpxAppCbs.rejection.length) {
36+
global.__mpxAppCbs.rejection.forEach((cb) => {
37+
cb(event)
38+
})
39+
} else {
40+
console.warn(`UNHANDLED PROMISE REJECTION ${(isNumber(event.id) || isString(event.id)) ? '(id:' + event.id + ')' : ''}: ${event.reason}\n`)
41+
}
42+
}
3343
const rejectionTrackingOptions = {
3444
allRejections: true,
3545
onUnhandled (id, error) {
36-
if (global.__mpxAppCbs && global.__mpxAppCbs.rejection && global.__mpxAppCbs.rejection.length) {
37-
global.__mpxAppCbs.rejection.forEach((cb) => {
38-
cb(error, id)
39-
})
40-
} else {
41-
console.warn(`UNHANDLED PROMISE REJECTION (id: ${id}): ${error}\n`)
42-
}
46+
onUnhandledRejection({ id, reason: error, promise: null })
4347
}
4448
}
4549

46-
if (global?.HermesInternal?.hasPromise?.()) {
47-
global.HermesInternal?.enablePromiseRejectionTracker?.(rejectionTrackingOptions)
50+
// 支持 core-js promise polyfill
51+
const oldOnUnhandledRejection = global.onunhandledrejection
52+
global.onunhandledrejection = function onunhandledrejection (event) {
53+
onUnhandledRejection(event)
54+
isFunction(oldOnUnhandledRejection) && oldOnUnhandledRejection.call(this, event)
55+
}
56+
if (global.HermesInternal?.hasPromise?.()) {
57+
global.HermesInternal.enablePromiseRejectionTracker?.(rejectionTrackingOptions)
4858
} else {
4959
require('promise/setimmediate/rejection-tracking').enable(rejectionTrackingOptions)
5060
}

packages/core/src/platform/env/index.web.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function initGlobalErrorHandling () {
3838
window.addEventListener('unhandledrejection', (event) => {
3939
if (global.__mpxAppCbs && global.__mpxAppCbs.rejection && global.__mpxAppCbs.rejection.length) {
4040
global.__mpxAppCbs.rejection.forEach((cb) => {
41-
cb(event.reason, event.promise)
41+
cb(event)
4242
})
4343
} else {
4444
console.warn(`UNHANDLED PROMISE REJECTION: ${event.reason}\n`)

0 commit comments

Comments
 (0)