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 message on failed data download #1001

Merged
merged 1 commit into from
Apr 7, 2025
Merged
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
36 changes: 32 additions & 4 deletions src/components/download/TimeSeriesFileDownloadComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { authenticationManager } from '@/services/authentication/AuthenticationM
import { downloadFileAttachment } from '@/lib/download/downloadFiles.ts'
import {
computed,
onMounted,
onUnmounted,
onUpdated,
ref,
Expand All @@ -75,6 +76,7 @@ import { DateTime } from 'luxon'
import { DataDownloadFilter } from '@/lib/download/types/DataDownloadFilter.ts'
import { useDownloadDialogStore } from '@/stores/downloadDialog'
import { createTransformRequestFn } from '@/lib/requests/transformRequest'
import { useAlertsStore } from '@/stores/alerts'

const store = useSystemTimeStore()
const downloadDialogStore = useDownloadDialogStore()
Expand All @@ -85,6 +87,12 @@ const viewPeriodFromStore = computed<UseTimeSeriesOptions>(() => {
}
})

const alertStore = useAlertsStore()
const userId = ref('')
onMounted(() => {
userId.value = crypto.randomUUID()
})

interface Props {
config?: DisplayConfig | null
options: UseDisplayConfigOptions
Expand Down Expand Up @@ -224,7 +232,7 @@ const downloadFile = (downloadFormat: DocumentFormat) => {
documentFormat: downloadFormat,
...viewPeriod,
})
return downloadFileAttachment(
return downloadFileSafe(
url.href,
fileName.value,
downloadFormat,
Expand All @@ -239,7 +247,7 @@ const downloadFile = (downloadFormat: DocumentFormat) => {
documentFormat: downloadFormat,
...viewPeriod,
})
return downloadFileAttachment(
return downloadFileSafe(
url.href,
fileName.value,
downloadFormat,
Expand All @@ -252,7 +260,7 @@ const downloadFile = (downloadFormat: DocumentFormat) => {
documentFormat: downloadFormat,
...viewPeriod,
})
return downloadFileAttachment(
return downloadFileSafe(
url.href,
fileName.value,
downloadFormat,
Expand All @@ -270,11 +278,31 @@ const downloadFile = (downloadFormat: DocumentFormat) => {
...viewPeriod,
}
const url = piProvider.timeSeriesTopologyActionsUrl(timeSeriesFilter)
return downloadFileAttachment(
return downloadFileSafe(
url.href,
fileName.value,
downloadFormat,
authenticationManager.getAccessToken(),
)
}

async function downloadFileSafe(
url: string,
fileName: string,
documentFormat: string,
accessToken: string,
) {
try {
await downloadFileAttachment(url, fileName, documentFormat, accessToken)
} catch (error) {
if (error instanceof Error) {
alertStore.addAlert({
id: `data-download-error-${userId.value}`,
type: 'error',
message: error.message,
})
downloadDialogStore.showDialog = false
}
}
}
</script>
25 changes: 11 additions & 14 deletions src/lib/download/downloadFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ async function downloadFileWithFetch(
const blob = await response.blob()
clickDownloadBlob(blob, fileName)
} else {
console.error('Error downloading file')
const message = await response.text()
throw new Error(message)
}
}

Expand All @@ -41,20 +42,16 @@ export async function downloadFileAttachment(
documentFormat: string,
accessToken: string,
) {
try {
const headers = new Headers()
if (accessToken) {
let extension: string = 'csv'
if (documentFormat === DocumentFormat.PI_JSON) extension = '.json'
if (documentFormat === DocumentFormat.PI_XML) extension = '.xml'
if (documentFormat === DocumentFormat.PI_CSV) extension = '.csv'
const downloadFileName = fileName + extension
await downloadFileWithFetch(headers, url, downloadFileName, accessToken)
}
if (!accessToken || accessToken == '') downloadWithLink(url, fileName)
} catch (error) {
console.error('Error downloading file:', error)
const headers = new Headers()
if (accessToken) {
let extension: string = 'csv'
if (documentFormat === DocumentFormat.PI_JSON) extension = '.json'
if (documentFormat === DocumentFormat.PI_XML) extension = '.xml'
if (documentFormat === DocumentFormat.PI_CSV) extension = '.csv'
const downloadFileName = fileName + extension
await downloadFileWithFetch(headers, url, downloadFileName, accessToken)
}
if (!accessToken || accessToken == '') downloadWithLink(url, fileName)
}

export async function downloadFileWithXhr(
Expand Down