fix: introduce Error Boundaries to handle unexpected failures (#7671)

Two new components have been updated via plugin system: ErrorBoundary and Fallback.
These components can be overridden by user plugins.

Refs #7647
This commit is contained in:
Vladimir Gorej
2021-11-25 13:47:22 +01:00
committed by GitHub
parent fd22564598
commit b299be764f
8 changed files with 157 additions and 89 deletions

View File

@@ -28,6 +28,7 @@ export default class BaseLayout extends React.Component {
const SchemesContainer = getComponent("SchemesContainer", true)
const AuthorizeBtnContainer = getComponent("AuthorizeBtnContainer", true)
const FilterContainer = getComponent("FilterContainer", true)
const ErrorBoundary = getComponent("ErrorBoundary", true)
let isSwagger2 = specSelectors.isSwagger2()
let isOAS3 = specSelectors.isOAS3()
@@ -36,7 +37,7 @@ export default class BaseLayout extends React.Component {
const loadingStatus = specSelectors.loadingStatus()
let loadingMessage = null
if(loadingStatus === "loading") {
loadingMessage = <div className="info">
<div className="loading-container">
@@ -85,8 +86,8 @@ export default class BaseLayout extends React.Component {
const hasSecurityDefinitions = !!specSelectors.securityDefinitions()
return (
<div className='swagger-ui'>
<ErrorBoundary targetName="BaseLayout">
<SvgAssets />
<VersionPragmaFilter isSwagger2={isSwagger2} isOAS3={isOAS3} alsoShow={<Errors/>}>
<Errors/>
@@ -119,7 +120,8 @@ export default class BaseLayout extends React.Component {
</Col>
</Row>
</VersionPragmaFilter>
</div>
)
</ErrorBoundary>
</div>
)
}
}

View File

@@ -0,0 +1,45 @@
import PropTypes from "prop-types"
import React, { Component } from "react"
import Fallback from "./fallback"
export class ErrorBoundary extends Component {
constructor(props) {
super(props)
this.state = { hasError: false, error: null }
}
static getDerivedStateFromError(error) {
return { hasError: true, error }
}
componentDidCatch(error, errorInfo) {
console.error(error, errorInfo) // eslint-disable-line no-console
}
render() {
const { getComponent, targetName, children } = this.props
const FallbackComponent = getComponent("Fallback")
if (this.state.hasError) {
return <FallbackComponent name={targetName} />
}
return children
}
}
ErrorBoundary.propTypes = {
targetName: PropTypes.string,
getComponent: PropTypes.func,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
])
}
ErrorBoundary.defaultProps = {
targetName: "this component",
getComponent: () => Fallback,
children: null,
}
export default ErrorBoundary

View File

@@ -0,0 +1,13 @@
import React from "react"
import PropTypes from "prop-types"
const Fallback = ({ name }) => (
<div className="fallback">
😱 <i>Could not render { name === "t" ? "this component" : name }, see the console.</i>
</div>
)
Fallback.propTypes = {
name: PropTypes.string.isRequired,
}
export default Fallback

View File

@@ -1,6 +1,9 @@
import * as rootInjects from "./root-injects"
import { memoize } from "core/utils"
import ErrorBoundary from "./error-boundary"
import Fallback from "./fallback"
export default function({getComponents, getStore, getSystem}) {
let { getComponent, render, makeMappedContainer } = rootInjects
@@ -14,6 +17,10 @@ export default function({getComponents, getStore, getSystem}) {
getComponent: memGetComponent,
makeMappedContainer: memMakeMappedContainer,
render: render.bind(null, getSystem, getStore, getComponent, getComponents),
}
},
components: {
ErrorBoundary,
Fallback,
},
}
}

View File

@@ -1,20 +1,24 @@
import React, { Component } from "react"
import PropTypes from "prop-types"
import ReactDOM from "react-dom"
import { connect, Provider } from "react-redux"
import omit from "lodash/omit"
const SystemWrapper = (getSystem, ComponentToWrap ) => class extends Component {
render() {
return <ComponentToWrap {...getSystem() } {...this.props} {...this.context} />
return <ComponentToWrap {...getSystem()} {...this.props} {...this.context} />
}
}
const RootWrapper = (reduxStore, ComponentToWrap) => class extends Component {
const RootWrapper = (getSystem, reduxStore, ComponentToWrap) => class extends Component {
render() {
const { getComponent } = getSystem()
const ErrorBoundary = getComponent("ErrorBoundary", true)
return (
<Provider store={reduxStore}>
<ComponentToWrap {...this.props} {...this.context} />
<ErrorBoundary targetName={ComponentToWrap?.name}>
<ComponentToWrap {...this.props} {...this.context} />
</ErrorBoundary>
</Provider>
)
}
@@ -30,7 +34,7 @@ const makeContainer = (getSystem, component, reduxStore) => {
let wrappedWithSystem = SystemWrapper(getSystem, component, reduxStore)
let connected = connect( mapStateToProps )(wrappedWithSystem)
if(reduxStore)
return RootWrapper(reduxStore, connected)
return RootWrapper(getSystem, reduxStore, connected)
return connected
}
@@ -66,73 +70,43 @@ export const makeMappedContainer = (getSystem, getStore, memGetComponent, getCom
}
export const render = (getSystem, getStore, getComponent, getComponents, domNode) => {
let App = (getComponent(getSystem, getStore, getComponents, "App", "root"))
ReactDOM.render(( <App/> ), domNode)
let App = getComponent(getSystem, getStore, getComponents, "App", "root")
ReactDOM.render(<App/>, domNode)
}
class ErrorBoundary extends Component {
constructor(props) {
super(props)
this.state = { hasError: false, error: null }
}
static getDerivedStateFromError(error) {
return { hasError: true, error }
}
componentDidCatch(error, errorInfo) {
console.error(error, errorInfo) // eslint-disable-line no-console
}
/**
* Creates a class component from a stateless one and wrap it with Error Boundary
* to handle errors coming from a stateless component.
*/
const createClass = (getSystem, OriginalComponent) => class extends Component {
render() {
if (this.state.hasError) {
return <Fallback name={this.props.targetName} />
}
const { getComponent } = getSystem()
const ErrorBoundary = getComponent("ErrorBoundary")
return this.props.children
}
}
ErrorBoundary.propTypes = {
targetName: PropTypes.string,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
])
}
ErrorBoundary.defaultProps = {
targetName: "this component",
children: null,
}
const Fallback = ({ name }) => (
<div className="fallback">
😱 <i>Could not render { name === "t" ? "this component" : name }, see the console.</i>
</div>
)
Fallback.propTypes = {
name: PropTypes.string.isRequired,
}
// Render try/catch wrapper
const createClass = OriginalComponent => class extends Component {
render() {
return (
<ErrorBoundary targetName={OriginalComponent?.name}>
<ErrorBoundary targetName={OriginalComponent?.name} getComponent={getComponent}>
<OriginalComponent {...this.props} />
</ErrorBoundary>
)
}
}
const wrapRender = (component) => {
const wrapRender = (getSystem, component) => {
const isStateless = component => !(component.prototype && component.prototype.isReactComponent)
const target = isStateless(component) ? createClass(component) : component
const target = isStateless(component) ? createClass(getSystem, component) : component
const { render: oriRender} = target.prototype
/**
* This render method override handles errors that are throw in render method
* of class components.
*/
target.prototype.render = function render(...args) {
try {
return oriRender.apply(this, args)
} catch (error) {
const { getComponent } = getSystem()
const Fallback = getComponent("Fallback")
console.error(error) // eslint-disable-line no-console
return <Fallback name={target.name} />
}
@@ -159,11 +133,11 @@ export const getComponent = (getSystem, getStore, getComponents, componentName,
}
if(!container)
return wrapRender(component)
return wrapRender(getSystem, component)
if(container === "root")
return makeContainer(getSystem, component, getStore())
// container == truthy
return makeContainer(getSystem, wrapRender(component))
return makeContainer(getSystem, wrapRender(getSystem, component))
}