From d71a9e58a8a52118223930f34f3e0644781537bd Mon Sep 17 00:00:00 2001 From: Tino Hager Date: Wed, 27 Nov 2024 16:18:17 +0100 Subject: [PATCH] optimize axios error handling --- .../js/composables/album/albumRefresher.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/resources/js/composables/album/albumRefresher.ts b/resources/js/composables/album/albumRefresher.ts index 39035fb48d4..ae32612cef1 100644 --- a/resources/js/composables/album/albumRefresher.ts +++ b/resources/js/composables/album/albumRefresher.ts @@ -1,6 +1,7 @@ import AlbumService from "@/services/album-service"; import { AuthStore } from "@/stores/Auth"; import { computed, Ref, ref } from "vue"; +import axios, { AxiosError } from 'axios' export function useAlbumRefresher(albumId: Ref, auth: AuthStore, isLoginOpen: Ref, nsfw_consented: Ref) { const isPasswordProtected = ref(false); @@ -39,13 +40,16 @@ export function useAlbumRefresher(albumId: Ref, auth: AuthStore, isLogin } photos.value = album.value?.photos ?? []; isAlbumConsented.value = nsfw_consented.value.find((e) => e === albumId.value) !== undefined; - } catch (error) { - if (error.response.status === 401 && error.response.data.message === "Password required") { - isPasswordProtected.value = true; - } else if (error.response.status === 403 && error.response.data.message === "Password required") { - isPasswordProtected.value = true; - } else if (error.response.status === 401) { - isLoginOpen.value = true; + } catch (error: unknown) { + if (axios.isAxiosError(error)) { + const axiosError = error as AxiosError; + if (axiosError.response?.status === 401 && axiosError.response?.data?.message === "Password required") { + isPasswordProtected.value = true; + } else if (axiosError.response?.status === 403 && axiosError.response?.data?.message === "Password required") { + isPasswordProtected.value = true; + } else if (axiosError.response?.status === 401) { + isLoginOpen.value = true; + } } else { console.error(error); }