Skip to content

React (Unhelpfully) Eats Stacktraces #4368

@machineghost

Description

@machineghost

If you have a line of code like the following:

new SomeReactComponent({content: this.props.foo.get('bar')})

and this.props.foo doesn't exist, the React code makes it difficult to debug what's going on.

Normally you can ask the Chrome debugger (and other debuggers as well) to pause on an exception, and it will halt the debugger right on that line. You can then mouseover this.props.foo and see that it is undefined.

However, React has this code:

try {
    renderedComponent = this._renderValidatedComponentWithoutOwnerOrContext();
} finally {
    ReactContext.current = previousContext;
    ReactCurrentOwner.current = null;
}

As you can see, any errors that happen during _renderValidatedComponentWithoutOwnerOrContext are swallowed up, losing both the useful stacktrace and the ability for the browser to debug that code.

I understand React needs to do the two lines in the finally to clean up, but it would be helpful if it could save the exception thrown (eg. try{...} catch (e) { var caughtError = e}) and then rethrow it after it has finished the cleanup (eg. throw (caughtError);).

If for some reason that's impossible, an alternative (but less useful) solution would be to log the error (console && console.error(caughtError)). This would at least provide the developer with the stacktrace, although it wouldn't let them debug the error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions