Created
December 18, 2018 10:02
-
-
Save xeviknal/a0deb0aee1b9c8a20df52f7aee37fb68 to your computer and use it in GitHub Desktop.
HOC for error boundaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<TabContent> | |
<TabPaneWithErrorBoundary message={this.errorBoundaryMessage('Workloads')}> | |
{(Object.keys(workloads).length > 0 || this.props.serviceDetails.istioSidecar) && ( | |
<ServiceInfoWorkload workloads={workloads} namespace={this.props.namespace} /> | |
)} | |
</TabPaneWithErrorBoundary> | |
<TabPaneWithErrorBoundary eventKey={'sources'} message={this.errorBoundaryMessage('Sources')}> | |
<ServiceInfoRoutes dependencies={dependencies} /> | |
</TabPaneWithErrorBoundary> | |
<TabPaneWithErrorBoundary | |
eventKey={'virtualservices'} | |
message={this.errorBoundaryMessage('Virtual Services')} | |
> | |
{this.renderImportantThings()} | |
</TabPaneWithErrorBoundary> | |
<TabPaneWithErrorBoundary | |
eventKey={'destinationrules'} | |
message={this.errorBoundaryMessage('Destination Rules')} | |
> | |
<AnotherComponent /> | |
</TabPaneWithErrorBoundary> | |
</TabContent> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import { Alert, TabPane } from 'patternfly-react'; | |
import ErrorBoundary from '../../components/ErrorBoundary/ErrorBoundary'; | |
interface WithErrorBoundaryProps { | |
message: string; | |
} | |
const withErrorBoundary = <P extends object>(Component: React.ComponentType<P>) => { | |
return class extends React.Component<P & WithErrorBoundaryProps> { | |
wrongFormatMessage() { | |
const { message, ...props} = this.props as WithErrorBoundaryProps; | |
const displayingMessage = message || 'Something went wrong rending this component'; | |
return ( | |
<div className="card-pf-body"> | |
<Alert type="warning">{displayingMessage}</Alert> | |
</div> | |
); | |
} | |
render() { | |
return ( | |
<Component {...this.props}> | |
<ErrorBoundary fallBackComponent={this.wrongFormatMessage()}> | |
{this.props.children} | |
</ErrorBoundary> | |
</Component> | |
); | |
} | |
}; | |
}; | |
const TabPaneWithErrorBoundary = withErrorBoundary(TabPane); | |
export default TabPaneWithErrorBoundary; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What do you thinbk about ??