Skip to content

Commit

Permalink
Add error messagen when network communication fails
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjelonek committed Sep 5, 2023
1 parent 25b2713 commit 47c767f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 32 deletions.
5 changes: 4 additions & 1 deletion src/BakrepApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ class BakrepApiImpl implements BakrepApi {
}

toJson(r: Response): Promise<any> {
if (!r.ok) throw r.text().then((t) => Promise.reject(t));
if (!r.ok)
return r
.text()
.then((t) => Promise.reject(`${r.status}: ${r.statusText}\n${t}`));
return r.text().then(json5.parse);
}

Expand Down
12 changes: 10 additions & 2 deletions src/PageState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export enum State {

export interface PageState {
state: State;
message: string | undefined;
setError(message: string): void;
setState(state: State): void;
loading: boolean;
loaded: boolean;
Expand All @@ -21,14 +23,15 @@ export interface PageState {

export default function usePageState(): Ref<PageState> {
const state: Ref<State> = ref(State.Main);
const message: Ref<undefined | string> = ref();
const loading = computed(
() => state.value === "loading" || state.value === "exporting"
() => state.value === "loading" || state.value === "exporting",
);
const loaded = computed(
() =>
state.value === "main" ||
state.value === "saving" ||
state.value === "saved"
state.value === "saved",
);

const saved = computed(() => state.value === "saved");
Expand All @@ -37,7 +40,12 @@ export default function usePageState(): Ref<PageState> {

const pageState = ref({
state: state,
message: message,
setState: (newState: State) => (state.value = newState),
setError: (newMessage: string) => {
message.value = newMessage;
state.value = State.Error;
},
loading: loading,
loaded: loaded,
saved: saved,
Expand Down
3 changes: 3 additions & 0 deletions src/components/Loading.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<CenteredLargeSpinner />
</slot>
</template>
<template v-else-if="state.error">
<div class="alert alert-danger">Error: {{ state.message }}</div>
</template>
<template v-else>
<slot name="content" />
<slot />
Expand Down
14 changes: 9 additions & 5 deletions src/views/search/SearchView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ const ordering: Ref<SortOption[]> = ref([{ field: "id", ord: "asc" }]);
const searchinfo: Ref<SearchInfo> = ref({ fields: [] });
function init() {
pageState.value.setState(State.Loading);
api.searchinfo().then((r) => {
searchinfo.value = r;
pageState.value.setState(State.Main);
});
api
.searchinfo()
.then((r) => {
searchinfo.value = r;
pageState.value.setState(State.Main);
})
.catch((err) => pageState.value.setError(err));
}
function searchinfo2querybuilderrules(f: SearchInfoField): Rule {
Expand Down Expand Up @@ -103,7 +106,8 @@ function search(offset = 0) {
searchState.value.setState(State.Main);
pagination.value.offset = r.offset;
pagination.value.total = r.total;
});
})
.catch((err) => pageState.value.setError(err));
}
const positionInResults: Ref<PositionInResult> = computed(() =>
Expand Down
50 changes: 26 additions & 24 deletions src/views/show-results/ResultView.vue
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
<script setup lang="ts">
import { computed, onMounted, ref, type Ref } from "vue";
import usePageState, { State } from "@/PageState";
import DownloadLinks from "@/components/DownloadLinks.vue";
import Loading from "@/components/Loading.vue";
import Pane from "@/components/Pane.vue";
import DownloadLinks from "@/components/DownloadLinks.vue";
import usePageState, { State } from "@/PageState";
import { computed, onMounted, ref, type Ref } from "vue";
import { useApi } from "@/BakrepApi";
import type { Dataset } from "@/model/Dataset";
import { useRoute } from "vue-router";
import DatasetSummary from "./DatasetSummary.vue";
import GtdbtkTaxonomy from "./GtdbtkTaxonomy.vue";
import DisplayAssembly from "./DisplayAssembly.vue";
import type { BaktaResult } from "@/model/BaktaResults";
import type { GtdbtkResult } from "@/model/GtdbtkResult";
import type { CheckmResult } from "@/model/CheckmResults";
import FeatureTable from "./FeatureTable.vue";
import type { Dataset } from "@/model/Dataset";
import type { GtdbtkResult } from "@/model/GtdbtkResult";
import type { MlstResult } from "@/model/MlstResults";
import { useRoute } from "vue-router";
import BaktaAnnotationTable from "./bakta/BaktaAnnotationTable.vue";
import BaktaGenomeViewer from "./bakta/BaktaGenomeViewer.vue";
import BaktaStats from "./bakta/BaktaStats.vue";
import CenteredLargeSpinner from "@/components/CenteredLargeSpinner.vue";
import type { MlstResult } from "@/model/MlstResults";
import DatasetSummary from "./DatasetSummary.vue";
import DisplayAssembly from "./DisplayAssembly.vue";
import FeatureTable from "./FeatureTable.vue";
import GtdbtkTaxonomy from "./GtdbtkTaxonomy.vue";
const route = useRoute();
const id = computed(() => route.params.id as string);
const api = useApi();
Expand All @@ -31,18 +30,21 @@ const checkmResult: Ref<CheckmResult | undefined> = ref();
const mlstResult: Ref<MlstResult | undefined> = ref();
function loadData() {
return api.getDataset(id.value).then((x) => {
dataset.value = x;
return Promise.all([
api.fetchBaktaResult(x).then((r) => (baktaResult.value = r)),
api.fetchGtdbtkResult(x).then((r) => (gtdbtkResult.value = r)),
api.fetchCheckmResult(x).then((r) => (checkmResult.value = r)),
api.fetchMlstResult(x).then((r) => (mlstResult.value = r)),
]).then(() => {
active_tab.value = "summary";
state.value.setState(State.Main);
});
});
return api
.getDataset(id.value)
.then((x) => {
dataset.value = x;
return Promise.all([
api.fetchBaktaResult(x).then((r) => (baktaResult.value = r)),
api.fetchGtdbtkResult(x).then((r) => (gtdbtkResult.value = r)),
api.fetchCheckmResult(x).then((r) => (checkmResult.value = r)),
api.fetchMlstResult(x).then((r) => (mlstResult.value = r)),
]).then(() => {
active_tab.value = "summary";
state.value.setState(State.Main);
});
})
.catch((err) => state.value.setError(err));
}
onMounted(loadData);
Expand Down

0 comments on commit 47c767f

Please sign in to comment.