From 46b2d39d41ec3851159b24f4a4e78cf5b56d6241 Mon Sep 17 00:00:00 2001 From: Dominique Pfister Date: Thu, 26 Sep 2024 15:43:57 +0200 Subject: [PATCH] fix: do not throw but provide errors --- package-lock.json | 55 ++++++++++++++++++- .../helix-shared-config/src/IndexConfig.js | 14 ++--- .../test/IndexConfig.test.js | 18 ++++-- .../test/specs/queryconfigs/broken.json | 17 ++++++ 4 files changed, 88 insertions(+), 16 deletions(-) create mode 100644 packages/helix-shared-config/test/specs/queryconfigs/broken.json diff --git a/package-lock.json b/package-lock.json index 3a93a07e..7324efac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15386,7 +15386,7 @@ }, "packages/helix-shared-config": { "name": "@adobe/helix-shared-config", - "version": "10.6.8", + "version": "11.0.0", "license": "Apache-2.0", "dependencies": { "@adobe/fetch": "^4.0.1", @@ -15631,6 +15631,55 @@ "c8": "10.1.2" } }, + "packages/helix-shared-indexer/node_modules/@adobe/helix-shared-config": { + "version": "10.6.9", + "resolved": "https://registry.npmjs.org/@adobe/helix-shared-config/-/helix-shared-config-10.6.9.tgz", + "integrity": "sha512-IDsDyweqyxp+jCA3MBSMBjKLzzH+qTtjIU+vX9RNpYi9Gj1h5U0ViNhxs0cL5ZqZZ8B8rNbcABkNPHtUpergeA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "@adobe/fetch": "^4.0.1", + "@adobe/helix-shared-git": "^3.0.0", + "@adobe/helix-shared-prune": "^1.0.5", + "@adobe/helix-shared-utils": "^2.1.0", + "ajv": "8.17.1", + "ajv-formats": "3.0.1", + "cookie": "0.6.0", + "fs-extra": "11.2.0", + "ignore": "6.0.2", + "lru-cache": "11.0.1", + "object-hash": "3.0.0", + "uri-js": "4.4.1", + "yaml": "2.5.1" + } + }, + "packages/helix-shared-indexer/node_modules/@adobe/helix-shared-prune": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@adobe/helix-shared-prune/-/helix-shared-prune-1.0.5.tgz", + "integrity": "sha512-xvNIA5qxRSYnNAtzikIEbn8SEqnmAdr2NbsonQqTimor5Ie8jstLuIo4wXHzeK2l+Vy9zVvimzGokEkgBslgIg==", + "dev": true, + "license": "Apache-2.0" + }, + "packages/helix-shared-indexer/node_modules/@adobe/helix-shared-utils": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@adobe/helix-shared-utils/-/helix-shared-utils-2.2.0.tgz", + "integrity": "sha512-Xu0cRb62Ml+SIQxm573WtI2PhkwhCdTwv4r67jxJJYbuFZATX1zNRq31HB1QMtMBKpK1hB7hGpwpkZNaH+n1sw==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">= 14.18" + } + }, + "packages/helix-shared-indexer/node_modules/ignore": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/ignore/-/ignore-6.0.2.tgz", + "integrity": "sha512-InwqeHHN2XpumIkMvpl/DCJVrAHgCsG5+cn1XlnLWGwtZBm8QJfSusItfrwx81CTp5agNZqpKU2J/ccC5nGT4A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 4" + } + }, "packages/helix-shared-process-queue": { "name": "@adobe/helix-shared-process-queue", "version": "3.0.4", @@ -15697,7 +15746,7 @@ }, "packages/helix-shared-storage": { "name": "@adobe/helix-shared-storage", - "version": "1.0.6", + "version": "1.0.7", "license": "Apache-2.0", "dependencies": { "@adobe/fetch": "^4.1.2", @@ -15718,7 +15767,7 @@ }, "packages/helix-shared-tokencache": { "name": "@adobe/helix-shared-tokencache", - "version": "1.4.32", + "version": "1.4.33", "license": "Apache-2.0", "dependencies": { "@adobe/fetch": "^4.0.10", diff --git a/packages/helix-shared-config/src/IndexConfig.js b/packages/helix-shared-config/src/IndexConfig.js index b8b6c604..682763d5 100644 --- a/packages/helix-shared-config/src/IndexConfig.js +++ b/packages/helix-shared-config/src/IndexConfig.js @@ -195,15 +195,11 @@ export class IndexConfig extends SchemaDerivedConfig { } /** - * Validates the loaded configuration and coerces types and sets defaulst + * Return errors encountered in parsing. + * + * @returns {String[]} parsing errors */ - async validate() { - await super.validate(); - - if (this._document?.errors?.length) { - const detail = this._document.errors.map(({ message }) => (message)).join('\n'); - throw new Error(`Invalid index configuration: - ${detail}`); - } + getErrors() { + return this._document?.errors ?? []; } } diff --git a/packages/helix-shared-config/test/IndexConfig.test.js b/packages/helix-shared-config/test/IndexConfig.test.js index bd695496..674848b8 100644 --- a/packages/helix-shared-config/test/IndexConfig.test.js +++ b/packages/helix-shared-config/test/IndexConfig.test.js @@ -72,18 +72,28 @@ describe('Index Config Loading', () => { assert.deepEqual(actual, expected); }); - it('Trips over broken config', async () => { + it('Does not trip over broken config', async () => { const cfg = new IndexConfig() .withConfigPath(path.resolve(SPEC_ROOT, 'broken.yaml')); - await assert.rejects(async () => cfg.init(), /Implicit map keys need to be followed by map value/); + await cfg.init(); + + assert.strictEqual(cfg.getQuery('foo', 'bar'), undefined); + + const actual = cfg.toJSON(); + const expected = JSON.parse(await fs.readFile(path.resolve(SPEC_ROOT, 'broken.json'), 'utf-8')); + + assert.deepEqual(actual, expected); }); - it('Trips over config with duplicate property', async () => { + it('Parsed config contains errors', async () => { const cfg = new IndexConfig() .withConfigPath(path.resolve(SPEC_ROOT, 'duplicate.yaml')); - await assert.rejects(async () => cfg.init(), /Map keys must be unique at line 10, column 7/); + await cfg.init(); + + const details = cfg.getErrors().map(({ message }) => (message)).join('\n'); + assert.match(details, /Map keys must be unique at line 10, column 7/); }); it('Does not trip over non-existing config', async () => { diff --git a/packages/helix-shared-config/test/specs/queryconfigs/broken.json b/packages/helix-shared-config/test/specs/queryconfigs/broken.json new file mode 100644 index 00000000..f8c662f7 --- /dev/null +++ b/packages/helix-shared-config/test/specs/queryconfigs/broken.json @@ -0,0 +1,17 @@ +{ + "indices": { + "blog-posts": { + "fetch": "https://${repo}-${owner}.project-helix.page/${path}", + "properties": { + "author": { + "faceted": true, + "select": "main > div:nth-of-type(3) > p:nth-of-type(1)", + "value": "${match('by (.*)')}\n" + } + }, + "source": "html" + } + }, + "version": "1", + "​": null +} \ No newline at end of file