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

Most upvoted comments

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.

thank you! with that said, if the stack getter returns at test.js:4:25 like above, it would still be really really helpful 🙏 although I know this might be hard to achieve.

@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 FlowCancellationError exception, it goes to the global scrope where Sentry gets it.

Next from sentry code:

...
typeof potentialError.stack === 'string'
...

and the application crashes with an error Error.stack getter called with an invalid receiver

@wobsoriano it will be in RN 0.76