Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: Add error boundary in the React UI #2759

Merged
merged 4 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/query/api/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,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>
onprem marked this conversation as resolved.
Show resolved Hide resolved
<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 };