hermes: Many libraries can cause "Error.stack getter called with an invalid receiver"
Bug Description
When an error is thrown while rendering, RN crashes of course as expected. However, for some errors, when ExceptionsManager calls error.stack, it throws new errors, masking the original errors, which makes it impossible to know what the original error was for users in production (using tools like sentry).
This happens when libraries using a technique like this to create their error classes:
const MyError = function(message) {
this.message = message;
};
MyError.prototype = new Error;
MyError.prototype.name = 'MyError';
I can find at least 5 popular libraries that use this technique, and if it works in classic JS, I think it’s supposed to work with hermes?
- The issue is reproducible with the latest version of React Native.
React Native version: 0.73.5 OS: iOS, Android
Steps To Reproduce
useEffect(() => {
const MyError = function(message) {
this.message = message;
};
MyError.prototype = new Error;
MyError.prototype.name = 'MyError';
throw new MyError('1234')
}, []);
The Expected Behavior
Calling new MyError().stack doesn’t throw Error.stack getter called with an invalid receiver
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 27 (15 by maintainers)
Commits related to this issue
- JSError: look for stack data in the prototype chain Summary: Look for stack data in the entire prototype chain in order to accommodate usage like the one in the test. See https://github.com/facebook... — committed to tmikov/hermes by deleted user 2 years ago
- JSError: look for stack data in the prototype chain (#1497) Summary: Pull Request resolved: https://github.com/facebook/hermes/pull/1497 Look for stack data in the entire prototype chain in order to... — committed to tmikov/hermes by deleted user 2 years ago
- JSError: look for stack data in the prototype chain (#1497) Summary: Pull Request resolved: https://github.com/facebook/hermes/pull/1497 Look for stack data in the entire prototype chain in order to... — committed to facebook/hermes by deleted user 2 years ago
- JSError: look for stack data in the prototype chain Summary: Imported from static_h Original Author: tmikov@meta.com Original Git: 8b7a9f8fc0d99f14a4673a7d6a973fe761feb582 Original Reviewed By: avp O... — committed to facebook/hermes by deleted user 2 years ago
- fix(pouchdb-error): do not inherit error properties by copying from parent error manually to avoid Hermes from causing an error. See https://github.com/facebook/hermes/issues/1496 — committed to craftzdog/pouchdb-react-native by craftzdog 2 years ago
This is the fix, landing in Static Hermes. The getter returns the stack from the prototype. https://github.com/facebook/hermes/pull/1497
@exzos28 thank you, I see the problem. We will fix it.
Note that it doesn’t “fatal” - it correctly throws an exception that the getter is applied to an incorrect target, namely an Object, when it should be applied to an instance of Error.
@thomasttvo turns out you were right, sorry! 😢
@tmikov It’s not the JS exception that crashes the application, it’s the attempt to read the stack property.
My case is that I have an unhandled
FlowCancellationErrorexception, it goes to the global scrope where Sentry gets it.Next from sentry code:
and the application crashes with an error
Error.stack getter called with an invalid receiver@wobsoriano it will be in RN 0.76