From 07a304cbc17839f1e081fbcd6e2d4f718db0be89 Mon Sep 17 00:00:00 2001 From: slice Date: Mon, 16 Jul 2018 18:06:06 -0700 Subject: [PATCH] properly handle fetch errors --- priv/frontend/src/components/App.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/priv/frontend/src/components/App.js b/priv/frontend/src/components/App.js index e6aeb02..d6c4507 100644 --- a/priv/frontend/src/components/App.js +++ b/priv/frontend/src/components/App.js @@ -151,13 +151,23 @@ export default class App extends Component { log('loading metrics') try { - const resp = await fetch(`${DOMAIN}/api/status`) - const json = await resp.json() - - this.setState({ metrics: json, loading: false }) + var resp = await fetch(`${DOMAIN}/api/status`) } catch (err) { - this.setState({ error: err.toString() }) + this.setState({ + error: `Network error: ${err}`, + }) } + + if (!resp.ok) { + this.setState({ + error: `Failed to fetch stats (${resp.status} ${resp.statusText})`, + }) + return + } + + const json = await resp.json() + + this.setState({ metrics: json, loading: false }) } render () {