fix ajax error handling
This commit is contained in:
parent
160e763e0d
commit
a82cc57f83
|
@ -11,12 +11,9 @@ export async function setFavorited (statusId, favorited) {
|
||||||
let instanceName = store.get('currentInstance')
|
let instanceName = store.get('currentInstance')
|
||||||
let accessToken = store.get('accessToken')
|
let accessToken = store.get('accessToken')
|
||||||
try {
|
try {
|
||||||
let result = await (favorited
|
await (favorited
|
||||||
? favoriteStatus(instanceName, accessToken, statusId)
|
? favoriteStatus(instanceName, accessToken, statusId)
|
||||||
: unfavoriteStatus(instanceName, accessToken, statusId))
|
: unfavoriteStatus(instanceName, accessToken, statusId))
|
||||||
if (result.error) {
|
|
||||||
throw new Error(result.error)
|
|
||||||
}
|
|
||||||
await database.setStatusFavorited(instanceName, statusId, favorited)
|
await database.setStatusFavorited(instanceName, statusId, favorited)
|
||||||
let statusModifications = store.get('statusModifications')
|
let statusModifications = store.get('statusModifications')
|
||||||
let currentStatusModifications = statusModifications[instanceName] =
|
let currentStatusModifications = statusModifications[instanceName] =
|
||||||
|
|
|
@ -7,6 +7,17 @@ function fetchWithTimeout (url, options) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function throwErrorIfInvalidResponse(response) {
|
||||||
|
let json = await response.json()
|
||||||
|
if (response.status >= 200 && response.status < 300) {
|
||||||
|
return json
|
||||||
|
}
|
||||||
|
if (json && json.error) {
|
||||||
|
throw new Error(response.status + ': ' + json.error)
|
||||||
|
}
|
||||||
|
throw new Error('Request failed: ' + response.status)
|
||||||
|
}
|
||||||
|
|
||||||
async function _post (url, body, headers, timeout) {
|
async function _post (url, body, headers, timeout) {
|
||||||
let fetchFunc = timeout ? fetchWithTimeout : fetch
|
let fetchFunc = timeout ? fetchWithTimeout : fetch
|
||||||
let opts = {
|
let opts = {
|
||||||
|
@ -23,17 +34,19 @@ async function _post (url, body, headers, timeout) {
|
||||||
'Accept': 'application/json'
|
'Accept': 'application/json'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return (await fetchFunc(url, opts)).json()
|
let response = await fetchFunc(url, opts)
|
||||||
|
return throwErrorIfInvalidResponse(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _get (url, headers, timeout) {
|
async function _get (url, headers, timeout) {
|
||||||
let fetchFunc = timeout ? fetchWithTimeout : fetch
|
let fetchFunc = timeout ? fetchWithTimeout : fetch
|
||||||
return (await fetchFunc(url, {
|
let response = await fetchFunc(url, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: Object.assign(headers, {
|
headers: Object.assign(headers, {
|
||||||
'Accept': 'application/json'
|
'Accept': 'application/json'
|
||||||
})
|
})
|
||||||
})).json()
|
})
|
||||||
|
return throwErrorIfInvalidResponse(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function post (url, body, headers = {}) {
|
export async function post (url, body, headers = {}) {
|
||||||
|
|
Loading…
Reference in New Issue