aboutsummaryrefslogtreecommitdiff
path: root/src/services/api/api.service.js
diff options
context:
space:
mode:
authortaehoon <th.dev91@gmail.com>2019-03-01 22:47:07 -0500
committertaehoon <th.dev91@gmail.com>2019-03-21 16:26:13 -0400
commitd65422a6a59e0126201f71d0ca42343c075da54e (patch)
treef2e3a95cb71e107dc63f34a8c99a185df8d3d4fa /src/services/api/api.service.js
parent9b690209d056d82fa855b682ffeb74378128460e (diff)
Improve fetch error handling using a util
Diffstat (limited to 'src/services/api/api.service.js')
-rw-r--r--src/services/api/api.service.js54
1 files changed, 18 insertions, 36 deletions
diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js
index 7da2758a..b7fcf510 100644
--- a/src/services/api/api.service.js
+++ b/src/services/api/api.service.js
@@ -64,6 +64,19 @@ let fetch = (url, 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
// cropH
// cropW
@@ -249,16 +262,7 @@ const denyUser = ({id, credentials}) => {
const fetchUser = ({id, credentials}) => {
let url = `${MASTODON_USER_URL}/${id}`
- return fetch(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)
- }))
- })
+ return promisedRequest(url, { headers: authHeaders(credentials) })
.then((data) => parseUser(data))
}
@@ -526,50 +530,28 @@ const changePassword = ({credentials, password, newPassword, newPasswordConfirma
}
const fetchMutes = ({credentials}) => {
- return fetch(MUTES_URL, {
- headers: authHeaders(credentials)
- }).then((data) => {
- if (data.ok) {
- return data.json()
- }
- throw new Error('Error fetching mutes', data)
- })
+ return promisedRequest(MUTES_URL, { headers: authHeaders(credentials) })
}
const muteUser = ({id, credentials}) => {
const url = generateUrl(MUTING_URL, { id })
- return fetch(url, {
+ return promisedRequest(url, {
headers: authHeaders(credentials),
method: 'POST'
- }).then((data) => {
- if (data.ok) {
- return data.json()
- }
- throw new Error('Error muting', data)
})
}
const unmuteUser = ({id, credentials}) => {
const url = generateUrl(UNMUTING_URL, { id })
- return fetch(url, {
+ return promisedRequest(url, {
headers: authHeaders(credentials),
method: 'POST'
- }).then((data) => {
- if (data.ok) {
- return data.json()
- }
- throw new Error('Error unmuting', data)
})
}
const fetchBlocks = ({credentials}) => {
- return fetch(BLOCKS_URL, {
+ return promisedRequest(BLOCKS_URL, {
headers: authHeaders(credentials)
- }).then((data) => {
- if (data.ok) {
- return data.json()
- }
- throw new Error('Error fetching blocks', data)
})
}