Skip to content

Commit

Permalink
[FIX] XMLTemplateAnalyzer: Include document name for XML parsing errors
Browse files Browse the repository at this point in the history
Also properly wait for the asynchronous analyze function to prevent
unhandled promise rejections and possible race conditions.

Change wording of an unrelated error message.

Resolves SAP/ui5-tooling#539
  • Loading branch information
RandomByte committed Jul 21, 2021
1 parent 1e04478 commit 3c88ca4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/lbt/analyzer/XMLTemplateAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class XMLTemplateAnalyzer {
_analyze(xml, info, isFragment) {
if ( this.busy ) {
// TODO delegate to fresh instances instead
throw new Error("analyzer is busy");
throw new Error("XMLTemplateAnalyzer is unexpectedly busy");
}

this.info = info;
Expand All @@ -160,7 +160,7 @@ class XMLTemplateAnalyzer {
// parse error
if ( err ) {
this.busy = false;
reject(err);
reject(new Error(`Error while parsing XML document ${info.name}: ${err.message}`));
return;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/lbt/resources/ResourcePool.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ async function determineDependencyInfo(resource, rawInfo, pool) {
} else if ( /\.view.xml$/.test(resource.name) ) {
const xmlView = await resource.buffer();
info.size = xmlView.length;
new XMLTemplateAnalyzer(pool).analyzeView(xmlView, info);
await new XMLTemplateAnalyzer(pool).analyzeView(xmlView, info);
} else if ( /\.control.xml$/.test(resource.name) ) {
const xmlView = await resource.buffer();
info.size = xmlView.length;
new XMLTemplateAnalyzer(pool).analyzeFragment(xmlView, info);
await new XMLTemplateAnalyzer(pool).analyzeFragment(xmlView, info);
} else if ( /\.fragment.xml$/.test(resource.name) ) {
const xmlView = await resource.buffer();
info.size = xmlView.length;
new XMLTemplateAnalyzer(pool).analyzeFragment(xmlView, info);
await new XMLTemplateAnalyzer(pool).analyzeFragment(xmlView, info);
}

return info;
Expand Down
9 changes: 6 additions & 3 deletions test/lib/lbt/analyzer/XMLTemplateAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,11 @@ test("_analyze: parseString error", async (t) => {
const analyzer = new XMLTemplateAnalyzer();
sinon.stub(analyzer._parser, "parseString").callsArgWith(1, new Error("my-error"), "result");

const error = await t.throwsAsync(analyzer._analyze());
t.deepEqual(error.message, "my-error");
const moduleInfo = {
name: "my.fragment.xml"
};
const error = await t.throwsAsync(analyzer._analyze(null, moduleInfo));
t.deepEqual(error.message, "Error while parsing XML document my.fragment.xml: my-error");
t.false(analyzer.busy, "busy state is restored");
});

Expand All @@ -391,7 +394,7 @@ test("_analyze: call twice to simulate busy", async (t) => {
const error = t.throws(()=> {
analyzer._analyze(null, moduleInfo, true);
});
t.deepEqual(error.message, "analyzer is busy");
t.deepEqual(error.message, "XMLTemplateAnalyzer is unexpectedly busy");

await resultPromise;
t.false(analyzer.busy, "busy state is reset after promise resolves");
Expand Down

0 comments on commit 3c88ca4

Please sign in to comment.