Skip to content

Commit

Permalink
chore: refactor response error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiresviana committed May 4, 2022
1 parent d1d7b23 commit 96afaca
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 97 deletions.
42 changes: 17 additions & 25 deletions frontend/src/api/files.js
Expand Up @@ -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}`;

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 += "/";
}

return item;
});
}
let data = await res.json();
data.url = `/files${url}`;

return data;
} else {
throw new Error(res.status);
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 += "/";
}

return item;
});
}

return data;
}

async function resourceAction(url, method, content) {
Expand All @@ -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) {
Expand Down
36 changes: 16 additions & 20 deletions frontend/src/api/pub.js
Expand Up @@ -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}`;

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 += "/";
}

return item;
});
}
let data = await res.json();
data.url = `/share${url}`;

return data;
} else {
throw new Error(res.status);
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 += "/";
}

return item;
});
}

return data;
}

export function download(format, hash, token, ...files) {
Expand Down
22 changes: 9 additions & 13 deletions frontend/src/api/search.js
Expand Up @@ -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;
}
6 changes: 1 addition & 5 deletions frontend/src/api/settings.js
Expand Up @@ -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);
}
}
6 changes: 1 addition & 5 deletions frontend/src/api/share.js
Expand Up @@ -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") {
Expand Down
14 changes: 2 additions & 12 deletions frontend/src/api/users.js
Expand Up @@ -20,32 +20,22 @@ 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",
which: which,
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);
}
}
7 changes: 7 additions & 0 deletions frontend/src/api/utils.js
Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/store/getters.js
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/upload.js
Expand Up @@ -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);
Expand Down
9 changes: 1 addition & 8 deletions frontend/src/views/Errors.vue
Expand Up @@ -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];
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/Files.vue
Expand Up @@ -4,7 +4,7 @@

<breadcrumbs base="/files" />

<errors v-if="error" :errorCode="error.message" />
<errors v-if="error" :errorCode="error.status" />
<component v-else-if="currentView" :is="currentView"></component>
<div v-else>
<h2 class="message delayed">
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/views/Share.vue
Expand Up @@ -30,7 +30,7 @@
</h2>
</div>
<div v-else-if="error">
<div v-if="error.message === '401'">
<div v-if="error.status === 401">
<div class="card floating" id="password">
<div v-if="attemptedPasswordLogin" class="share__wrong__password">
{{ $t("login.wrongCredentials") }}
Expand Down Expand Up @@ -60,7 +60,7 @@
</div>
</div>
</div>
<errors v-else :errorCode="error.message" />
<errors v-else :errorCode="error.status" />
</div>
<div v-else>
<div class="share">
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/settings/Global.vue
@@ -1,5 +1,5 @@
<template>
<errors v-if="error" :errorCode="error.message" />
<errors v-if="error" :errorCode="error.status" />
<div class="row" v-else-if="!loading">
<div class="column">
<form class="card" @submit.prevent="save">
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/settings/Shares.vue
@@ -1,5 +1,5 @@
<template>
<errors v-if="error" :errorCode="error.message" />
<errors v-if="error" :errorCode="error.status" />
<div class="row" v-else-if="!loading">
<div class="column">
<div class="card">
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/settings/User.vue
@@ -1,5 +1,5 @@
<template>
<errors v-if="error" :errorCode="error.message" />
<errors v-if="error" :errorCode="error.status" />
<div class="row" v-else-if="!loading">
<div class="column">
<form @submit="save" class="card">
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/settings/Users.vue
@@ -1,5 +1,5 @@
<template>
<errors v-if="error" :errorCode="error.message" />
<errors v-if="error" :errorCode="error.status" />
<div class="row" v-else-if="!loading">
<div class="column">
<div class="card">
Expand Down

0 comments on commit 96afaca

Please sign in to comment.