forked from AkkomaGang/akkoma-fe
Improve fetch error handling using a util
This commit is contained in:
parent
9b690209d0
commit
d65422a6a5
1 changed files with 18 additions and 36 deletions
|
@ -64,6 +64,19 @@ let fetch = (url, options) => {
|
||||||
return oldfetch(fullUrl, options)
|
return oldfetch(fullUrl, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const promisedRequest = (url, options) => {
|
||||||
|
return fetch(url, options)
|
||||||
|
.then((response) => {
|
||||||
|
return new Promise((resolve, reject) => response.json()
|
||||||
|
.then((json) => {
|
||||||
|
if (!response.ok) {
|
||||||
|
return reject(new StatusCodeError(response.status, json, { url, options }, response))
|
||||||
|
}
|
||||||
|
return resolve(json)
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Params
|
// Params
|
||||||
// cropH
|
// cropH
|
||||||
// cropW
|
// cropW
|
||||||
|
@ -249,16 +262,7 @@ const denyUser = ({id, credentials}) => {
|
||||||
|
|
||||||
const fetchUser = ({id, credentials}) => {
|
const fetchUser = ({id, credentials}) => {
|
||||||
let url = `${MASTODON_USER_URL}/${id}`
|
let url = `${MASTODON_USER_URL}/${id}`
|
||||||
return fetch(url, { headers: authHeaders(credentials) })
|
return promisedRequest(url, { headers: authHeaders(credentials) })
|
||||||
.then((response) => {
|
|
||||||
return new Promise((resolve, reject) => response.json()
|
|
||||||
.then((json) => {
|
|
||||||
if (!response.ok) {
|
|
||||||
return reject(new StatusCodeError(response.status, json, { url }, response))
|
|
||||||
}
|
|
||||||
return resolve(json)
|
|
||||||
}))
|
|
||||||
})
|
|
||||||
.then((data) => parseUser(data))
|
.then((data) => parseUser(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -526,50 +530,28 @@ const changePassword = ({credentials, password, newPassword, newPasswordConfirma
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchMutes = ({credentials}) => {
|
const fetchMutes = ({credentials}) => {
|
||||||
return fetch(MUTES_URL, {
|
return promisedRequest(MUTES_URL, { headers: authHeaders(credentials) })
|
||||||
headers: authHeaders(credentials)
|
|
||||||
}).then((data) => {
|
|
||||||
if (data.ok) {
|
|
||||||
return data.json()
|
|
||||||
}
|
|
||||||
throw new Error('Error fetching mutes', data)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const muteUser = ({id, credentials}) => {
|
const muteUser = ({id, credentials}) => {
|
||||||
const url = generateUrl(MUTING_URL, { id })
|
const url = generateUrl(MUTING_URL, { id })
|
||||||
return fetch(url, {
|
return promisedRequest(url, {
|
||||||
headers: authHeaders(credentials),
|
headers: authHeaders(credentials),
|
||||||
method: 'POST'
|
method: 'POST'
|
||||||
}).then((data) => {
|
|
||||||
if (data.ok) {
|
|
||||||
return data.json()
|
|
||||||
}
|
|
||||||
throw new Error('Error muting', data)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const unmuteUser = ({id, credentials}) => {
|
const unmuteUser = ({id, credentials}) => {
|
||||||
const url = generateUrl(UNMUTING_URL, { id })
|
const url = generateUrl(UNMUTING_URL, { id })
|
||||||
return fetch(url, {
|
return promisedRequest(url, {
|
||||||
headers: authHeaders(credentials),
|
headers: authHeaders(credentials),
|
||||||
method: 'POST'
|
method: 'POST'
|
||||||
}).then((data) => {
|
|
||||||
if (data.ok) {
|
|
||||||
return data.json()
|
|
||||||
}
|
|
||||||
throw new Error('Error unmuting', data)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchBlocks = ({credentials}) => {
|
const fetchBlocks = ({credentials}) => {
|
||||||
return fetch(BLOCKS_URL, {
|
return promisedRequest(BLOCKS_URL, {
|
||||||
headers: authHeaders(credentials)
|
headers: authHeaders(credentials)
|
||||||
}).then((data) => {
|
|
||||||
if (data.ok) {
|
|
||||||
return data.json()
|
|
||||||
}
|
|
||||||
throw new Error('Error fetching blocks', data)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue