From 77bda6bf53c899fc91537d753dabf7d4a610eb32 Mon Sep 17 00:00:00 2001
From: Julia Friesel <julia.friesel@gmail.com>
Date: Tue, 7 Aug 2018 18:52:30 +0200
Subject: [PATCH] Make import errors prettier and actually handle them

---
 components/settings.js               | 19 +++++++++++--------
 lib/import-export/import-from-csv.js | 14 ++++----------
 2 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/components/settings.js b/components/settings.js
index 2021f62e..b7b56b6e 100644
--- a/components/settings.js
+++ b/components/settings.js
@@ -42,11 +42,11 @@ async function openShareDialogAndExport() {
   try {
     data = getDataAsCsvDataUri()
     if (!data) {
-      return Alert.alert(labels.errors.noData)
+      return alertError(labels.errors.noData)
     }
   } catch (err) {
     console.error(err)
-    return Alert.alert(labels.errors.couldNotConvert)
+    return alertError(labels.errors.couldNotConvert)
   }
 
   try {
@@ -59,7 +59,7 @@ async function openShareDialogAndExport() {
     })
   } catch (err) {
     console.error(err)
-    return Alert.alert(labels.errors.problemSharing)
+    return alertError(labels.errors.problemSharing)
   }
 }
 
@@ -83,14 +83,17 @@ async function getFileContentAndImport() {
   try {
     fileContent = await rnfs.readFile(fileInfo.uri, 'utf8')
   } catch (err) {
-    console.log(err)
-    return Alert.alert('Could not open file')
+    return alertError('Could not open file')
   }
 
   try {
-    importCsv(fileContent, false)
-    Alert.alert('Data successfully imported')
+    await importCsv(fileContent, false)
+    Alert.alert('Success', 'Data successfully imported')
   } catch(err) {
-    //TODO
+    alertError(err.message)
   }
 }
+
+function alertError(msg) {
+  Alert.alert('Error', msg)
+}
\ No newline at end of file
diff --git a/lib/import-export/import-from-csv.js b/lib/import-export/import-from-csv.js
index 50048fd7..698586dc 100644
--- a/lib/import-export/import-from-csv.js
+++ b/lib/import-export/import-from-csv.js
@@ -37,15 +37,9 @@ export default async function importCsv(csv, deleteFirst) {
     }, {})
   }
 
-  let cycleDays
-  try {
-    cycleDays = await csvParser(config)
-      .fromString(csv)
-      .on('header', validateHeaders)
-  } catch(err) {
-    // TODO
-    console.log(err)
-  }
+  const cycleDays = await csvParser(config)
+    .fromString(csv)
+    .on('header', validateHeaders)
 
   //remove symptoms where all fields are null
   putNullForEmptySymptoms(cycleDays)
@@ -72,7 +66,7 @@ function tryToCreateCycleDay(day, i) {
   try {
     db.create('CycleDay', day)
   } catch (err) {
-    const msg = `Error for line ${i + 1}(${day.date}): ${err.message}`
+    const msg = `Line ${i + 1}(${day.date}): ${err.message}`
     throw new Error(msg)
   }
 }
-- 
GitLab