Skip to content

Commit

Permalink
Merge branch 'release/1.2.4-beta'
Browse files Browse the repository at this point in the history
  • Loading branch information
mattRedBox committed Jul 24, 2020
2 parents 37aff73 + e380003 commit a174518
Show file tree
Hide file tree
Showing 5 changed files with 939 additions and 868 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@




[![Unit Test Coverage Status](https://coveralls.io/repos/github/qcif/data-curator/badge.svg?branch=ci)](https://coveralls.io/github/qcif/data-curator?branch=ci)
[![Github All Releases](https://img.shields.io/github/downloads/qcif/data-curator/total.svg)](https://github.com/qcif/data-curator/releases)
[![Github Releases](https://img.shields.io/github/downloads/qcif/data-curator/latest/total.svg?label=Latest%20version%20downloads)](https://github.com/qcif/data-curator/releases/latest)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "DataCurator",
"version": "1.2.3-beta",
"version": "1.2.4-beta",
"author": " <matt@redboxresearchdata.com.au>",
"description": "Data Curator is a simple desktop CSV editor to help describe, validate and share usable open data",
"license": "MIT",
Expand Down
32 changes: 22 additions & 10 deletions src/renderer/frictionlessDataPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import tabStore from '@/store/modules/tabs.js'
import hotStore from '@/store/modules/hots.js'
import path from 'path'
import { createJsonFile, createZipFile } from '@/exportPackage.js'
import { getValidNames, hasAllColumnNames } from '@/frictionlessUtilities.js'
import {
getValidNames,
hasAllColumnNames,
addErrorCauseToMessage,
handleFrictionlessError
} from './frictionlessUtilities'
import _ from 'lodash'

export async function createDataPackageAsZippedResources () {
Expand All @@ -25,26 +30,36 @@ export async function createDataPackage (postCreateFunc) {
try {
let dataPackage = await buildDataPackage(errorMessages)
if (_.isEmpty(errorMessages) && dataPackage) {
dataPackage.commit()
commitPackage(dataPackage)
if (dataPackage.valid) {
postCreateFunc(dataPackage.descriptor)
} else {
errorMessages.push('There is a problem with at least 1 package property. Please check and try again.')
}
}
} catch (err) {
console.error('There was an error creating the data package.', err)
let errorMessage = 'There was an error creating the data package.'
errorMessages.push(addErrorCauseToMessage(err, errorMessage))
console.error(errorMessage, err)
}
return errorMessages
}

export function commitPackage (dataPackage) {
try {
dataPackage.commit()
} catch (err) {
handleFrictionlessError(err)
}
}

export function haveAllTabsGotFilenames () {
return tabStore.getters.getTabFilenames(tabStore.state).length === tabStore.state.tabs.length
}

async function buildDataPackage (errorMessages) {
auditPackageRequirements(errorMessages)
let dataPackage = await initPackage()
let dataPackage = await Package.load()
await buildAllResourcesForDataPackage(dataPackage, errorMessages)
// adding package properties for validation only
addPackageProperties(dataPackage.descriptor)
Expand All @@ -69,11 +84,6 @@ function auditPackageRequirements (requiredMessages) {
}
}

async function initPackage () {
const dataPackage = await Package.load()
return dataPackage
}

function addPackageProperties (descriptor) {
let packageProperties = _.cloneDeep(hotStore.state.packageProperties)
_.merge(descriptor, packageProperties)
Expand All @@ -98,7 +108,9 @@ async function buildAllResourcesForDataPackage (dataPackage, errorMessages) {
resourcePaths.push(resource.descriptor.path)
dataPackage.addResource(resource.descriptor)
} catch (err) {
console.error('There was an error creating a resource.', err)
let errorMessage = 'There was an error creating a resource.'
errorMessages.push(addErrorCauseToMessage(err, errorMessage))
console.error(errorMessage, err)
break
}
}
Expand Down
58 changes: 58 additions & 0 deletions src/renderer/frictionlessUtilities.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import store from '@/store/modules/hots.js'
import { DEFAULT_DIALECT } from 'datapackage/lib/config.js'
import { TableSchemaError } from 'datapackage'
import _ from 'lodash'

const errorMatchMappings = [
{
errorClass: TableSchemaError,
messagePatterns: [/Tabular format.*tsv.*is not supported/gi]
}
]

export function includeHeadersInData (hot) {
let allData = hot.getData()
Expand Down Expand Up @@ -44,3 +53,52 @@ export function isCaseSensitive (hotId) {
}
return caseSensitiveHeader
}

export function handleFrictionlessError (error) {
if (!isErrorWhitelisted(error)) {
throw error
} else {
console.warn('Recovered from frictionless library problem.', error)
}
}

export function isErrorWhitelisted (error) {
let isWhiteListed = false
let extraErrorMessage = extractMessageFromFrictionlessError(error)
if (!_.isEmpty(extraErrorMessage)) {
_.each(errorMatchMappings, function (next) {
if (error instanceof next.errorClass) {
for (const messagePattern of next.messagePatterns) {
let indexFound = extraErrorMessage.search(messagePattern)
if (indexFound > -1) {
isWhiteListed = true
break
}
}
}
})
}
return isWhiteListed
}

export function addErrorCauseToMessage (error, errorMessage) {
let errorCauseMessage = extractMessageFromFrictionlessError(error)
return addExtraToErrorMessage(errorCauseMessage, errorMessage)
}

export function extractMessageFromFrictionlessError (error) {
let errorMessage = _.trim(_.get(error, 'message', ''))
if (_.isEmpty(errorMessage)) {
if (_.isArray(error) && !_.isEmpty(error)) {
errorMessage = _.trim(_.get(error[0], 'message', ''))
}
}
return errorMessage
}

export function addExtraToErrorMessage (extraMessage, errorMessage) {
if (!_.isEmpty(_.trim(extraMessage))) {
errorMessage = `${errorMessage}\n (${extraMessage})`
}
return errorMessage
}
Loading

0 comments on commit a174518

Please sign in to comment.