[#89] Fix error handling of fetch
API
fixes #89 Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
1a24fbaba6
commit
f0fd162b4e
1 changed files with 45 additions and 27 deletions
44
src/app.vue
44
src/app.vue
|
@ -260,16 +260,25 @@ export default {
|
|||
},
|
||||
method: 'POST',
|
||||
})
|
||||
.then(resp => resp.json())
|
||||
.then(data => ({ data }))
|
||||
.then(resp => {
|
||||
this.secretId = resp.data.secret_id
|
||||
if (resp.status !== 201) {
|
||||
// Server says "no"
|
||||
this.error = this.$t('alert-something-went-wrong')
|
||||
this.showError = true
|
||||
return
|
||||
}
|
||||
|
||||
resp.json()
|
||||
.then(data => {
|
||||
this.secretId = data.secret_id
|
||||
this.secret = ''
|
||||
|
||||
// Give the interface a moment to transistion and focus
|
||||
window.setTimeout(() => this.$refs.secretUrl.focus(), 100)
|
||||
})
|
||||
})
|
||||
.catch(err => {
|
||||
// Network error
|
||||
this.error = this.$t('alert-something-went-wrong')
|
||||
this.showError = true
|
||||
}))
|
||||
|
@ -300,10 +309,24 @@ export default {
|
|||
// requestSecret requests the encrypted secret from the backend
|
||||
requestSecret() {
|
||||
fetch(`api/get/${this.secretId}`)
|
||||
.then(resp => resp.json())
|
||||
.then(data => ({ data }))
|
||||
.then(resp => {
|
||||
const secret = resp.data.secret
|
||||
if (resp.status === 404) {
|
||||
// Secret has already been consumed
|
||||
this.error = this.$t('alert-secret-not-found')
|
||||
this.showError = true
|
||||
return
|
||||
}
|
||||
|
||||
if (resp.status !== 200) {
|
||||
// Some other non-200: Something(tm) was wrong
|
||||
this.error = this.$t('alert-something-went-wrong')
|
||||
this.showError = true
|
||||
return
|
||||
}
|
||||
|
||||
resp.json()
|
||||
.then(data => {
|
||||
const secret = data.secret
|
||||
if (!this.securePassword) {
|
||||
this.secret = secret
|
||||
return
|
||||
|
@ -318,16 +341,11 @@ export default {
|
|||
this.showError = true
|
||||
})
|
||||
})
|
||||
})
|
||||
.catch(err => {
|
||||
switch (err.response.status) {
|
||||
case 404:
|
||||
this.error = this.$t('alert-secret-not-found')
|
||||
this.showError = true
|
||||
break
|
||||
default:
|
||||
// Network error
|
||||
this.error = this.$t('alert-something-went-wrong')
|
||||
this.showError = true
|
||||
}
|
||||
})
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue