Skip to content

Commit

Permalink
ui: Add error boundary in the React UI (#2759)
Browse files Browse the repository at this point in the history
* 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
onprem authored Jun 17, 2020
1 parent 6b882af commit 8439d36
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 74 deletions.
4 changes: 4 additions & 0 deletions pkg/query/api/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ func (api *API) labelValues(r *http.Request) (interface{}, []error, *ApiError) {
return nil, nil, &ApiError{errorExec, err}
}

if vals == nil {
vals = make([]string, 0)
}

return vals, warnings, nil
}

Expand Down
142 changes: 71 additions & 71 deletions pkg/ui/bindata.go

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pkg/ui/react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { Alerts, Config, Flags, Rules, ServiceDiscovery, Status, Targets, TSDBSt
import PathPrefixProps from './types/PathPrefixProps';
import ThanosComponentProps from './thanos/types/ThanosComponentProps';
import Navigation from './thanos/Navbar';
import { ErrorBoundary } from './thanos/pages';

import './App.css';

const App: FC<PathPrefixProps & ThanosComponentProps> = ({ pathPrefix, thanosComponent }) => {
return (
<>
<ErrorBoundary>
<Navigation pathPrefix={pathPrefix} thanosComponent={thanosComponent} />
<Container fluid style={{ paddingTop: 70 }}>
<Router basepath={`${pathPrefix}/new`}>
Expand All @@ -32,7 +33,7 @@ const App: FC<PathPrefixProps & ThanosComponentProps> = ({ pathPrefix, thanosCom
<Targets path="/targets" pathPrefix={pathPrefix} />
</Router>
</Container>
</>
</ErrorBoundary>
);
};

Expand Down
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 pkg/ui/react-app/src/thanos/pages/errorBoundary/ErrorBoundary.tsx
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&nbsp;
<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;
4 changes: 3 additions & 1 deletion pkg/ui/react-app/src/thanos/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export {};
import ErrorBoundary from './errorBoundary/ErrorBoundary';

export { ErrorBoundary };

0 comments on commit 8439d36

Please sign in to comment.