-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b091fe9
commit 1f00cb3
Showing
3 changed files
with
78 additions
and
81 deletions.
There are no files selected for viewing
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
121 changes: 63 additions & 58 deletions
121
superset-frontend/src/components/ErrorBoundary/index.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,84 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { Component, ErrorInfo, ReactNode } from 'react'; | ||
|
||
import { Component, ReactNode, ErrorInfo, useState } from 'react'; | ||
import { t } from '@superset-ui/core'; | ||
import ErrorMessageWithStackTrace from 'src/components/ErrorMessage/ErrorMessageWithStackTrace'; | ||
import ErrorAlert from 'src/components/ErrorMessage/ErrorAlert'; | ||
|
||
export interface ErrorBoundaryProps { | ||
interface ErrorBoundaryProps { | ||
children: ReactNode; | ||
onError?: (error: Error, info: ErrorInfo) => void; | ||
showMessage?: boolean; | ||
} | ||
|
||
interface ErrorBoundaryState { | ||
error: Error | null; | ||
info: ErrorInfo | null; | ||
} | ||
|
||
export default class ErrorBoundary extends Component< | ||
ErrorBoundaryProps, | ||
ErrorBoundaryState | ||
> { | ||
static defaultProps: Partial<ErrorBoundaryProps> = { | ||
showMessage: true, | ||
}; | ||
|
||
constructor(props: ErrorBoundaryProps) { | ||
super(props); | ||
this.state = { error: null, info: null }; | ||
/** | ||
* A class component for handling React error boundaries. | ||
*/ | ||
class ErrorBoundaryFallback extends Component<ErrorBoundaryProps> { | ||
componentDidCatch(error: Error, info: ErrorInfo) { | ||
const { onError } = this.props; | ||
if (onError) { | ||
onError(error, info); | ||
} | ||
} | ||
|
||
componentDidCatch(error: Error, info: ErrorInfo): void { | ||
this.props.onError?.(error, info); | ||
this.setState({ error, info }); | ||
render() { | ||
return this.props.children; | ||
} | ||
} | ||
|
||
render() { | ||
const { error, info } = this.state; | ||
if (error) { | ||
const firstLine = error.toString(); | ||
const messageString = `${t('Unexpected error')}${ | ||
firstLine ? `: ${firstLine}` : '' | ||
}`; | ||
const messageElement = ( | ||
<span> | ||
<strong>{t('Unexpected error')}</strong> | ||
{firstLine ? `: ${firstLine}` : ''} | ||
</span> | ||
); | ||
/** | ||
* ErrorBoundary component to handle unexpected errors and display an error message if needed. | ||
*/ | ||
const ErrorBoundary: React.FC<ErrorBoundaryProps> = ({ | ||
children, | ||
onError, | ||
showMessage = true, | ||
}) => { | ||
const [error, setError] = useState<Error | null>(null); | ||
const [info, setInfo] = useState<ErrorInfo | null>(null); | ||
|
||
if (this.props.showMessage) { | ||
return ( | ||
<ErrorMessageWithStackTrace | ||
subtitle={messageElement} | ||
copyText={messageString} | ||
stackTrace={info?.componentStack} | ||
/> | ||
); | ||
} | ||
return null; | ||
const handleError = (caughtError: Error, caughtInfo: ErrorInfo) => { | ||
setError(caughtError); | ||
setInfo(caughtInfo); | ||
if (onError) { | ||
onError(caughtError, caughtInfo); | ||
} | ||
return this.props.children; | ||
}; | ||
|
||
if (error) { | ||
const errorMessage = error.toString(); | ||
if (showMessage) { | ||
return ( | ||
<ErrorAlert | ||
errorType={t('Unexpected error')} | ||
message={errorMessage} | ||
descriptionDetails={info?.componentStack || errorMessage} | ||
/> | ||
); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
return ( | ||
<ErrorBoundaryFallback handleError={handleError}> | ||
{children} | ||
</ErrorBoundaryFallback> | ||
); | ||
}; | ||
|
||
export default ErrorBoundary; |
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