properly handle fetch errors
This commit is contained in:
parent
7a5e39f078
commit
07a304cbc1
1 changed files with 15 additions and 5 deletions
|
@ -151,13 +151,23 @@ export default class App extends Component {
|
|||
log('loading metrics')
|
||||
|
||||
try {
|
||||
const resp = await fetch(`${DOMAIN}/api/status`)
|
||||
var resp = await fetch(`${DOMAIN}/api/status`)
|
||||
} catch (err) {
|
||||
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 })
|
||||
} catch (err) {
|
||||
this.setState({ error: err.toString() })
|
||||
}
|
||||
}
|
||||
|
||||
render () {
|
||||
|
|
Loading…
Reference in a new issue