refactor(ivy): use FatalDiagnosticError to throw more descriptive errors while extracting queries information (#31123)

Prior to this commit, the logic to extract query information from class fields used an instance of regular Error class to throw an error. As a result, some useful information (like reference to a specific field) was missing. Replacing Error class with FatalDiagnosticError one makes the error more verbose that should simplify debugging.

PR Close #31123
This commit is contained in:
Andrew Kushnir
2019-06-18 17:23:51 -07:00
committed by Kara Erickson
parent b11a2057c6
commit 2aba485118
4 changed files with 80 additions and 43 deletions

View File

@ -435,16 +435,24 @@ export function queriesFromFields(
fields: {member: ClassMember, decorators: Decorator[]}[], reflector: ReflectionHost,
evaluator: PartialEvaluator): R3QueryMetadata[] {
return fields.map(({member, decorators}) => {
const decorator = decorators[0];
const node = member.node || decorator.node;
// Throw in case of `@Input() @ContentChild('foo') foo: any`, which is not supported in Ivy
if (member.decorators !.some(v => v.name === 'Input')) {
throw new Error(`Cannot combine @Input decorators with query decorators`);
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_COLLISION, node,
'Cannot combine @Input decorators with query decorators');
}
if (decorators.length !== 1) {
throw new Error(`Cannot have multiple query decorators on the same class member`);
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_COLLISION, node,
'Cannot have multiple query decorators on the same class member');
} else if (!isPropertyTypeMember(member)) {
throw new Error(`Query decorator must go on a property-type member`);
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_UNEXPECTED, node,
'Query decorator must go on a property-type member');
}
const decorator = decorators[0];
return extractQueryMetadata(
decorator.node, decorator.name, decorator.args || [], member.name, reflector, evaluator);
});

View File

@ -14,7 +14,7 @@ export enum ErrorCode {
DECORATOR_UNEXPECTED = 1005,
/**
* This error code indicates that there are incompatible decorators on a type.
* This error code indicates that there are incompatible decorators on a type or a class field.
*/
DECORATOR_COLLISION = 1006,