Working error handling; refactored a bit but same functionality

This commit is contained in:
efarooqui
2021-02-09 16:50:58 -08:00
parent 7c963eb1d0
commit 3c391e5dfa
2 changed files with 11 additions and 8 deletions

View File

@@ -59,13 +59,13 @@ class Controller {
indexedData[item] = results[idx] indexedData[item] = results[idx]
}) })
try { try {
await exportToFile(fileDest, indexedData) const message = await exportToFile(fileDest, indexedData)
return copy.success.export(fileDest) return message
} catch (e) { } catch (e) {
return Promise.reject(e) return Promise.reject(e)
} }
} else { } else {
return Promise.reject(new Error(copy.errors.export.writeFailed)) throw new Error(copy.errors.export.writeFailed)
} }
}) })
} }

View File

@@ -1,5 +1,5 @@
import R from 'ramda' import R from 'ramda'
import fs from 'file-system' import { promises as fs } from 'file-system'
import copy from '../copy/en' import copy from '../copy/en'
/* eslint-disable */ /* eslint-disable */
@@ -16,13 +16,16 @@ function camelize (str) {
}) })
} }
export function exportToFile(fileDest, data) { export async function exportToFile(fileDest, data) {
const stringifiedData = JSON.stringify(data, null, 2) const stringifiedData = JSON.stringify(data, null, 2)
const filePath = `${fileDest}/export.json` const filePath = `${fileDest}/export.json`
fs.writeFile(filePath, stringifiedData, (err) => { try {
if (err) throw new Error(copy.errors.export.writeFailed) await fs.writeFile(filePath, stringifiedData)
}) return copy.success.export(filePath)
} catch (err) {
throw new Error(copy.errors.export.writeFailed)
}
} }
export const fmtObj = R.curry( export const fmtObj = R.curry(