From b8698ec6af64a1ff03f852b476b068e0be667df7 Mon Sep 17 00:00:00 2001 From: Daniil Tiuftin Date: Mon, 16 Dec 2024 17:13:26 +0300 Subject: [PATCH 1/3] catch xml parsing errors in transform() --- eo2js/src/commands/transpile.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/eo2js/src/commands/transpile.js b/eo2js/src/commands/transpile.js index bb66261..d6e6675 100644 --- a/eo2js/src/commands/transpile.js +++ b/eo2js/src/commands/transpile.js @@ -78,7 +78,12 @@ const needsRetranspile = function(source, transpiled) { */ const transform = function(tojo, options, transformations, parser) { const text = fs.readFileSync(tojo[verified]).toString() - let xml = parser.parse(text) + let xml; + try { + xml = parser.parse(text); + } catch (e) { + throw new Error(`Failed to parse XML for ${tojo[verified]}: ${e.message}`); + } const pth = pathFromName(xml['program']['@_name']) const isTest = hasMeta(xml, 'tests') const transpiled = path.resolve(options.target, dir, `${pth}.xmir`) From ebef1bcee7a666e1484a80595b70fb9922fe250a Mon Sep 17 00:00:00 2001 From: Daniil Tiuftin Date: Mon, 16 Dec 2024 17:14:45 +0300 Subject: [PATCH 2/3] catch other errors in transform() --- eo2js/src/commands/transpile.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/eo2js/src/commands/transpile.js b/eo2js/src/commands/transpile.js index d6e6675..880caa5 100644 --- a/eo2js/src/commands/transpile.js +++ b/eo2js/src/commands/transpile.js @@ -88,7 +88,13 @@ const transform = function(tojo, options, transformations, parser) { const isTest = hasMeta(xml, 'tests') const transpiled = path.resolve(options.target, dir, `${pth}.xmir`) const dest = path.resolve(options.project, `${pth}${isTest ? '.test' : ''}.js`) - if (needsRetranspile(tojo[verified], transpiled)) { + if (!needsRetranspile(tojo[verified], transpiled)) { + if (options.verbose) { + console.log(`Skipping ${pth} - already transpiled`); + } + return; + } + try { makeDirIfNotExist(transpiled.substring(0, transpiled.lastIndexOf(path.sep))) fs.writeFileSync(transpiled, text) xml = text @@ -113,8 +119,8 @@ const transform = function(tojo, options, transformations, parser) { fs.writeFileSync(dest, first['javascript']) filtered.slice(1).forEach((obj) => fs.appendFileSync(dest, `\n${obj['javascript']}`)) } - } else if (options.verbose) { - console.log(`Skipping ${pth} - already transpiled`) + } catch (e) { + throw new Error(`Error transforming ${tojo[verified]}: ${e.message}`); } } From 44e57354d7f509f4c6dbfb80bb62907a7e061007 Mon Sep 17 00:00:00 2001 From: Daniil Tiuftin Date: Mon, 16 Dec 2024 17:19:07 +0300 Subject: [PATCH 3/3] improve error messages in transpile() --- eo2js/src/commands/transpile.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eo2js/src/commands/transpile.js b/eo2js/src/commands/transpile.js index 880caa5..ff60959 100644 --- a/eo2js/src/commands/transpile.js +++ b/eo2js/src/commands/transpile.js @@ -133,10 +133,10 @@ const transpile = function(options) { const foreign = path.resolve(options['target'], options['foreign']) console.log(`Reading foreign tojos from: ${foreign}`) if (!fs.existsSync(foreign)) { - throw new Error(`File ${foreign} is not found`) + throw new Error(`File not found: ${foreign}. Ensure the file exists and is accessible.`) } if (!foreign.endsWith('.json')) { - throw new Error(`Only .json foreign tojos file is supported, given ${foreign.substring(foreign.lastIndexOf(path.sep))}`) + throw new Error(`Invalid foreign tojo file format: ${foreign.substring(foreign.lastIndexOf(path.sep))}. Only .json files are supported.`); } const transformations = [ 'objects', 'package', 'tests', 'attrs', 'data', 'to-js'