Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly wrap components in traditional HoC pattern #13605

Merged
merged 1 commit into from
Jun 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/sentry/static/sentry/app/utils/errorHandler.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@ import React from 'react';
import RouteError from 'app/views/routeError';

export default function errorHandler(Component) {
const originalRender = Component.prototype.render;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, interesting we were doing this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evanpurkhiser it's super old-school React 😄 see: preactjs/preact#531 (comment)

Component.prototype.render = function() {
try {
return originalRender.apply(this, arguments);
} catch (err) {
/*eslint no-console:0*/
class ErrorHandler extends React.Component {
static getDerivedStateFromError(error) {
setTimeout(() => {
throw err;
throw error;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm wonder if we even need this since RouteError captures errors to Sentry

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@billyvg I tried preserving the implementation as much as I could; since I'm not aware if anything is catching this re-thrown error.

Looks like this PR introduced this setTimeout(): #4443

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I don't think there's anything catching it - I think we only use errorHandler in our routes, so it would probably only bubble up to app/main and app/index. Can we follow up with a PR to see if we remove it? I would say if we throw an error in a component and we have a reasonable stacktrace, it can go.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I'll follow up with a PR to remove it.

});
return <RouteError error={err} component={this} />;

// Update state so the next render will show the fallback UI.
return {
hasError: true,
error,
};
}

state = {
hasError: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a bit redundant? We know error state with state.error

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially thought that the error thrown would always be truthy; but interestingly, that isn't the case since JS lets you throw anything e.g. throw null.

Here's a sample react app showcasing that error can be a falsy value received by static getDerivedStateFromError(error): https://codesandbox.io/s/mdq0z?fontsize=14

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, that's an additional issue, we should never be throwing null, but this is 👍

error: null,
};

render() {
if (this.state.hasError) {
return <RouteError error={this.state.error} />;
}

return <Component {...this.props} />;
}
};
return Component;
}

return ErrorHandler;
}