Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7f8dbc1

Browse files
committed
[HttpKernel] Better exception page when the invokable controller returns nothing
1 parent91c5b14 commit7f8dbc1

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

‎src/Symfony/Component/HttpKernel/Exception/ControllerDoesNotReturnResponseException.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ private function parseControllerDefinition(callable $controller): ?array
6767
if (\is_object($controller)) {
6868
$r =new \ReflectionClass($controller);
6969

70+
try {
71+
$line =$r->getMethod('__invoke');
72+
}catch (\ReflectionException$e) {
73+
$line =$r->getEndLine();
74+
}
75+
7076
return [
7177
'file' =>$r->getFileName(),
72-
'line' =>$r->getEndLine(),
78+
'line' =>$line,
7379
];
7480
}
7581

‎src/Symfony/Component/HttpKernel/HttpKernel.php‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,17 @@ private function handleRaw(Request $request, int $type = self::MASTER_REQUEST)
154154
$event =newGetResponseForControllerResultEvent($this,$request,$type,$response);
155155
$this->dispatcher->dispatch(KernelEvents::VIEW,$event);
156156

157-
if ($event->hasResponse()) {
158-
$response =$event->getResponse();
159-
}else {
160-
$msg =sprintf('The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned %s.',$this->varToString($response));
161-
162-
// the user may have forgotten to return something
163-
if (null ===$response) {
164-
$msg .=' Did you forget to add a return statement somewhere in your controller?';
165-
}
166-
167-
thrownewControllerDoesNotReturnResponseException($msg,$controller,__FILE__,__LINE__ -17);
157+
if (!$event->hasResponse()) {
158+
$message =sprintf(
159+
'The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned %s.%s',
160+
$this->varToString($response),
161+
' Did you forget to add a return statement somewhere in your controller?'
162+
);
163+
164+
thrownewControllerDoesNotReturnResponseException($message,$controller,__FILE__,__LINE__ -14);
168165
}
166+
167+
$response =$event->getResponse();
169168
}
170169

171170
return$this->filterResponse($response,$request,$type);

‎src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,19 @@ public function testHandleWhenTheControllerIsAStaticArray()
215215
publicfunctiontestHandleWhenTheControllerDoesNotReturnAResponse()
216216
{
217217
$dispatcher =newEventDispatcher();
218-
$kernel =$this->getHttpKernel($dispatcher,function () {return'foo';});
218+
$kernel =$this->getHttpKernel($dispatcher,function () {});
219219

220220
try {
221221
$kernel->handle(newRequest());
222222

223223
$this->fail('The kernel should throw an exception.');
224224
}catch (ControllerDoesNotReturnResponseException$e) {
225-
}
225+
$first =$e->getTrace()[0];
226226

227-
$first =$e->getTrace()[0];
228-
229-
// `file` index the array starting at 0, and __FILE__ starts at 1
230-
$line =file($first['file'])[$first['line'] -2];
231-
$this->assertContains('// call controller',$line);
227+
// `file` index the array starting at 0, and __FILE__ starts at 1
228+
$line =file($first['file'])[$first['line'] -2];
229+
$this->assertContains('// call controller',$line);
230+
}
232231
}
233232

234233
publicfunctiontestHandleWhenTheControllerDoesNotReturnAResponseButAViewIsRegistered()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp