From 96afaca0adaa332aece4fed63d6396f0d40dd801 Mon Sep 17 00:00:00 2001 From: Ramires Viana <59319979+ramiresviana@users.noreply.github.com> Date: Wed, 4 May 2022 12:11:36 +0000 Subject: [PATCH] chore: refactor response error handling --- frontend/src/api/files.js | 38 ++++++++++---------------- frontend/src/api/pub.js | 32 ++++++++++------------ frontend/src/api/search.js | 22 ++++++--------- frontend/src/api/settings.js | 6 +--- frontend/src/api/share.js | 6 +--- frontend/src/api/users.js | 14 ++-------- frontend/src/api/utils.js | 7 +++++ frontend/src/store/getters.js | 4 ++- frontend/src/utils/upload.js | 2 +- frontend/src/views/Errors.vue | 9 +----- frontend/src/views/Files.vue | 2 +- frontend/src/views/Share.vue | 4 +-- frontend/src/views/settings/Global.vue | 2 +- frontend/src/views/settings/Shares.vue | 2 +- frontend/src/views/settings/User.vue | 2 +- frontend/src/views/settings/Users.vue | 2 +- 16 files changed, 61 insertions(+), 93 deletions(-) diff --git a/frontend/src/api/files.js b/frontend/src/api/files.js index 6cae2359..49077123 100644 --- a/frontend/src/api/files.js +++ b/frontend/src/api/files.js @@ -7,28 +7,24 @@ export async function fetch(url) { const res = await fetchURL(`/api/resources${url}`, {}); - if (res.status === 200) { - let data = await res.json(); - data.url = `/files${url}`; + let data = await res.json(); + data.url = `/files${url}`; - if (data.isDir) { - if (!data.url.endsWith("/")) data.url += "/"; - data.items = data.items.map((item, index) => { - item.index = index; - item.url = `${data.url}${encodeURIComponent(item.name)}`; + if (data.isDir) { + if (!data.url.endsWith("/")) data.url += "/"; + data.items = data.items.map((item, index) => { + item.index = index; + item.url = `${data.url}${encodeURIComponent(item.name)}`; - if (item.isDir) { - item.url += "/"; - } + if (item.isDir) { + item.url += "/"; + } - return item; - }); - } - - return data; - } else { - throw new Error(res.status); + return item; + }); } + + return data; } async function resourceAction(url, method, content) { @@ -42,11 +38,7 @@ async function resourceAction(url, method, content) { const res = await fetchURL(`/api/resources${url}`, opts); - if (res.status !== 200) { - throw new Error(await res.text()); - } else { - return res; - } + return res; } export async function remove(url) { diff --git a/frontend/src/api/pub.js b/frontend/src/api/pub.js index 626571b3..f84504f9 100644 --- a/frontend/src/api/pub.js +++ b/frontend/src/api/pub.js @@ -8,28 +8,24 @@ export async function fetch(url, password = "") { headers: { "X-SHARE-PASSWORD": encodeURIComponent(password) }, }); - if (res.status === 200) { - let data = await res.json(); - data.url = `/share${url}`; + let data = await res.json(); + data.url = `/share${url}`; - if (data.isDir) { - if (!data.url.endsWith("/")) data.url += "/"; - data.items = data.items.map((item, index) => { - item.index = index; - item.url = `${data.url}${encodeURIComponent(item.name)}`; + if (data.isDir) { + if (!data.url.endsWith("/")) data.url += "/"; + data.items = data.items.map((item, index) => { + item.index = index; + item.url = `${data.url}${encodeURIComponent(item.name)}`; - if (item.isDir) { - item.url += "/"; - } + if (item.isDir) { + item.url += "/"; + } - return item; - }); - } - - return data; - } else { - throw new Error(res.status); + return item; + }); } + + return data; } export function download(format, hash, token, ...files) { diff --git a/frontend/src/api/search.js b/frontend/src/api/search.js index 08be5c1d..42846880 100644 --- a/frontend/src/api/search.js +++ b/frontend/src/api/search.js @@ -11,21 +11,17 @@ export default async function search(base, query) { let res = await fetchURL(`/api/search${base}?query=${query}`, {}); - if (res.status === 200) { - let data = await res.json(); + let data = await res.json(); - data = data.map((item) => { - item.url = `/files${base}` + url.encodePath(item.path); + data = data.map((item) => { + item.url = `/files${base}` + url.encodePath(item.path); - if (item.dir) { - item.url += "/"; - } + if (item.dir) { + item.url += "/"; + } - return item; - }); + return item; + }); - return data; - } else { - throw Error(res.status); - } + return data; } diff --git a/frontend/src/api/settings.js b/frontend/src/api/settings.js index 8abe1f1e..e03b0db1 100644 --- a/frontend/src/api/settings.js +++ b/frontend/src/api/settings.js @@ -5,12 +5,8 @@ export function get() { } export async function update(settings) { - const res = await fetchURL(`/api/settings`, { + await fetchURL(`/api/settings`, { method: "PUT", body: JSON.stringify(settings), }); - - if (res.status !== 200) { - throw new Error(res.status); - } } diff --git a/frontend/src/api/share.js b/frontend/src/api/share.js index 29dfe877..1ac4473a 100644 --- a/frontend/src/api/share.js +++ b/frontend/src/api/share.js @@ -10,13 +10,9 @@ export async function get(url) { } export async function remove(hash) { - const res = await fetchURL(`/api/share/${hash}`, { + await fetchURL(`/api/share/${hash}`, { method: "DELETE", }); - - if (res.status !== 200) { - throw new Error(res.status); - } } export async function create(url, password = "", expires = "", unit = "hours") { diff --git a/frontend/src/api/users.js b/frontend/src/api/users.js index 7975d66a..105d6cc0 100644 --- a/frontend/src/api/users.js +++ b/frontend/src/api/users.js @@ -20,13 +20,11 @@ export async function create(user) { if (res.status === 201) { return res.headers.get("Location"); - } else { - throw new Error(res.status); } } export async function update(user, which = ["all"]) { - const res = await fetchURL(`/api/users/${user.id}`, { + await fetchURL(`/api/users/${user.id}`, { method: "PUT", body: JSON.stringify({ what: "user", @@ -34,18 +32,10 @@ export async function update(user, which = ["all"]) { data: user, }), }); - - if (res.status !== 200) { - throw new Error(res.status); - } } export async function remove(id) { - const res = await fetchURL(`/api/users/${id}`, { + await fetchURL(`/api/users/${id}`, { method: "DELETE", }); - - if (res.status !== 200) { - throw new Error(res.status); - } } diff --git a/frontend/src/api/utils.js b/frontend/src/api/utils.js index f9fc9023..4319e8ff 100644 --- a/frontend/src/api/utils.js +++ b/frontend/src/api/utils.js @@ -26,6 +26,13 @@ export async function fetchURL(url, opts) { await renew(store.state.jwt); } + if (res.status < 200 || res.status > 299) { + const error = new Error(await res.text()); + error.status = res.status; + + throw error; + } + return res; } diff --git a/frontend/src/store/getters.js b/frontend/src/store/getters.js index ce0598a5..6bee9bcd 100644 --- a/frontend/src/store/getters.js +++ b/frontend/src/store/getters.js @@ -28,7 +28,9 @@ const getters = { let name = upload.file.name; let size = state.upload.sizes[id]; let isDir = upload.file.isDir; - let progress = isDir ? 100 : Math.ceil((state.upload.progress[id] / size) * 100); + let progress = isDir + ? 100 + : Math.ceil((state.upload.progress[id] / size) * 100); files.push({ id, diff --git a/frontend/src/utils/upload.js b/frontend/src/utils/upload.js index 05cf5ea6..2184072f 100644 --- a/frontend/src/utils/upload.js +++ b/frontend/src/utils/upload.js @@ -130,7 +130,7 @@ export function handleFiles(files, base, overwrite = false) { path, file, overwrite, - ...(!file.isDir && { type: detectType(file.type) }) + ...(!file.isDir && { type: detectType(file.type) }), }; store.dispatch("upload/upload", item); diff --git a/frontend/src/views/Errors.vue b/frontend/src/views/Errors.vue index b010906d..43746105 100644 --- a/frontend/src/views/Errors.vue +++ b/frontend/src/views/Errors.vue @@ -38,15 +38,8 @@ export default { }, props: ["errorCode", "showHeader"], computed: { - code() { - return this.errorCode === "0" || - this.errorCode === "404" || - this.errorCode === "403" - ? parseInt(this.errorCode) - : 500; - }, info() { - return errors[this.code]; + return errors[this.errorCode] ? errors[this.errorCode] : errors[500]; }, }, }; diff --git a/frontend/src/views/Files.vue b/frontend/src/views/Files.vue index 128b8a9b..bc9e24ce 100644 --- a/frontend/src/views/Files.vue +++ b/frontend/src/views/Files.vue @@ -4,7 +4,7 @@ - +

diff --git a/frontend/src/views/Share.vue b/frontend/src/views/Share.vue index f26b4fc2..36490f1f 100644 --- a/frontend/src/views/Share.vue +++ b/frontend/src/views/Share.vue @@ -30,7 +30,7 @@

-
+
- +