From 54118031a6558d4f93234b3ff9f0ec070f3dfa1e Mon Sep 17 00:00:00 2001
From: Spectralitree <72747870+Spectralitree@users.noreply.github.com>
Date: Sun, 3 Oct 2021 19:11:21 +0200
Subject: [PATCH] somewhat better error handling
---
src/Components/Private/Edit/EditSystem.js | 18 ++++++--
.../Private/Edit/EditSystemPrivacy.js | 34 +++++++++-----
src/Components/Private/MemberCard.js | 46 +++++++++----------
src/Components/Private/MemberPage.js | 44 +++++++++---------
src/Components/Private/System.js | 7 ++-
src/Pages/Home.js | 37 +++++++++++----
6 files changed, 116 insertions(+), 70 deletions(-)
diff --git a/src/Components/Private/Edit/EditSystem.js b/src/Components/Private/Edit/EditSystem.js
index 4d9c2106..40eea325 100644
--- a/src/Components/Private/Edit/EditSystem.js
+++ b/src/Components/Private/Edit/EditSystem.js
@@ -16,8 +16,9 @@ const EditSystem = ({
editDesc,
setEditMode,
setErrorAlert,
- setUser,
user,
+ setUser,
+ setErrorMessage
}) => {
const [invalidTimezone, setInvalidTimezone] = useState(false);
@@ -38,7 +39,11 @@ const EditSystem = ({
Authorization: localStorage.getItem("token"),
},
})
- .then((res) => res.json())
+ .then((res) => {
+ if (!res.ok)
+ throw new Error('HTTP Status ' + res.status)
+ return res.json();
+ })
.then(() => {
setUser((prevState) => {
return { ...prevState, ...data };
@@ -47,7 +52,14 @@ const EditSystem = ({
setEditMode(false);
})
.catch((error) => {
- console.error(error);
+ console.log(error);
+ setErrorMessage(error.message);
+ if (error.message === 'HTTP Status 401') {
+ setErrorMessage("Your token is invalid, please log out and enter a new token.")
+ };
+ if (error.message === 'HTTP Status 500') {
+ setErrorMessage("500: Internal server error.")
+ }
setErrorAlert(true);
});
};
diff --git a/src/Components/Private/Edit/EditSystemPrivacy.js b/src/Components/Private/Edit/EditSystemPrivacy.js
index b7a32e70..27e6c6f1 100644
--- a/src/Components/Private/Edit/EditSystemPrivacy.js
+++ b/src/Components/Private/Edit/EditSystemPrivacy.js
@@ -10,6 +10,7 @@ const EditSystemPrivacy = ({
setUser,
user,
setPrivacyEdit,
+ setErrorMessage
}) => {
const { register: registerPrivacy, handleSubmit: handleSubmitPrivacy } =
useForm();
@@ -24,18 +25,29 @@ const EditSystemPrivacy = ({
Authorization: localStorage.getItem("token"),
},
})
- .then((res) => res.json())
- .then((data) => {
- setUser((prevState) => {
- return { ...prevState, ...data };
- });
- localStorage.setItem("user", JSON.stringify(user));
- setPrivacyEdit(false);
- })
- .catch((error) => {
- console.error(error);
- setErrorAlert(true);
+ .then((res) => {
+ if (!res.ok)
+ throw new Error('HTTP Status ' + res.status)
+ return res.json();
+ })
+ .then(() => {
+ setUser((prevState) => {
+ return { ...prevState, ...data };
});
+ localStorage.setItem("user", JSON.stringify(user));
+ setPrivacyEdit(false);
+ })
+ .catch((error) => {
+ console.log(error);
+ setErrorMessage(error.message);
+ if (error.message === 'HTTP Status 401') {
+ setErrorMessage("Your token is invalid, please log out and enter a new token.")
+ };
+ if (error.message === 'HTTP Status 500') {
+ setErrorMessage("500: Internal server error.")
+ }
+ setErrorAlert(true);
+ });
};
return (
diff --git a/src/Components/Private/MemberCard.js b/src/Components/Private/MemberCard.js
index 6686c3cb..7272e7c9 100644
--- a/src/Components/Private/MemberCard.js
+++ b/src/Components/Private/MemberCard.js
@@ -45,6 +45,7 @@ export default function MemberCard(props) {
const [ errorAlert, setErrorAlert ] = useState(false);
const [ wrongID, setWrongID ] = useState(false);
const [ memberDeleted, setMemberDeleted ] = useState(false);
+ const [ errorMessage, setErrorMessage ] = useState("");
const {
register: registerEdit,
@@ -122,46 +123,45 @@ export default function MemberCard(props) {
}
}, [member.description, member.color, member.birthday, member.display_name, member.pronouns, member.avatar_url, member.proxy_tags, member.created, member.banner]);
- const submitEdit = data => {
- props.edit(Object.assign(member, data));
-
+ function submitEditPatch(data) {
fetch(`${API_URL}m/${member.id}`,{
method: 'PATCH',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'Authorization': localStorage.getItem("token")
- }}).then (res => res.json()
+ }}).then (res => {
+ if (!res.ok) {
+ throw new Error('HTTP Status ' + res.status);
+ }
+ return res.json();
+ }
).then (data => {
setMember(prevState => {return {...prevState, ...data}});
setErrorAlert(false);
setEditMode(false);
}
).catch (error => {
- console.error(error);
+ console.log(error);
+ setErrorMessage(error.message);
+ if (error.message === 'HTTP Status 401') {
+ setErrorMessage("401: Your token is invalid, please log out and enter a new token.")
+ };
+ if (error.message === 'HTTP Status 500') {
+ setErrorMessage("500: Internal server error.")
+ }
setErrorAlert(true);
});
}
+ const submitEdit = data => {
+ props.edit(Object.assign(member, data));
+ submitEditPatch(data);
+ }
+
const submitPrivacy = data => {
props.edit(Object.assign(member, data));
-
- fetch(`${API_URL}m/${member.id}`,{
- method: 'PATCH',
- body: JSON.stringify(data),
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': localStorage.getItem("token")
- }}).then (res => res.json()
- ).then (data => {
- setMember(prevState => {return {...prevState, ...data}});
- setErrorAlert(false);
- setprivacyEdit(false)
- }
- ).catch (error => {
- console.error(error);
- setErrorAlert(true);
- })
+ submitEditPatch(data);
}
const deleteMember = data => {
@@ -232,7 +232,7 @@ export default function MemberCard(props) {
function renderCard() {
return (