From 7a8eba922ad34182628f80cf2496d8654abe91e6 Mon Sep 17 00:00:00 2001 From: Zero~Informatique Date: Sat, 12 Sep 2020 06:26:39 +0200 Subject: viewer: improved network errors handling --- viewer/src/store/galleryStore.ts | 15 +++++++++++---- viewer/src/views/MainLayout.vue | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-) (limited to 'viewer/src') diff --git a/viewer/src/store/galleryStore.ts b/viewer/src/store/galleryStore.ts index b672551..5d599aa 100644 --- a/viewer/src/store/galleryStore.ts +++ b/viewer/src/store/galleryStore.ts @@ -85,20 +85,22 @@ export default class GalleryStore extends VuexModule { // Fetches the gallery's JSON config @action async fetchConfig() { - return fetch(`${process.env.VUE_APP_DATA_URL}${GalleryStore.getUrlConfig()}`, { cache: "no-cache" }) - .then(response => response.json()) + await fetch(`${process.env.VUE_APP_DATA_URL}${GalleryStore.getUrlConfig()}`, { cache: "no-cache" }) + .then(GalleryStore.responseToJson) .then(this.setConfig); + return this.config!; } // Fetches the gallery's JSON metadata @action async fetchGalleryItems() { const root = this.config?.galleryRoot ?? ""; const index = this.config?.galleryIndex ?? "index.json"; - return fetch(`${process.env.VUE_APP_DATA_URL}${root}${index}`, { cache: "no-cache" }) - .then(response => response.json()) + await fetch(`${process.env.VUE_APP_DATA_URL}${root}${index}`, { cache: "no-cache" }) + .then(GalleryStore.responseToJson) .then(this.setGalleryIndex) .then(this.indexTags) .then(this.indexTagCategories); + return this.galleryIndex!; } // Indexes the gallery @@ -128,4 +130,9 @@ export default class GalleryStore extends VuexModule { if (search.length > 1) return search.substr(1) + ".json"; return "config.json"; } + + private static responseToJson(response: Response) { + if (!response.ok) throw new Error(`${response.status}: ${response.statusText}`); + return response.json(); + } } diff --git a/viewer/src/views/MainLayout.vue b/viewer/src/views/MainLayout.vue index 21c9cb6..d9ae954 100644 --- a/viewer/src/views/MainLayout.vue +++ b/viewer/src/views/MainLayout.vue @@ -70,12 +70,12 @@ export default class MainLayout extends Vue { } get isReady() { - return !this.isLoading && this.$galleryStore.currentPath !== null; + return !this.isLoading && this.$galleryStore.config && this.$galleryStore.currentPath !== null; } displayError(reason: any) { this.$buefy.snackbar.open({ - message: `Error ${reason}`, + message: `${reason}`, actionText: "Retry", position: "is-top", type: "is-danger", -- cgit v1.2.3