diff --git a/templates/base.html b/templates/base.html index 656d7a4..c7c7a56 100644 --- a/templates/base.html +++ b/templates/base.html @@ -107,13 +107,27 @@ var promEl = document.getElementById('source-status-prometheus'); var osEl = document.getElementById('source-status-openstack'); if (!promEl || !osEl) return; - fetch('/api/source-status/').then(function(r) { return r.ok ? r.json() : {}; }).then(function(data) { - updateSourceStatus(promEl, 'Prometheus', data.prometheus); - updateSourceStatus(osEl, 'OpenStack', data.openstack); - }).catch(function() { - updateSourceStatus(promEl, 'Prometheus', { status: 'error', message: 'Failed to fetch status' }); - updateSourceStatus(osEl, 'OpenStack', { status: 'error', message: 'Failed to fetch status' }); - }); + fetch('/api/source-status/') + .then(function(r) { + if (r.ok) return r.json().then(function(data) { return { data: data }; }); + return r.json().catch(function() { return {}; }).then(function(body) { + return { error: true, message: (body && body.message) || 'Failed to fetch status' }; + }); + }) + .then(function(result) { + if (result && result.error) { + updateSourceStatus(promEl, 'Prometheus', { status: 'error', message: result.message }); + updateSourceStatus(osEl, 'OpenStack', { status: 'error', message: result.message }); + } else { + var data = result && result.data; + updateSourceStatus(promEl, 'Prometheus', data && data.prometheus); + updateSourceStatus(osEl, 'OpenStack', data && data.openstack); + } + }) + .catch(function() { + updateSourceStatus(promEl, 'Prometheus', { status: 'error', message: 'Failed to fetch status' }); + updateSourceStatus(osEl, 'OpenStack', { status: 'error', message: 'Failed to fetch status' }); + }); }); })();