|
420 | 420 | } |
421 | 421 | } |
422 | 422 |
|
| 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 | + |
423 | 445 | if (globalThis.__engine === "V8") { |
424 | 446 | // Only report errors for promise rejections that go unhandled. |
425 | 447 | globalThis.onUnhandledPromiseRejectionTracker = ( |
|
429 | 451 | ) => { |
430 | 452 | if (event === globalThis.__promiseUnhandledEvent) { |
431 | 453 | 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); |
436 | 455 | unhandledPromise(promise, error); |
437 | 456 | } else { |
438 | 457 | handledPromise(promise); |
|
444 | 463 | reason, |
445 | 464 | isHandled |
446 | 465 | ) => { |
447 | | - |
448 | 466 | if (!isHandled) { |
449 | 467 | 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); |
454 | 469 | // Preserve original stack trace. |
455 | 470 | if (!promise.then["[[stack]]"]) { |
456 | 471 | promise.then["[[stack]]"] = error.stack; |
|
462 | 477 | handledPromise(promise); |
463 | 478 | } |
464 | 479 | }; |
| 480 | + } else if (globalThis.__engine === "Hermes") { |
| 481 | + HermesInternal.enablePromiseRejectionTracker({ |
| 482 | + allRejections: true, |
| 483 | + onUnhandled: function (id, error) { |
| 484 | + globalThis.__onUncaughtError(makeRejectionError(error)); |
| 485 | + }, |
| 486 | + }); |
465 | 487 | } |
466 | 488 | })(); |
0 commit comments