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 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'viewer/src/store') 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(); + } } -- cgit v1.2.3