Skip to content

Commit d5dfbb1

Browse files
authored
Ignore hermes stack error (#13786)
1 parent f72ae69 commit d5dfbb1

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

packages/mobile/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* eslint-disable import/first */
22
/* eslint-disable import/order */
3+
// Apply Hermes Error.stack polyfill before any other code - fixes "Error.stack getter
4+
// called with an invalid receiver" from libraries using non-standard error inheritance
5+
require('./src/utils/hermes-error-stack-polyfill').applyHermesErrorStackPolyfill()
6+
37
// Import React first to ensure React internals are available
48
import React from 'react'
59
import 'react-native-gesture-handler'
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Hermes Engine Error.stack Polyfill for React Native 0.78.x
3+
*
4+
* Fixes the "Error.stack getter called with an invalid receiver" bug that occurs
5+
* when libraries use non-standard error inheritance patterns (e.g. Object.create(Error.prototype)
6+
* or prototype = new Error). Hermes throws when accessing .stack on these objects.
7+
*
8+
* References:
9+
* - https://github.com/facebook/react-native/issues/43636
10+
* - https://github.com/facebook/hermes/issues/1496
11+
* - https://github.com/facebook/hermes/pull/1621
12+
*/
13+
export function applyHermesErrorStackPolyfill(): void {
14+
if (typeof global === 'undefined') {
15+
return
16+
}
17+
18+
// Only apply polyfill when running on Hermes engine
19+
const hermesInternal = (global as unknown as { HermesInternal?: unknown })
20+
.HermesInternal
21+
if (!hermesInternal) {
22+
return
23+
}
24+
25+
const descriptor = Object.getOwnPropertyDescriptor(Error.prototype, 'stack')
26+
const originalGetter = descriptor?.get
27+
if (!originalGetter) {
28+
return
29+
}
30+
31+
// Intentional polyfill for Hermes - must modify Error.prototype to fix invalid receiver
32+
// eslint-disable-next-line no-extend-native
33+
Object.defineProperty(Error.prototype, 'stack', {
34+
get: function () {
35+
try {
36+
return originalGetter!.call(this)
37+
} catch {
38+
const message =
39+
this && typeof this === 'object' && 'message' in this
40+
? String((this as { message: unknown }).message)
41+
: 'Unknown error'
42+
return `Error: ${message}\n at (Hermes stack unavailable)`
43+
}
44+
},
45+
configurable: true
46+
})
47+
}

0 commit comments

Comments
 (0)