properly handle fetch errors

This commit is contained in:
slice 2018-07-16 18:06:06 -07:00
parent 7a5e39f078
commit 07a304cbc1
No known key found for this signature in database
GPG Key ID: 1508C19D7436A26D
1 changed files with 15 additions and 5 deletions

View File

@ -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 () {