Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Aug 1, 2018
1 parent 0d10ba5 commit 5372dec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
21 changes: 13 additions & 8 deletions src/core_plugins/status_page/public/components/status_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,24 @@ class StatusApp extends Component {
super();
this.state = {
loading: true,
fetchError: false,
data: null
};
}

componentDidMount = async function () {
try {
this.setState({
data: await loadStatus(),
loading: false
});
} catch (e) {
console.error(e);
const data = await loadStatus();

if (data) {
this.setState({ loading: false, data: data });
} else {
this.setState({ fetchError: true, loading: false });
}
}

render() {
const { buildNum, buildSha } = this.props;
const { loading, data } = this.state;
const { loading, fetchError, data } = this.state;

// If we're still loading, return early with a spinner
if (loading) {
Expand All @@ -68,6 +67,12 @@ class StatusApp extends Component {
);
}

if (fetchError) {
return (
<EuiText color="danger">An error occurred loading the status</EuiText>
);
}

// Extract the items needed to render each component
const { metrics, statuses, serverState, name } = data;

Expand Down
26 changes: 22 additions & 4 deletions src/core_plugins/status_page/public/lib/load_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import _ from 'lodash';

import chrome from 'ui/chrome';
import { notify } from 'ui/notify';

// Module-level error returned by notify.error
let errorNotif;

/*
Returns an object of any keys that should be included for metrics.
Expand Down Expand Up @@ -62,12 +66,26 @@ function formatMetrics(data) {
}

async function loadStatus() {
const response = await fetch(chrome.addBasePath('/api/status'), {
method: 'get'
});
// Clear any existing error banner.
if (errorNotif) {
errorNotif.clear();
errorNotif = null;
}

let response;

try {
response = await fetch(chrome.addBasePath('/api/status'), { method: 'get' });
} catch (e) {
// If the fetch failed to connect, display an error and bail.
errorNotif = notify.error('Failed to request server status. Perhaps your server is down?');
return;
}

if (response.status >= 400) {
throw new Error(`Request failed with status code: ${response.status}`);
// If the server does not respond with a successful status, display an error and bail.
errorNotif = notify.error(`Failed to request server status with status code ${response.status}`);
return;
}

const data = await response.json();
Expand Down

0 comments on commit 5372dec

Please sign in to comment.