Skip to content

Commit 80132fc

Browse files
committed
error handling for pushNewScope
1 parent 06c9a67 commit 80132fc

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

lib/get_it_impl.dart

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,12 +1352,23 @@ class _GetItImplementation implements GetIt {
13521352
);
13531353
_pushScopeInProgress = true;
13541354
_scopes.add(_Scope(name: scopeName, disposeFunc: dispose));
1355-
init?.call(this);
1356-
if (isFinal) {
1357-
_scopes.last.isFinal = true;
1355+
try {
1356+
init?.call(this);
1357+
if (isFinal) {
1358+
_scopes.last.isFinal = true;
1359+
}
1360+
onScopeChanged?.call(true);
1361+
} catch (e) {
1362+
final failedScope = _scopes.last;
1363+
1364+
/// prevend any new registrations in this scope
1365+
failedScope.isFinal = true;
1366+
failedScope.reset(dispose: true);
1367+
_scopes.removeLast();
1368+
rethrow;
1369+
} finally {
1370+
_pushScopeInProgress = false;
13581371
}
1359-
onScopeChanged?.call(true);
1360-
_pushScopeInProgress = false;
13611372
}
13621373

13631374
bool _pushScopeInProgress = false;
@@ -1395,12 +1406,24 @@ class _GetItImplementation implements GetIt {
13951406
);
13961407
_pushScopeInProgress = true;
13971408
_scopes.add(_Scope(name: scopeName, disposeFunc: dispose));
1398-
await init?.call(this);
1399-
if (isFinal) {
1400-
_scopes.last.isFinal = true;
1409+
try {
1410+
await init?.call(this);
1411+
1412+
if (isFinal) {
1413+
_scopes.last.isFinal = true;
1414+
}
1415+
onScopeChanged?.call(true);
1416+
} catch (e) {
1417+
final failedScope = _scopes.last;
1418+
1419+
/// prevend any new registrations in this scope
1420+
failedScope.isFinal = true;
1421+
await failedScope.reset(dispose: true);
1422+
_scopes.removeLast();
1423+
rethrow;
1424+
} finally {
1425+
_pushScopeInProgress = false;
14011426
}
1402-
onScopeChanged?.call(true);
1403-
_pushScopeInProgress = false;
14041427
}
14051428

14061429
/// Disposes all factories/Singletons that have been registered in this scope
@@ -1756,9 +1779,6 @@ class _GetItImplementation implements GetIt {
17561779
outerFutureGroup.close();
17571780
});
17581781

1759-
/// outerFutureGroup.future returns a Future<List> and not a Future<T>
1760-
/// As we know that the actual factory function was added last to the FutureGroup
1761-
/// we just use that one
17621782
serviceFactory.pendingResult =
17631783
outerFutureGroup.future.then((completedFutures) {
17641784
return serviceFactory.instance!;

0 commit comments

Comments
 (0)