Skip to content

Commit 0c930ec

Browse files
[DI] fix casting AutowiringFailedException to string when its callback throws
1 parent d706f59 commit 0c930ec

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Compiler/AutowirePass.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,16 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
226226
if ($parameter->isDefaultValueAvailable()) {
227227
$value = $parameter->getDefaultValue();
228228
} elseif (!$parameter->allowsNull()) {
229-
throw new AutowiringFailedException($this->currentId, $failureMessage);
229+
if (\function_exists('xdebug_disable')) {
230+
xdebug_disable();
231+
}
232+
try {
233+
throw new AutowiringFailedException($this->currentId, $failureMessage);
234+
} finally {
235+
if (\function_exists('xdebug_enable')) {
236+
xdebug_enable();
237+
}
238+
}
230239
}
231240
}
232241

Compiler/PassConfig.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,15 @@ public function __construct()
8484
new RemoveUnusedDefinitionsPass(),
8585
new InlineServiceDefinitionsPass(new AnalyzeServiceReferencesPass()),
8686
new AnalyzeServiceReferencesPass(),
87-
new DefinitionErrorExceptionPass(),
8887
new CheckExceptionOnInvalidReferenceBehaviorPass(),
8988
new ResolveHotPathPass(),
9089
]];
90+
91+
$this->afterRemovingPasses = [
92+
100 => [
93+
new DefinitionErrorExceptionPass(),
94+
],
95+
];
9196
}
9297

9398
/**

Exception/AutowiringFailedException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ public function __toString(): string
4747
$messageCallback = $this->messageCallback;
4848
$this->messageCallback = null;
4949

50-
return $this->message = $messageCallback();
50+
try {
51+
return $this->message = $messageCallback();
52+
} catch (\Throwable $e) {
53+
return $this->message = $e->getMessage();
54+
}
5155
}
5256
};
5357
}

0 commit comments

Comments
 (0)