handle when response is not json

This commit is contained in:
2023-02-20 13:37:36 +10:00
parent 5ee55b298f
commit 0349225ede

View File

@@ -79,12 +79,6 @@ export const api = {
if (!response.body) { if (!response.body) {
return response; return response;
// return {
// status: 0,
// message:
// "ReadableStream not yet supported in this browser.",
// data: null,
// };
} }
let contentLength = let contentLength =
@@ -138,7 +132,12 @@ export const api = {
return response; return response;
}) })
.then(async (response) => { .then(async (response) => {
const data = response.json ? await response.json() : {}; let data: string | object = "";
if (response.headers.get("content-type") == null) {
data = response.text ? await response.text() : "";
} else {
data = response.json ? await response.json() : {};
}
const result = { const result = {
status: response.status, status: response.status,
statusText: response.statusText, statusText: response.statusText,
@@ -154,7 +153,6 @@ export const api = {
resolve(result); resolve(result);
}) })
.catch((error) => { .catch((error) => {
console.log(error);
// Handle any errors thrown during the fetch process // Handle any errors thrown during the fetch process
const { response, ...rest } = error; const { response, ...rest } = error;
reject({ reject({