Skip to content

Commit

Permalink
[import] Fix JSON parse errors not being emitted (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Mar 27, 2018
1 parent 561fca9 commit 7b7efe2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 9 additions & 6 deletions packages/@sanity/import/src/importFromStream.js
Expand Up @@ -15,18 +15,19 @@ module.exports = (stream, options, importers) =>
debug('Importing from stream')

let isTarStream = false
let jsonDocuments

miss.pipe(stream, gunzipMaybe(), untarMaybe(), err => {
if (err) {
reject(err)
return
}

if (!isTarStream) {
return // Will be resolved by concatenation
if (isTarStream) {
findAndImport()
} else {
resolve(importers.fromArray(jsonDocuments, options))
}

findAndImport()
})

function untarMaybe() {
Expand All @@ -38,13 +39,15 @@ module.exports = (stream, options, importers) =>
}

debug('Stream is an ndjson file, streaming JSON')
return swap(null, miss.pipeline(getJsonStreamer(), miss.concat(resolveNdjsonStream)))
const ndjsonStream = miss.pipeline(getJsonStreamer(), miss.concat(resolveNdjsonStream))
ndjsonStream.on('error', reject)
return swap(null, ndjsonStream)
})
}

function resolveNdjsonStream(documents) {
debug('Finished reading ndjson stream')
resolve(importers.fromArray(documents, options))
jsonDocuments = documents
}

async function findAndImport() {
Expand Down
9 changes: 8 additions & 1 deletion packages/@sanity/import/src/util/getJsonStreamer.js
Expand Up @@ -21,9 +21,16 @@ module.exports = function getJsonStreamer() {

return doc
} catch (err) {
this.emit('error', new Error(`Failed to parse line #${lineNumber}: ${err.message}`))
this.emit('error', new Error(getErrorMessage(err)))
}

return undefined
}

function getErrorMessage(err) {
const suffix =
lineNumber === 1 ? '\n\nMake sure this is valid ndjson (one JSON-document *per line*)' : ''

return `Failed to parse line #${lineNumber}: ${err.message}${suffix}`
}
}

0 comments on commit 7b7efe2

Please sign in to comment.