Skip to content

Commit

Permalink
handle fetch error
Browse files Browse the repository at this point in the history
  • Loading branch information
f-w committed Aug 10, 2023
1 parent 61e4e15 commit cc9037b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions client/src/components/shared/combo-table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@
color="red darken-2"
variant="text"
@click="deleteItemConfirm"
>OK</v-btn
>Yes</v-btn
>
<v-btn color="primary" variant="text" @click="deleteItemCancel"
>Cancel</v-btn
>No</v-btn
>
<v-spacer></v-spacer>
</v-card-actions>
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/shared/editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<template>
<div>
<div id="nb-item-editor"></div>
<v-alert color="red" dense type="error" v-if="errorMessage">
<v-alert closable color="red" dense type="error" v-if="errorMessage">
{{ errorMessage }}
</v-alert>
<v-btn color="primary" @click="setCurrentlyEditedItem">save</v-btn>
Expand Down
41 changes: 33 additions & 8 deletions client/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const useDefaultStore = defineStore('default', {
}),
actions: {
setLocalItems(payload) {
this[payload.model].items = payload.items;
this[payload.model].items = payload?.items;
},
setTotalItemCount(payload) {
this[payload.model].totalCount = payload.cnt;
Expand Down Expand Up @@ -111,7 +111,10 @@ export const useDefaultStore = defineStore('default', {
};
const url = apiUrlPrefix + '/' + payload.model + (id ? '/' + id : '');
await setAuthorizationHeader(req, this);
await fetch(url, req);
const res = await fetch(url, req);
if (!res?.ok) {
throw `${res.status}: ${res.statusText}`;
}
},

async deleteItem(payload) {
Expand All @@ -120,7 +123,10 @@ export const useDefaultStore = defineStore('default', {
method: 'DELETE',
};
await setAuthorizationHeader(req, this);
await fetch(url, req);
const res = await fetch(url, req);
if (!res?.ok) {
throw `${res.status}: ${res.statusText}`;
}
},

async fetchItems(payload) {
Expand All @@ -142,7 +148,11 @@ export const useDefaultStore = defineStore('default', {
await setAuthorizationHeader(req, this);
let items;
try {
items = await (await fetch(url, req)).json();
const res = await fetch(url, req);
if (!res?.ok) {
throw `${res.status}: ${res.statusText}`;
}
items = await res.json();
} catch (ex) {
this.setLocalItems({model: payload.model, items: []});
this.setTotalItemCount({model: payload.model, cnt: undefined});
Expand All @@ -155,7 +165,11 @@ export const useDefaultStore = defineStore('default', {
}
req = {};
await setAuthorizationHeader(req, this);
let response = await (await fetch(url, req)).json();
const res = await fetch(url, req);
if (!res?.ok) {
throw `${res.status}: ${res.statusText}`;
}
let response = await res.json();
this.setTotalItemCount({
model: payload.model,
cnt: response.count,
Expand All @@ -165,14 +179,21 @@ export const useDefaultStore = defineStore('default', {
let url = apiUrlPrefix + '/subscriptions/services';
let req = {};
await setAuthorizationHeader(req, this);
let res = await fetch(url, req);
const res = await fetch(url, req);
if (!res?.ok) {
throw `${res.status}: ${res.statusText}`;
}
return res.json();
},
async getAuthenticationStrategy() {
let url = apiUrlPrefix + '/administrators/whoami';
let req = {};
await setAuthorizationHeader(req, this);
let res = await (await fetch(url, req)).json();
let res = await fetch(url, req);
if (!res?.ok) {
throw `${res.status}: ${res.statusText}`;
}
res = await res.json();
this.setAuthnStrategy(res.authnStrategy);
},
async login(payload) {
Expand All @@ -188,7 +209,11 @@ export const useDefaultStore = defineStore('default', {
},
};
await setAuthorizationHeader(req, this);
let res = await (await fetch(url, req)).json();
let res = await fetch(url, req);
if (!res?.ok) {
throw `${res.status}: ${res.statusText}`;
}
res = await res.json();
this.setAccessToken(res.token);
},
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notify-bc",
"version": "4.1.8",
"version": "4.1.9",
"dbSchemaVersion": "0.8.0",
"description": "A versatile notification API server",
"keywords": [
Expand Down

0 comments on commit cc9037b

Please sign in to comment.