|
| 1 | +import { isFunction, isNumber, isString } from '@mpxjs/utils' |
1 | 2 | import { createI18n } from '../builtInMixins/i18nMixin'
|
2 | 3 |
|
3 | 4 | export function init (Mpx) {
|
@@ -30,21 +31,30 @@ function initGlobalErrorHandling () {
|
30 | 31 | })
|
31 | 32 | }
|
32 | 33 |
|
| 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 | + } |
33 | 43 | const rejectionTrackingOptions = {
|
34 | 44 | allRejections: true,
|
35 | 45 | 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 }) |
43 | 47 | }
|
44 | 48 | }
|
45 | 49 |
|
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) |
48 | 58 | } else {
|
49 | 59 | require('promise/setimmediate/rejection-tracking').enable(rejectionTrackingOptions)
|
50 | 60 | }
|
|
0 commit comments