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

[HttpKernel] Better exception page when the controller returns nothing#27138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@lyrixx
Copy link
Member

@lyrixxlyrixx commentedMay 3, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#27137
LicenseMIT
Doc PR

Before:

before

after:

after

pyrech, webignition, pierrefeelunique, yceruto, ogizanagi, linaori, jenkoian, PaperCoder, onEXHovia, andreybolonin, and 2 more reacted with thumbs up emoji
throw$e;
}

$r =new \ReflectionProperty(\Exception::class,'trace');
Copy link
Member

@nicolas-grekasnicolas-grekasMay 3, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

What about using a rebound closure instead of reflection? It'd make things easier to read IMHO.

lyrixx reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Warning: Cannot bind closure to scope of internal class Exception

$r->setAccessible(true);
$r->setValue($e,array_merge(array(
array(
'line' =>$e->getLine(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This could be even more accurate, passing the line of the call_user_fun above (using__LINE__ next to it.)
Aren't they other keys we should put in the frame? How does it look when thrown from inside the controller?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

fixed

}

if (is_object($controller)) {
$r =new \ReflectionClass($controller);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

method__invoke()?

Copy link
MemberAuthor

@lyrixxlyrixxMay 3, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

yes

BTW, I adapted the code from

protectedfunctionparseController($controller)
{
if (is_string($controller) &&false !==strpos($controller,'::')) {
$controller =explode('::',$controller);
}
if (is_array($controller)) {
try {
$r =new \ReflectionMethod($controller[0],$controller[1]);
returnarray(
'class' =>is_object($controller[0]) ?get_class($controller[0]) :$controller[0],
'method' =>$controller[1],
'file' =>$r->getFileName(),
'line' =>$r->getStartLine(),
);
}catch (\ReflectionException$e) {
if (is_callable($controller)) {
// using __call or __callStatic
returnarray(
'class' =>is_object($controller[0]) ?get_class($controller[0]) :$controller[0],
'method' =>$controller[1],
'file' =>'n/a',
'line' =>'n/a',
);
}
}
}
if ($controllerinstanceof \Closure) {
$r =new \ReflectionFunction($controller);
returnarray(
'class' =>$r->getName(),
'method' =>null,
'file' =>$r->getFileName(),
'line' =>$r->getStartLine(),
);
}
if (is_object($controller)) {
$r =new \ReflectionClass($controller);
returnarray(
'class' =>$r->getName(),
'method' =>null,
'file' =>$r->getFileName(),
'line' =>$r->getStartLine(),
);
}
returnis_string($controller) ?$controller :'n/a';
}

@lyrixxlyrixxforce-pushed thehttp-kernel-exception-controller branch from31a63b9 to93ecc0bCompareMay 3, 2018 15:05

// call controller
$response =\call_user_func_array($controller,$arguments);
$response =\call_user_func_array($controller,$arguments);$line =__LINE__;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I would put this on the next line using$line = __LINE__ - 1; instead. The current code does not comply with our coding standards, and would force to exclude the whole HttpKernel.php file from the CS tooling to avoid having PHP-CS-Fixer breaking it on each run.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

done

'line' =>$r->getStartLine(),
);
}catch (\ReflectionException$e) {
returnnull;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

trynew \ReflectionClass($controller[0])?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

To get only the file?

Copy link
Member

@nicolas-grekasnicolas-grekasMay 3, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes, WDYT ? And start line of the class of course.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why not, But I don't know how to "simulate" this fail :/ Do you know how to?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Looking at RequestDataCollector I'd say using__call?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I don't understand. Sorry.
If the method does not exist in the first place Symfony throw an error.
I don't want to add dead code to Symfony, so please give me a reproducer ;)

@nicolas-grekas
Copy link
Member

WDYT of moving all the added logic to a new exception class, child of LogicException?

lyrixx and andreybolonin reacted with thumbs up emoji

@nicolas-grekasnicolas-grekas added this to thenext milestoneMay 3, 2018
@lyrixxlyrixxforce-pushed thehttp-kernel-exception-controller branch 2 times, most recently from8c5d7b2 toc8f75e5CompareMay 4, 2018 12:15
@lyrixx
Copy link
MemberAuthor

lyrixx commentedMay 4, 2018
edited
Loading

@nicolas-grekas I added the new Exception class.

Note: Exception::trace is private, So I need reflection. line and file are protected ;).
andget* methods are final

}
thrownew \LogicException($msg);

thrownewControllerDoesNotReturnResponseException($msg,$controller,__FILE__,__LINE__ -17);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

we need a test case to ensure we won't miss updating this "17" in the future I suppose

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Tests added

@lyrixxlyrixxforce-pushed thehttp-kernel-exception-controller branch fromc8f75e5 tof1e9aa4CompareMay 7, 2018 08:40
@fabpot
Copy link
Member

Thank you@lyrixx.

@fabpotfabpot merged commitf1e9aa4 intosymfony:masterMay 9, 2018
fabpot added a commit that referenced this pull requestMay 9, 2018
… returns nothing (lyrixx)This PR was merged into the 4.2-dev branch.Discussion----------[HttpKernel] Better exception page when the controller returns nothing| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#27137| License       | MIT| Doc PR        |---Before:![before](https://user-images.githubusercontent.com/408368/39580501-9c8d5e0a-4ee9-11e8-84a9-aad8566da4b6.png)after:![after](https://user-images.githubusercontent.com/408368/39580504-a23e3b76-4ee9-11e8-9a89-01e519b94a20.png)Commits-------f1e9aa4 [HttpKernel] Better exception page when the controller returns nothing
@lyrixxlyrixx deleted the http-kernel-exception-controller branchMay 9, 2018 08:31
@nicolas-grekasnicolas-grekas removed this from thenext milestoneNov 1, 2018
@nicolas-grekasnicolas-grekas added this to the4.1 milestoneNov 1, 2018
This was referencedNov 3, 2018
fabpot added a commit that referenced this pull requestMar 5, 2019
…controller returns nothing (dimabory)This PR was merged into the 4.3-dev branch.Discussion----------[HttpKernel] Better exception page when the invokable controller returns nothing| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#27138| License       | MIT| Doc PR        |---__Prerequisites___Configure invokable controller_```php# config/routes.yamlindex:    path: /    controller: App\Controller\Start```__Before:__![before](https://user-images.githubusercontent.com/11414342/53577698-ca739000-3b7e-11e9-98ac-8c8e27626fbe.png)__After:__![after](https://user-images.githubusercontent.com/11414342/53577733-df502380-3b7e-11e9-8377-a4a97ea73df8.png)---Take a look for an enhancement/refactoring in `HttpKernel.php`Commits-------f6c1622 [HttpKernel] Better exception page when the invokable controller returns nothing
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@stofstofstof requested changes

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

4.1

Development

Successfully merging this pull request may close these issues.

5 participants

@lyrixx@nicolas-grekas@fabpot@stof@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp