Skip to content

[HttpKernel] Create responses for unhandled HttpExceptionInterface exceptions #26928

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #25844
License MIT
Doc PR -

Alternative to #26138

if ($event->hasResponse()) {
$response = $event->getResponse();
} elseif ($e instanceof HttpExceptionInterface) {
$response = new Response($e->getMessage());
Copy link
Contributor

@dmaicher dmaicher Apr 14, 2018

Choose a reason for hiding this comment

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

Wondering... couldn't $e->getMessage() potentially expose something that is not meant to be displayed to a user/client ever?

Maybe better use Response::$statusTexts[$code]?

Copy link
Member Author

@nicolas-grekas nicolas-grekas Apr 14, 2018

Choose a reason for hiding this comment

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

When twig is installed, this is the message that is displayed (isn't it?), so I don't think there is anything harmful here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

correct, fixed

@junowilderness
Copy link
Contributor

@nicolas-grekas This is more elegant than #26138.

$response = $kernel->handle(new Request());

$this->assertSame(404, $response->getStatusCode());
$this->assertSame('404 Not Found', $response->getContent());
Copy link
Member

Choose a reason for hiding this comment

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

As demonstrated by this test change, this changes the behavior.

Copy link
Member

Choose a reason for hiding this comment

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

So, I would not do the change in 3.4.

@nicolas-grekas
Copy link
Member Author

Rebased on master, ready for 4.1

@nicolas-grekas nicolas-grekas changed the base branch from 3.4 to master April 20, 2018 09:47
@sroze sroze modified the milestones: 3.4, 4.1 Apr 20, 2018
@fabpot
Copy link
Member

fabpot commented Apr 20, 2018

Can you add a note in the CHANGELOG?

@nicolas-grekas
Copy link
Member Author

CHANGELOG updated

$response = $event->getResponse();
} elseif ($e instanceof HttpExceptionInterface) {
$code = $e->getStatusCode();
$response = new Response(isset(Response::$statusTexts[$code]) ? $code.' '.Response::$statusTexts[$code] : $code);
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to do this only in non-debug mode? And keep the exception in debug mode?

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 for keeping them in debug

Copy link
Contributor

Choose a reason for hiding this comment

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

Nevermind my prior comment. You already answered it in #26928 (comment). Please ignore. :-)

@nicolas-grekas
Copy link
Member Author

Would it make sense to do this only in non-debug mode? And keep the exception in debug mode?

HttpKernel doesn't know about debug/non-debug.
But actually, you made me realize this is not the correct approach.
The front controller needs to be updated instead.
I'm going to submit a PR on the recipe.

@nicolas-grekas nicolas-grekas deleted the 404 branch April 20, 2018 12:00
@dmaicher
Copy link
Contributor

@nicolas-grekas wondering if the front controller really is the best place for this? Shouldn't it have as little logic as possible? In functional tests the front controller is not used for example 😕

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Apr 22, 2018

Well, adding a listener is possible, nothing prevents it.
But using ExceptionHandler effectively fixes the linked issue as it is described.
We could reopen #26138 for sure, but then I'd make it simpler: it should just be a low priority exception listener that, when the event has no reponse set, uses ExceptionHandler as much as possible.
We should not design something that makes it harder for user to handle exceptions themselves (like having to unregister this fallback listener, as the linked PR does for now, and shouldn't have to.)
WDYT?

@dmaicher
Copy link
Contributor

We should not design something that makes it harder for user to handle exceptions themselves (like having to unregister this fallback listener, as the linked PR does for now, and shouldn't have to.)
WDYT?

I agree that this "fallback listener" should only set a response if there is none set yet (by any other listener with higher priority).

But if TwigBundle is registered then it makes sense to remove this fallback listener? or not? Otherwise its just a no-op listener that is instantiated and called for no reason?

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Apr 23, 2018

We could remove it, but it should not be a requirement. And in fact, it would be a micro-optimization, since listeners are lazy and exceptions are not on the typical hot path.

@nicolas-grekas
Copy link
Member Author

Let's continue on #26138

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants