chore: refactor response error handling
This commit is contained in:
parent
d1d7b23da6
commit
96afaca0ad
|
@ -7,7 +7,6 @@ export async function fetch(url) {
|
||||||
|
|
||||||
const res = await fetchURL(`/api/resources${url}`, {});
|
const res = await fetchURL(`/api/resources${url}`, {});
|
||||||
|
|
||||||
if (res.status === 200) {
|
|
||||||
let data = await res.json();
|
let data = await res.json();
|
||||||
data.url = `/files${url}`;
|
data.url = `/files${url}`;
|
||||||
|
|
||||||
|
@ -26,9 +25,6 @@ export async function fetch(url) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
} else {
|
|
||||||
throw new Error(res.status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resourceAction(url, method, content) {
|
async function resourceAction(url, method, content) {
|
||||||
|
@ -42,11 +38,7 @@ async function resourceAction(url, method, content) {
|
||||||
|
|
||||||
const res = await fetchURL(`/api/resources${url}`, opts);
|
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) {
|
export async function remove(url) {
|
||||||
|
|
|
@ -8,7 +8,6 @@ export async function fetch(url, password = "") {
|
||||||
headers: { "X-SHARE-PASSWORD": encodeURIComponent(password) },
|
headers: { "X-SHARE-PASSWORD": encodeURIComponent(password) },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.status === 200) {
|
|
||||||
let data = await res.json();
|
let data = await res.json();
|
||||||
data.url = `/share${url}`;
|
data.url = `/share${url}`;
|
||||||
|
|
||||||
|
@ -27,9 +26,6 @@ export async function fetch(url, password = "") {
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
} else {
|
|
||||||
throw new Error(res.status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function download(format, hash, token, ...files) {
|
export function download(format, hash, token, ...files) {
|
||||||
|
|
|
@ -11,7 +11,6 @@ export default async function search(base, query) {
|
||||||
|
|
||||||
let res = await fetchURL(`/api/search${base}?query=${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) => {
|
data = data.map((item) => {
|
||||||
|
@ -25,7 +24,4 @@ export default async function search(base, query) {
|
||||||
});
|
});
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
} else {
|
|
||||||
throw Error(res.status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,8 @@ export function get() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function update(settings) {
|
export async function update(settings) {
|
||||||
const res = await fetchURL(`/api/settings`, {
|
await fetchURL(`/api/settings`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
body: JSON.stringify(settings),
|
body: JSON.stringify(settings),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.status !== 200) {
|
|
||||||
throw new Error(res.status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,13 +10,9 @@ export async function get(url) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function remove(hash) {
|
export async function remove(hash) {
|
||||||
const res = await fetchURL(`/api/share/${hash}`, {
|
await fetchURL(`/api/share/${hash}`, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.status !== 200) {
|
|
||||||
throw new Error(res.status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function create(url, password = "", expires = "", unit = "hours") {
|
export async function create(url, password = "", expires = "", unit = "hours") {
|
||||||
|
|
|
@ -20,13 +20,11 @@ export async function create(user) {
|
||||||
|
|
||||||
if (res.status === 201) {
|
if (res.status === 201) {
|
||||||
return res.headers.get("Location");
|
return res.headers.get("Location");
|
||||||
} else {
|
|
||||||
throw new Error(res.status);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function update(user, which = ["all"]) {
|
export async function update(user, which = ["all"]) {
|
||||||
const res = await fetchURL(`/api/users/${user.id}`, {
|
await fetchURL(`/api/users/${user.id}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
what: "user",
|
what: "user",
|
||||||
|
@ -34,18 +32,10 @@ export async function update(user, which = ["all"]) {
|
||||||
data: user,
|
data: user,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.status !== 200) {
|
|
||||||
throw new Error(res.status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function remove(id) {
|
export async function remove(id) {
|
||||||
const res = await fetchURL(`/api/users/${id}`, {
|
await fetchURL(`/api/users/${id}`, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.status !== 200) {
|
|
||||||
throw new Error(res.status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,13 @@ export async function fetchURL(url, opts) {
|
||||||
await renew(store.state.jwt);
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,9 @@ const getters = {
|
||||||
let name = upload.file.name;
|
let name = upload.file.name;
|
||||||
let size = state.upload.sizes[id];
|
let size = state.upload.sizes[id];
|
||||||
let isDir = upload.file.isDir;
|
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({
|
files.push({
|
||||||
id,
|
id,
|
||||||
|
|
|
@ -130,7 +130,7 @@ export function handleFiles(files, base, overwrite = false) {
|
||||||
path,
|
path,
|
||||||
file,
|
file,
|
||||||
overwrite,
|
overwrite,
|
||||||
...(!file.isDir && { type: detectType(file.type) })
|
...(!file.isDir && { type: detectType(file.type) }),
|
||||||
};
|
};
|
||||||
|
|
||||||
store.dispatch("upload/upload", item);
|
store.dispatch("upload/upload", item);
|
||||||
|
|
|
@ -38,15 +38,8 @@ export default {
|
||||||
},
|
},
|
||||||
props: ["errorCode", "showHeader"],
|
props: ["errorCode", "showHeader"],
|
||||||
computed: {
|
computed: {
|
||||||
code() {
|
|
||||||
return this.errorCode === "0" ||
|
|
||||||
this.errorCode === "404" ||
|
|
||||||
this.errorCode === "403"
|
|
||||||
? parseInt(this.errorCode)
|
|
||||||
: 500;
|
|
||||||
},
|
|
||||||
info() {
|
info() {
|
||||||
return errors[this.code];
|
return errors[this.errorCode] ? errors[this.errorCode] : errors[500];
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
<breadcrumbs base="/files" />
|
<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>
|
<component v-else-if="currentView" :is="currentView"></component>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<h2 class="message delayed">
|
<h2 class="message delayed">
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
</h2>
|
</h2>
|
||||||
</div>
|
</div>
|
||||||
<div v-else-if="error">
|
<div v-else-if="error">
|
||||||
<div v-if="error.message === '401'">
|
<div v-if="error.status === 401">
|
||||||
<div class="card floating" id="password">
|
<div class="card floating" id="password">
|
||||||
<div v-if="attemptedPasswordLogin" class="share__wrong__password">
|
<div v-if="attemptedPasswordLogin" class="share__wrong__password">
|
||||||
{{ $t("login.wrongCredentials") }}
|
{{ $t("login.wrongCredentials") }}
|
||||||
|
@ -60,7 +60,7 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<errors v-else :errorCode="error.message" />
|
<errors v-else :errorCode="error.status" />
|
||||||
</div>
|
</div>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<div class="share">
|
<div class="share">
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<template>
|
<template>
|
||||||
<errors v-if="error" :errorCode="error.message" />
|
<errors v-if="error" :errorCode="error.status" />
|
||||||
<div class="row" v-else-if="!loading">
|
<div class="row" v-else-if="!loading">
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<form class="card" @submit.prevent="save">
|
<form class="card" @submit.prevent="save">
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<template>
|
<template>
|
||||||
<errors v-if="error" :errorCode="error.message" />
|
<errors v-if="error" :errorCode="error.status" />
|
||||||
<div class="row" v-else-if="!loading">
|
<div class="row" v-else-if="!loading">
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<template>
|
<template>
|
||||||
<errors v-if="error" :errorCode="error.message" />
|
<errors v-if="error" :errorCode="error.status" />
|
||||||
<div class="row" v-else-if="!loading">
|
<div class="row" v-else-if="!loading">
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<form @submit="save" class="card">
|
<form @submit="save" class="card">
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<template>
|
<template>
|
||||||
<errors v-if="error" :errorCode="error.message" />
|
<errors v-if="error" :errorCode="error.status" />
|
||||||
<div class="row" v-else-if="!loading">
|
<div class="row" v-else-if="!loading">
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
|
|
Loading…
Reference in New Issue