Skip to content

Commit

Permalink
fix: add error handling of version file fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
olavis committed Feb 28, 2023
1 parent 2179e13 commit a4f6889
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions web/src/common/components/VersionText.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Typography } from '@equinor/eds-core-react'
import axios, { AxiosResponse } from 'axios'
import axios, { AxiosError, AxiosResponse } from 'axios'
import { useEffect, useState } from 'react'

type CommitInfo = {
Expand All @@ -16,12 +16,20 @@ const useCommitInfo = () => {
})

useEffect(() => {
axios.get('version.txt').then((response: AxiosResponse<string>) => {
const versionFile: { [key: string]: string } = Object.fromEntries(
response.data.split('\n').map((line) => line.split(': '))
)
setCommitInfo(versionFile as CommitInfo)
})
const fetchVersionFile = () =>
axios
.get('version.txt')
.then((res: AxiosResponse<string>) =>
Object.fromEntries(
res.data.split('\n').map((line) => line.split(': '))
)
)
.catch((error: AxiosError) => {
throw new Error(
`Could not read version file, ${error.response?.data ?? error}`
)
})
fetchVersionFile().then((commitInfo) => setCommitInfo(commitInfo))
}, [])

return commitInfo
Expand Down

0 comments on commit a4f6889

Please sign in to comment.