From ae857f4c8faa03d6079eb95432a15b07b5c6111c Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Sun, 10 Mar 2024 16:46:54 -0600 Subject: [PATCH] guard against another set of errors when deleting an image --- src/views/GiftedDetails.vue | 76 +++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/src/views/GiftedDetails.vue b/src/views/GiftedDetails.vue index 0ece39e..65fc7b3 100644 --- a/src/views/GiftedDetails.vue +++ b/src/views/GiftedDetails.vue @@ -202,35 +202,61 @@ export default class GiftedDetails extends Vue { } async deleteImage() { - const identity = await libsUtil.getIdentity(this.activeDid); - const token = await accessToken(identity); - const response = await this.axios.delete( - DEFAULT_IMAGE_API_SERVER + "/image/" + encodeURIComponent(this.imageUrl), - { - headers: { - Authorization: `Bearer ${token}`, - }, - }, - ); - if (response.status === 204) { - // don't bother with a notification - // (either they'll simply continue or they're canceling and going back) - } else { - console.error("Error deleting image:", response); - this.$notify( + try { + const identity = await libsUtil.getIdentity(this.activeDid); + const token = await accessToken(identity); + const response = await this.axios.delete( + DEFAULT_IMAGE_API_SERVER + + "/image/" + + encodeURIComponent(this.imageUrl), { - group: "alert", - type: "danger", - title: "Error", - text: "There was an error deleting the image.", + headers: { + Authorization: `Bearer ${token}`, + }, }, - 5000, ); - // keep the imageUrl in localStorage so the user can try again if they want - return; + if (response.status === 204) { + // don't bother with a notification + // (either they'll simply continue or they're canceling and going back) + } else { + console.error("Non-success deleting image:", response); + this.$notify( + { + group: "alert", + type: "danger", + title: "Error", + text: "There was a problem deleting the image.", + }, + 5000, + ); + // keep the imageUrl in localStorage so the user can try again if they want + return; + } + + localStorage.removeItem("imageUrl"); + this.imageUrl = ""; + } catch (error) { + console.error("Error deleting image:", error); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + if ((error as any).response.status === 404) { + console.log("The image was already deleted:", error); + + localStorage.removeItem("imageUrl"); + this.imageUrl = ""; + + // it already doesn't exist so we won't say anything to the user + } else { + this.$notify( + { + group: "alert", + type: "danger", + title: "Error", + text: "There was an error deleting the image.", + }, + 5000, + ); + } } - localStorage.removeItem("imageUrl"); - this.imageUrl = ""; } async confirm() {