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

[Netmanager] Proper error handling for data export #2306

Merged
merged 2 commits into from
Dec 10, 2024
Merged
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
22 changes: 13 additions & 9 deletions netmanager/src/views/pages/ExportData/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,19 @@ const ExportData = (props) => {
let filename = `airquality-data.${fileType.value}`;

// Check if response has data
if (response && response.data) {
if (response) {
if (fileType.value === 'csv') {
if (typeof response !== 'string') {
throw new Error('Invalid CSV data format.');
}
exportData(response, filename, 'text/csv;charset=utf-8;');
}

if (fileType.value === 'json') {
const jsonString = JSON.stringify(response.data);
exportData(jsonString, filename, 'application/json');
}

if (fileType.value === 'csv') {
// Convert JSON data to CSV using Papa Parse
const csvData = Papa.unparse(response.data);
exportData(csvData, filename, 'text/csv;charset=utf-8;');
}

dispatch(
updateMainAlert({
message: 'Air quality data download successful',
Expand Down Expand Up @@ -467,7 +468,10 @@ const ExportData = (props) => {
);
return;
}
errorMessage = err.response.data.message;
errorMessage =
typeof err.response.data.message === 'string'
? err.response.data.message
: 'An error occurred while downloading data';
}
} else if (err.request) {
// Request made but no response
Expand Down Expand Up @@ -530,7 +534,7 @@ const ExportData = (props) => {
startDateTime: roundToStartOfDay(new Date(startDate).toISOString()),
endDateTime: roundToEndOfDay(new Date(endDate).toISOString()),
sites: sitesList,
devices: getValues(selectedDevices),
device_names: getValues(selectedDevices),
airqlouds: getValues(selectedAirqlouds),
network: activeNetwork.net_name,
datatype: dataType.value,
Expand Down
Loading