Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show error when backend is unavailable #876

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading