Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
fix(Compiler): Better errors for @GenerateInjector.
Browse files Browse the repository at this point in the history
Closes #1571

Having a test here is prohibitively expensive (we don't currently have a pattern for compiler tests that import `*.template.dart), and we also didn't have a test for the other NPE case.

PiperOrigin-RevId: 208711303
  • Loading branch information
matanlurey committed Aug 14, 2018
1 parent cb2acfc commit c7e2e8f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
5 changes: 5 additions & 0 deletions angular/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@
caused the directive to no longer be functional
(`*ngFor="let x; let i = $index "`, for example).
* [#1570][]: When a provider's `token` for `@GeneratedInjector(...)` is read
as `null` (either intentionally, or due to analysis errors/imports missing)
a better error message is now thrown with the context of the error.
[#880]: https://github.com/dart-lang/angular/issues/880
[#1538]: https://github.com/dart-lang/angular/issues/1538
[#1539]: https://github.com/dart-lang/angular/issues/1539
[#1540]: https://github.com/dart-lang/angular/issues/1540
[#1558]: https://github.com/dart-lang/angular/issues/1558
[#1570]: https://github.com/dart-lang/angular/issues/1570
### Other improvements
Expand Down
2 changes: 2 additions & 0 deletions angular_compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Catches an (invalid) `null` token of a provider and throws a better error.

## 0.4.0

### New Features
Expand Down
25 changes: 19 additions & 6 deletions angular_compiler/lib/src/analyzer/di/injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,31 @@ class InjectorReader {
$GenerateInjector.firstAnnotationOfExact(field),
);

@alwaysThrows
void _throwParseError([DartObject context]) {
BuildError.throwForElement(
field,
context == null
? 'Unable to parse @GenerateInjector. You may have analysis errors'
: 'Unable to parse @GenerateInjector. A provider\'s token ($context) '
'was read as "null". This is either invalid configuration or you '
'have analysis errors',
);
}

/// Providers that are part of the provided list of the annotation.
Iterable<ProviderElement> get providers {
if (_providers == null) {
final providersOrModules = annotation.read('_providersOrModules');
if (providersOrModules.isNull) {
BuildError.throwForElement(
field,
'Unable to parse @GenerateInjector. You may have analysis errors',
);
_throwParseError();
}
try {
final module = moduleReader.parseModule(providersOrModules.objectValue);
_providers = moduleReader.deduplicateProviders(module.flatten());
} on NullTokenException catch (e) {
_throwParseError(e.constant);
}
final module = moduleReader.parseModule(providersOrModules.objectValue);
_providers = moduleReader.deduplicateProviders(module.flatten());
}
return _providers;
}
Expand Down
16 changes: 13 additions & 3 deletions angular_compiler/lib/src/analyzer/di/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ class ProviderReader {

ProviderElement _parseProvider(DartObject o) {
final reader = ConstantReader(o);
final token = _tokenReader.parseTokenObject(
reader.read('token').objectValue,
);
final value = reader.read('token');
if (value.isNull) {
throw new NullTokenException(o);
}
final token = _tokenReader.parseTokenObject(value.objectValue);
final useClass = reader.read('useClass');
if (!useClass.isNull) {
return _parseUseClass(token, o, useClass.typeValue.element);
Expand Down Expand Up @@ -328,3 +330,11 @@ class UseValueProviderElement extends ProviderElement {
bool multi = false,
}) : super._(e, providerType, multi);
}

/// Thrown when a value of `null` is read for a provider token.
class NullTokenException implements Exception {
/// Constant whose `.token` property was resolved to `null`.
final DartObject constant;

const NullTokenException(this.constant);
}

0 comments on commit c7e2e8f

Please sign in to comment.