Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not throw but provide errors #1002

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading