Skip to content

Commit

Permalink
feat(core): include error codes in RulesetValidationError
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Oct 14, 2022
1 parent 1724aba commit c01c6b5
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 122 deletions.
27 changes: 22 additions & 5 deletions packages/core/src/ruleset/__tests__/ruleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ describe('Ruleset', () => {
describe('error handling', () => {
it('given empty ruleset, should throw a user friendly error', () => {
expect(() => new Ruleset({})).toThrowError(
new RulesetValidationError('Ruleset must have rules or extends or overrides defined', []),
new RulesetValidationError(
'invalid-ruleset-definition',
'Ruleset must have rules or extends or overrides defined',
[],
),
);
});
});
Expand Down Expand Up @@ -1271,7 +1275,11 @@ describe('Ruleset', () => {
}),
).toThrowAggregateError(
new AggregateError([
new RulesetValidationError('Alias "PathItem-" does not exist', ['rules', 'valid-path', 'given']),
new RulesetValidationError('undefined-alias', 'Alias "PathItem-" does not exist', [
'rules',
'valid-path',
'given',
]),
]),
);
});
Expand All @@ -1298,6 +1306,7 @@ describe('Ruleset', () => {
).toThrowAggregateError(
new AggregateError([
new RulesetValidationError(
'generic-validation-error',
'Alias "Test" is circular. Resolution stack: Test -> Contact -> Info -> Root -> Info',
['rules', 'valid-path', 'given'],
),
Expand Down Expand Up @@ -1335,9 +1344,17 @@ describe('Ruleset', () => {
}),
).toThrowAggregateError(
new AggregateError([
new RulesetValidationError('Alias "PathItem" does not exist', ['rules', 'valid-path', 'given']),
new RulesetValidationError('Alias "Name" does not exist', ['rules', 'valid-name-and-description', 'given']),
new RulesetValidationError(`Alias "Description" does not exist`, [
new RulesetValidationError('undefined-alias', 'Alias "PathItem" does not exist', [
'rules',
'valid-path',
'given',
]),
new RulesetValidationError('undefined-alias', 'Alias "Name" does not exist', [
'rules',
'valid-name-and-description',
'given',
]),
new RulesetValidationError('undefined-alias', `Alias "Description" does not exist`, [
'rules',
'valid-name-and-description',
'given',
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/ruleset/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ function _resolveAlias(
const alias = ALIAS.exec(expression)?.[1];

if (alias === void 0 || alias === null) {
throw new ReferenceError(`Alias must match /^#([A-Za-z0-9_-]+)/`);
throw new TypeError(`Alias must match /^#([A-Za-z0-9_-]+)/`);
}

if (stack.has(alias)) {
const _stack = [...stack, alias];
throw new ReferenceError(`Alias "${_stack[0]}" is circular. Resolution stack: ${_stack.join(' -> ')}`);
throw new Error(`Alias "${_stack[0]}" is circular. Resolution stack: ${_stack.join(' -> ')}`);
}

stack.add(alias);
Expand Down
18 changes: 15 additions & 3 deletions packages/core/src/ruleset/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ addFormats(ajv);

export class RulesetFunctionValidationError extends RulesetValidationError {
constructor(fn: string, error: ErrorObject) {
super(RulesetFunctionValidationError.printMessage(fn, error), error.instancePath.slice(1).split('/'));
super(
'invalid-function-options',
RulesetFunctionValidationError.printMessage(fn, error),
error.instancePath.slice(1).split('/'),
);
}

private static printMessage(fn: string, error: ErrorObject): string {
Expand Down Expand Up @@ -150,7 +154,11 @@ export function createRulesetFunction<I, O>(
}

if (options === null) {
throw new RulesetValidationError(`"${fn.name || '<unknown>'}" function does not accept any options`, []);
throw new RulesetValidationError(
'invalid-function-options',
`"${fn.name || '<unknown>'}" function does not accept any options`,
[],
);
} else if (
'errors' in validateOptions &&
Array.isArray(validateOptions.errors) &&
Expand All @@ -160,7 +168,11 @@ export function createRulesetFunction<I, O>(
validateOptions.errors.map(error => new RulesetFunctionValidationError(fn.name || '<unknown>', error)),
);
} else {
throw new RulesetValidationError(`"functionOptions" of "${fn.name || '<unknown>'}" function must be valid`, []);
throw new RulesetValidationError(
'invalid-function-options',
`"functionOptions" of "${fn.name || '<unknown>'}" function must be valid`,
[],
);
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/ruleset/meta/ruleset.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
},
"required": ["files"],
"errorMessage": {
"type": "must be a override, i.e. { \"files\": [\"v2/**/*.json\"], \"rules\": {} }"
"type": "must be an override, i.e. { \"files\": [\"v2/**/*.json\"], \"rules\": {} }"
}
},
{
Expand Down
Loading

0 comments on commit c01c6b5

Please sign in to comment.