-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ui: Add error boundary in the React UI (#2759)
* ui: Add ErrorBoundary to the React UI Signed-off-by: Prem Kumar <prmsrswt@gmail.com> * query: Return empty array instead of nil from API Signed-off-by: Prem Kumar <prmsrswt@gmail.com> * ui: Fix typo; remove unnecessary inline style Signed-off-by: Prem Kumar <prmsrswt@gmail.com> * format bindata.go Signed-off-by: Prem Kumar <prmsrswt@gmail.com>
- Loading branch information
Showing
6 changed files
with
148 additions
and
74 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
17 changes: 17 additions & 0 deletions
17
pkg/ui/react-app/src/thanos/pages/errorBoundary/ErrorBoundary.module.css
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.container { | ||
display: flex; | ||
min-height: 100vh; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
|
||
button.detailsBtn { | ||
font-size: 1.2em; | ||
margin: 2em 0; | ||
} | ||
|
||
.errorDiv { | ||
white-space: pre-wrap; | ||
font-family: monospace; | ||
} |
50 changes: 50 additions & 0 deletions
50
pkg/ui/react-app/src/thanos/pages/errorBoundary/ErrorBoundary.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, { ErrorInfo } from 'react'; | ||
import { Container, UncontrolledCollapse, Button } from 'reactstrap'; | ||
import styles from './ErrorBoundary.module.css'; | ||
|
||
interface ErrorState { | ||
error: Error | null; | ||
errorInfo: ErrorInfo | null; | ||
} | ||
|
||
class ErrorBoundary extends React.Component<{}, ErrorState> { | ||
constructor(props: {}) { | ||
super(props); | ||
this.state = { | ||
error: null, | ||
errorInfo: null, | ||
}; | ||
} | ||
|
||
componentDidCatch(error: Error, errorInfo: ErrorInfo): void { | ||
this.setState({ | ||
error, | ||
errorInfo, | ||
}); | ||
} | ||
render(): React.ReactNode { | ||
if (this.state.errorInfo) { | ||
return ( | ||
<Container fluid className={styles.container}> | ||
<h1>Aaaah! Something went wrong.</h1> | ||
<h3> | ||
Please file an issue in the | ||
<a href="https://github.com/thanos-io/thanos/issues" target="_blank" rel="noreferrer noopener"> | ||
Thanos issue tracker. | ||
</a> | ||
</h3> | ||
<Button color="link" id="error-details-toggler" className={styles.detailsBtn}> | ||
View error details. | ||
</Button> | ||
<UncontrolledCollapse toggler="#error-details-toggler" className={styles.errorDiv}> | ||
<span>{this.state.error && this.state.error.toString()}</span> | ||
{this.state.errorInfo.componentStack} | ||
</UncontrolledCollapse> | ||
</Container> | ||
); | ||
} | ||
return this.props.children; | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export {}; | ||
import ErrorBoundary from './errorBoundary/ErrorBoundary'; | ||
|
||
export { ErrorBoundary }; |