Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(directive injector): DiCircularDependencyError -> _CircularDepen…
Browse files Browse the repository at this point in the history
…dencyError

And make _CircularDependencyError extends CircularDependencyError
so that catching CircularDependencyError catches any circular dep
error
  • Loading branch information
vicb committed Aug 28, 2014
1 parent 0b0080b commit 3ffe014
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
28 changes: 17 additions & 11 deletions lib/core_dom/directive_injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ final SOURCE_LIGHT_DOM_KEY = new Key(SourceLightDom);
final TEMPLATE_LOADER_KEY = new Key(TemplateLoader);
final SHADOW_ROOT_KEY = new Key(ShadowRoot);

final int NO_CONSTRUCTION = 0;
const int NO_CONSTRUCTION = 0;

// Maximum parent directive injectors that would be traversed.
final int MAX_TREE_DEPTH = 1 << 30;
const int MAX_TREE_DEPTH = 1 << 30;

// Maximum number of concurrent constructions a directive injector would
// support before throwing an error.
final int MAX_CONSTRUCTION_DEPTH = 50;
const int MAX_CONSTRUCTION_DEPTH = 50;

const int VISIBILITY_LOCAL = -1;
const int VISIBILITY_DIRECT_CHILD = -2;
Expand Down Expand Up @@ -114,7 +114,7 @@ class DirectiveInjector implements DirectiveBinder {
, COMPONENT_DIRECTIVE_INJECTOR_KEY
, KEEP_ME_LAST
];

final DirectiveInjector _parent;
final Injector _appInjector;
final Node _node;
Expand Down Expand Up @@ -323,8 +323,8 @@ class DirectiveInjector implements DirectiveBinder {

dynamic _new(Key k, List<Key> paramKeys, Function fn) {
if (_constructionDepth > MAX_CONSTRUCTION_DEPTH) {
_constructionDepth = NO_CONSTRUCTION;
throw new DiCircularDependencyError(key);
_constructionDepth = 0;
throw new _CircularDependencyError(k);
}
bool isFirstConstruction = (_constructionDepth++ == NO_CONSTRUCTION);
var oldTag = _TAG_GET.makeCurrent();
Expand Down Expand Up @@ -433,7 +433,7 @@ class TemplateDirectiveInjector extends DirectiveInjector {
if (_destLightDom != null) _destLightDom.addViewPort(viewPort);
return viewPort;
}

}

class ComponentDirectiveInjector extends DirectiveInjector {
Expand Down Expand Up @@ -478,8 +478,8 @@ class ComponentDirectiveInjector extends DirectiveInjector {

// For efficiency we run through the maximum resolving depth and unwind
// instead of setting 'resolving' key per type.
class DiCircularDependencyError extends ResolvingError {
DiCircularDependencyError(key) : super(key);
class _CircularDependencyError extends CircularDependencyError {
_CircularDependencyError(key) : super(key);

// strips the cyclical part of the chain.
List<Key> get stripCycle {
Expand All @@ -494,6 +494,12 @@ class DiCircularDependencyError extends ResolvingError {
return rkeys;
}

String get resolveChain => stripCycle.join(' -> ');
String toString() => "circular dependency (${resolveChain})";
String get resolveChain {
StringBuffer buffer = new StringBuffer()
..write("(resolving ")
..write(stripCycle.join(' -> '))
..write(")");
return buffer.toString();
}

}
12 changes: 8 additions & 4 deletions test/core_dom/directive_injector_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ void main() {
addDirective(_TypeC0);
addDirective(_TypeC1, Visibility.CHILDREN);
addDirective(_TypeC2, Visibility.CHILDREN);
expect(() => injector.get(_TypeC0)).toThrow(
'circular dependency (_TypeC0 -> _TypeC1 -> _TypeC2 -> _TypeC1)');
expect(() => injector.get(_TypeC0)).toThrowWith(
where: (e) => e is CircularDependencyError,
message: 'Cannot resolve a circular dependency! '
'(resolving _TypeC0 -> _TypeC1 -> _TypeC2 -> _TypeC1)');
});

it('should throw circular dependency error accross injectors', () {
Expand All @@ -124,8 +126,10 @@ void main() {
addDirective(_TypeC0, Visibility.LOCAL, childInjector);
addDirective(_TypeC1, Visibility.CHILDREN);
addDirective(_TypeC2, Visibility.CHILDREN);
expect(() => childInjector.get(_TypeC0)).toThrow(
'circular dependency (_TypeC0 -> _TypeC1 -> _TypeC2 -> _TypeC1)');
expect(() => childInjector.get(_TypeC0)).toThrowWith(
where: (e) => e is CircularDependencyError,
message: 'Cannot resolve a circular dependency! '
'(resolving _TypeC0 -> _TypeC1 -> _TypeC2 -> _TypeC1)');
});
});

Expand Down

0 comments on commit 3ffe014

Please sign in to comment.