Skip to content

Commit 041c89f

Browse files
authored
[ui5-builder][INTERNAL] bundleBuilder: log verbose for json parse failure (#502)
When JSON content is parsed for minification, errors are logged using verbose level, because the content is not relevant, only for minification.
1 parent 314a902 commit 041c89f

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/builder/lib/lbt/bundle/Builder.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ class BundleBuilder {
411411
try {
412412
fileContent = JSON.stringify( JSON.parse( fileContent) );
413413
} catch (e) {
414-
log.error(e);
414+
log.verbose("Failed to parse JSON file %s. Ignoring error, skipping compression.", module);
415+
log.verbose(e);
415416
}
416417
}
417418
outW.write(makeStringLiteral(fileContent));

packages/builder/test/lib/lbt/bundle/Builder.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,51 @@
11
const test = require("ava");
2+
const sinon = require("sinon");
3+
const mock = require("mock-require");
24

35
const Builder = require("../../../../lib/lbt/bundle/Builder");
46
const ResourcePool = require("../../../../lib/lbt/resources/ResourcePool");
57

8+
test.afterEach.always((t) => {
9+
mock.stopAll();
10+
sinon.restore();
11+
});
12+
13+
test.serial("writePreloadModule: with invalid json content", async (t) => {
14+
const writeStub = sinon.stub();
15+
const logger = require("@ui5/logger");
16+
const verboseLogStub = sinon.stub();
17+
const myLoggerInstance = {
18+
verbose: verboseLogStub
19+
};
20+
sinon.stub(logger, "getLogger").returns(myLoggerInstance);
21+
const BuilderWithStub = mock.reRequire("../../../../lib/lbt/bundle/Builder");
22+
const invalidJsonContent = `{
23+
"a": 47,
24+
"b": {{include: asd}}
25+
}`;
26+
27+
const builder = new BuilderWithStub({});
28+
builder.optimize = true;
29+
builder.outW = {
30+
write: writeStub
31+
};
32+
const invalidJsonResource = {
33+
buffer: async () => {
34+
return invalidJsonContent;
35+
}
36+
};
37+
const result = await builder.writePreloadModule("invalid.json", undefined, invalidJsonResource);
38+
39+
40+
t.is(verboseLogStub.callCount, 2, "called 2 times");
41+
t.is(verboseLogStub.firstCall.args[0], "Failed to parse JSON file %s. Ignoring error, skipping compression.", "first verbose log argument 0 is correct");
42+
t.is(verboseLogStub.firstCall.args[1], "invalid.json", "first verbose log argument 1 is correct");
43+
t.deepEqual(verboseLogStub.secondCall.args[0], new SyntaxError("Unexpected token { in JSON at position 19"), "second verbose log");
44+
45+
t.true(result, "result is true");
46+
t.is(writeStub.callCount, 1, "Writer is called once");
47+
});
48+
649

750
test("integration: createBundle EVOBundleFormat (ui5loader.js)", async (t) => {
851
const pool = new ResourcePool();

0 commit comments

Comments
 (0)