Skip to content

Commit 481851c

Browse files
committed
hermes: add promise rejection handler
1 parent 970a946 commit 481851c

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

test-app/app/src/main/assets/internal/ts_helpers.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,28 @@
420420
}
421421
}
422422

423+
function makeRejectionError(reason) {
424+
const stringValue = Object.prototype.toString.call(reason);
425+
if (
426+
stringValue === "[object Error]" ||
427+
reason instanceof Error ||
428+
typeof reason === "object"
429+
) {
430+
reason.message = `(Unhandled promise rejection): ${reason.message}`;
431+
432+
if (!reason.stack) {
433+
reason.stack = new Error("").stack;
434+
}
435+
return reason;
436+
} else {
437+
const error = new Error(reason, {
438+
cause: reason,
439+
});
440+
error.message = `(Unhandled promise rejection): ${error.message}`;
441+
return error;
442+
}
443+
}
444+
423445
if (globalThis.__engine === "V8") {
424446
// Only report errors for promise rejections that go unhandled.
425447
globalThis.onUnhandledPromiseRejectionTracker = (
@@ -429,10 +451,7 @@
429451
) => {
430452
if (event === globalThis.__promiseUnhandledEvent) {
431453
hasBeenNotifiedProperty.set(promise, false);
432-
const error = new Error(reason, {
433-
cause: reason,
434-
});
435-
error.name = "Unhandled promise rejection";
454+
const error = makeRejectionError(reason);
436455
unhandledPromise(promise, error);
437456
} else {
438457
handledPromise(promise);
@@ -444,13 +463,9 @@
444463
reason,
445464
isHandled
446465
) => {
447-
448466
if (!isHandled) {
449467
hasBeenNotifiedProperty.set(promise, false);
450-
const error = new Error(reason, {
451-
cause: reason,
452-
});
453-
error.name = "Unhandled promise rejection";
468+
const error = makeRejectionError(reason);
454469
// Preserve original stack trace.
455470
if (!promise.then["[[stack]]"]) {
456471
promise.then["[[stack]]"] = error.stack;
@@ -462,5 +477,12 @@
462477
handledPromise(promise);
463478
}
464479
};
480+
} else if (globalThis.__engine === "Hermes") {
481+
HermesInternal.enablePromiseRejectionTracker({
482+
allRejections: true,
483+
onUnhandled: function (id, error) {
484+
globalThis.__onUncaughtError(makeRejectionError(error));
485+
},
486+
});
465487
}
466488
})();

0 commit comments

Comments
 (0)