Skip to content

Commit

Permalink
Show error when backend is unavalaible
Browse files Browse the repository at this point in the history
  • Loading branch information
hvangeffen committed May 29, 2024
1 parent a3e9424 commit e8e608e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/layouts/DefaultLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@
<SplashScreen v-if="splashSrc" :img-url="splashSrc" :version="version" />
<GlobalSearchComponent />
</v-main>
<v-snackbar
v-model="showBackendError"
timeout="-1"
variant="text"
location="top"
>
<v-alert type="error">Backend is unavailable</v-alert>
</v-snackbar>
</v-layout>
</template>

Expand All @@ -162,9 +170,10 @@ import SplashScreen from '@/components/general/SplashScreen.vue'
import GlobalSearchComponent from '@/components/general/GlobalSearchComponent.vue'
import { configManager } from '@/services/application-config'
import { getResourcesStaticUrl } from '@/lib/fews-config'
import { getResourcesStaticUrl, isBackendAvailable } from '@/lib/fews-config'
import { StyleValue, nextTick } from 'vue'
import packageConfig from '../../package.json'
import { asyncComputed } from '@vueuse/core'
const configStore = useConfigStore()
const alertsStore = useAlertsStore()
Expand All @@ -181,6 +190,11 @@ const logoSrc = ref('')
const splashSrc = ref<string>()
const appBarStyle = ref<StyleValue>()
const appBarColor = ref<string>('')
const HEALTH_CHECK_TIMEOUT = 5000
const showBackendError = asyncComputed(
async () => !(await isBackendAvailable(HEALTH_CHECK_TIMEOUT)),
false,
)
function updateAppBarColor() {
appBarColor.value = getComputedStyle(document.body).getPropertyValue(
Expand Down
26 changes: 26 additions & 0 deletions src/lib/fews-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,29 @@ export function getResourcesIconsUrl(resource: string) {
})
return webServiceProvider.resourcesIconsUrl(resource).toString()
}

export async function isBackendAvailable(timeout?: number) {
const baseUrl = configManager.get('VITE_FEWS_WEBSERVICES_URL')

const controller = new AbortController()
const DEFAULT_TIMEOUT = 5000
const timeoutId = setTimeout(
() => controller.abort(),
timeout ?? DEFAULT_TIMEOUT,
)

const webServiceProvider = new PiWebserviceProvider(baseUrl, {
transformRequestFn: createTransformRequestFn(controller),
})

try {
await webServiceProvider.getVersion()
} catch (e) {
if (e instanceof Error && e.name === 'AbortError') {
return false
}
}

clearTimeout(timeoutId)
return true
}

0 comments on commit e8e608e

Please sign in to comment.