diff --git a/packages/service_container/lib/src/container.dart b/packages/service_container/lib/src/container.dart index 0ec0c79..2f3da4d 100644 --- a/packages/service_container/lib/src/container.dart +++ b/packages/service_container/lib/src/container.dart @@ -249,9 +249,15 @@ class Container implements ContainerContract, Map { dynamic resolve(String abstract, [List? parameters]) { abstract = _getAlias(abstract); + // if (_buildStack.any((stack) => stack.contains(abstract))) { + // // Instead of throwing an exception, return the abstract itself + // return abstract; + // } + if (_buildStack.any((stack) => stack.contains(abstract))) { - // Instead of throwing an exception, return the abstract itself - return abstract; + // Create a path that includes the current abstract and the existing build stack + List path = [..._buildStack.last, abstract]; + throw CircularDependencyException(path); } _buildStack.add([abstract]); @@ -451,10 +457,24 @@ class Container implements ContainerContract, Map { }); } + // @override + // T make(String abstract, [List? parameters]) { + // var result = resolve(abstract, parameters); + // return _applyExtenders(abstract, result, this) as T; + // } + @override T make(String abstract, [List? parameters]) { - var result = resolve(abstract, parameters); - return _applyExtenders(abstract, result, this) as T; + try { + var result = resolve(abstract, parameters); + return _applyExtenders(abstract, result, this) as T; + } catch (e) { + if (e is CircularDependencyException) { + rethrow; + } + throw BindingResolutionException( + 'Error resolving $abstract: ${e.toString()}'); + } } dynamic _applyExtenders(String abstract, dynamic result, Container c) {