Browse Source

improved fetch error handling

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
799dd85f79
  1. 13
      static/src/Home/Home.js
  2. 5
      static/src/Lookup/Lookup.js
  3. 3
      static/src/index.js

13
static/src/Home/Home.js

@ -4,6 +4,7 @@ import DatePicker from 'react-datepicker';
import moment from 'moment'; import moment from 'moment';
import MediaQuery from 'react-responsive'; import MediaQuery from 'react-responsive';
import 'react-datepicker/dist/react-datepicker.css'; import 'react-datepicker/dist/react-datepicker.css';
import toastr from 'toastr'
import CustomCard from '../Card/Card' import CustomCard from '../Card/Card'
import './Home.css' import './Home.css'
@ -22,11 +23,15 @@ export default class HomeComponent extends Component {
'Authorization': window.localStorage.getItem('token'), 'Authorization': window.localStorage.getItem('token'),
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
}).then(res => res.ok ? res.json() : Promise.reject(res.json())) })
.then(res => res.ok ? res.json() : Promise.reject(res.json()))
.then(() => { .then(() => {
this.setState({ showCustomIDError: true }) this.setState({ showCustomIDError: true })
}) })
.catch(() => this.setState({ showCustomIDError: false })) .catch(e => {
this.setState({ showCustomIDError: false })
toastr.error(`Could not fetch lookup: ${e}`)
})
} }
onSettingsChange = (e, { value }) => this.setState({ setOptions: value }) onSettingsChange = (e, { value }) => this.setState({ setOptions: value })
@ -56,7 +61,8 @@ export default class HomeComponent extends Component {
'Authorization': window.localStorage.getItem('token'), 'Authorization': window.localStorage.getItem('token'),
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
}).then(res => res.ok ? res.json() : Promise.reject(res.json())) })
.then(res => res.ok ? res.json() : Promise.reject(res.json()))
.then(r => this.setState({ .then(r => this.setState({
links: [...this.state.links, [ links: [...this.state.links, [
r.URL, r.URL,
@ -65,6 +71,7 @@ export default class HomeComponent extends Component {
r.DeletionURL r.DeletionURL
]] ]]
})) }))
.catch(e => toastr.error(`Could not fetch create: ${e}`))
} }
} }

5
static/src/Lookup/Lookup.js

@ -1,6 +1,7 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { Segment, Header, Form, Input, Card } from 'semantic-ui-react' import { Segment, Header, Form, Input, Card } from 'semantic-ui-react'
import moment from 'moment'; import moment from 'moment';
import toastr from 'toastr'
import CustomCard from '../Card/Card' import CustomCard from '../Card/Card'
@ -20,7 +21,8 @@ export default class LookupComponent extends Component {
'Authorization': window.localStorage.getItem('token'), 'Authorization': window.localStorage.getItem('token'),
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
}).then(res => res.ok ? res.json() : Promise.reject(res.json())) })
.then(res => res.ok ? res.json() : Promise.reject(res.json()))
.then(res => this.setState({ .then(res => this.setState({
links: [...this.state.links, [ links: [...this.state.links, [
res.URL, res.URL,
@ -31,6 +33,7 @@ export default class LookupComponent extends Component {
res.Expiration ? moment(res.Expiration) : null res.Expiration ? moment(res.Expiration) : null
]] ]]
})) }))
.catch(e => toastr.error(`Could not fetch lookup: ${e}`))
} }
render() { render() {
const { links } = this.state const { links } = this.state

3
static/src/index.js

@ -31,6 +31,7 @@ export default class BaseComponent extends Component {
.then(d => d.json()) .then(d => d.json())
.then(info => this.setState({ info })) .then(info => this.setState({ info }))
.then(() => this.checkAuth()) .then(() => this.checkAuth())
.catch(e => toastr.error(`Could not fetch info: ${e}`))
} }
checkAuth = () => { checkAuth = () => {
@ -54,7 +55,7 @@ export default class BaseComponent extends Component {
}) })
}) })
.catch(e => { .catch(e => {
toastr.error(`Could not fetch info: ${e}`) toastr.error(`Could not fetch check: ${e}`)
window.localStorage.removeItem('token'); window.localStorage.removeItem('token');
that.setState({ authorized: false }) that.setState({ authorized: false })
}) })

Loading…
Cancel
Save