Skip to content

Commit

Permalink
fix: do not throw but provide errors (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominique-pfister authored Sep 26, 2024
1 parent 36bb441 commit 3cdd7ca
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 16 deletions.
55 changes: 52 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions packages/helix-shared-config/src/IndexConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [];
}
}
18 changes: 14 additions & 4 deletions packages/helix-shared-config/test/IndexConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
17 changes: 17 additions & 0 deletions packages/helix-shared-config/test/specs/queryconfigs/broken.json
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 3cdd7ca

Please sign in to comment.