|
| 1 | +import tracking, { RejectionTrackingOptions } from 'promise/setimmediate/rejection-tracking'; |
| 2 | +import { sendCrashReport } from './InstabugUtils'; |
| 3 | +import { NativeCrashReporting } from '../native/NativeCrashReporting'; |
| 4 | + |
| 5 | +export interface HermesInternalType { |
| 6 | + enablePromiseRejectionTracker?: (options?: RejectionTrackingOptions) => void; |
| 7 | + hasPromise?: () => boolean; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * A typed version of the `HermesInternal` global object with the properties |
| 12 | + * we use. |
| 13 | + */ |
| 14 | +function _getHermes(): HermesInternalType | null { |
| 15 | + return (global as any).HermesInternal; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Checks whether the Promise object is provided by Hermes. |
| 20 | + * |
| 21 | + * @returns whether the `Promise` object is provided by Hermes. |
| 22 | + */ |
| 23 | +function _isHermesPromise() { |
| 24 | + const hermes = _getHermes(); |
| 25 | + const hasPromise = hermes?.hasPromise?.() === true; |
| 26 | + const canTrack = hermes?.enablePromiseRejectionTracker != null; |
| 27 | + |
| 28 | + return hasPromise && canTrack; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Enables unhandled Promise rejection tracking in Hermes. |
| 33 | + * |
| 34 | + * @param options Rejection tracking options. |
| 35 | + */ |
| 36 | +function _enableHermesRejectionTracking(options?: RejectionTrackingOptions) { |
| 37 | + const hermes = _getHermes(); |
| 38 | + |
| 39 | + hermes!.enablePromiseRejectionTracker!(options); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Enables unhandled Promise rejection tracking in the default `promise` polyfill. |
| 44 | + * |
| 45 | + * @param options Rejection tracking options. |
| 46 | + */ |
| 47 | +function _enableDefaultRejectionTracking(options?: RejectionTrackingOptions) { |
| 48 | + tracking.enable(options); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Tracks whether an unhandled Promise rejection happens and reports it. |
| 53 | + */ |
| 54 | +export function captureUnhandledRejections() { |
| 55 | + const options: RejectionTrackingOptions = { |
| 56 | + allRejections: true, |
| 57 | + onUnhandled: _onUnhandled, |
| 58 | + }; |
| 59 | + |
| 60 | + if (_isHermesPromise()) { |
| 61 | + _enableHermesRejectionTracking(options); |
| 62 | + } else { |
| 63 | + _enableDefaultRejectionTracking(options); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * The callback passed in the rejection tracking options to report unhandled |
| 69 | + * Promise rejection |
| 70 | + */ |
| 71 | +function _onUnhandled(id: number, rejection: unknown) { |
| 72 | + _originalOnUnhandled(id, rejection); |
| 73 | + |
| 74 | + if (__DEV__) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (rejection instanceof Error) { |
| 79 | + sendCrashReport(rejection, NativeCrashReporting.sendHandledJSCrash); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/* istanbul ignore next */ |
| 84 | +/** |
| 85 | + * The default unhandled promise rejection handler set by React Native. |
| 86 | + * |
| 87 | + * In fact, this is copied from the React Native repo but modified to work well |
| 88 | + * with our static analysis setup. |
| 89 | + * |
| 90 | + * https://github.com/facebook/react-native/blob/f2447e6048a6b519c3333767d950dbf567149b75/packages/react-native/Libraries/promiseRejectionTrackingOptions.js#L15-L49 |
| 91 | + */ |
| 92 | +function _originalOnUnhandled(id: number, rejection: unknown = {}) { |
| 93 | + let message: string; |
| 94 | + let stack: string | undefined; |
| 95 | + |
| 96 | + const stringValue = Object.prototype.toString.call(rejection); |
| 97 | + if (stringValue === '[object Error]') { |
| 98 | + message = Error.prototype.toString.call(rejection); |
| 99 | + const error = rejection as Error; |
| 100 | + stack = error.stack; |
| 101 | + } else { |
| 102 | + try { |
| 103 | + message = require('pretty-format')(rejection); |
| 104 | + } catch { |
| 105 | + message = typeof rejection === 'string' ? rejection : JSON.stringify(rejection); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + const warning = |
| 110 | + `Possible Unhandled Promise Rejection (id: ${id}):\n` + |
| 111 | + `${message ?? ''}\n` + |
| 112 | + (stack == null ? '' : stack); |
| 113 | + console.warn(warning); |
| 114 | +} |
0 commit comments