Skip to content

Add an action to show *error* pages in kernel.debug mode #12096

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 6 commits into from
Closed

Add an action to show *error* pages in kernel.debug mode #12096

wants to merge 6 commits into from

Conversation

mpdude
Copy link
Contributor

@mpdude mpdude commented Oct 2, 2014

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #7446, #1486, #11327
License MIT
Doc PR symfony/symfony-docs#4293

See #7446 for the initial reasoning. In short, add to your routing_development.yml file the following

_errors:
    resource: "@TwigBundle/Resources/config/routing/errors.xml"
    prefix:   /_error

Then you can use http://localhost/app_dev.php/_error/xxx to preview the HTML error page that the default ExceptionController (from TwigBundle) would pick for the XXX status code.

You can also use http://localhost/app_dev.php/_error/xxx.{some_format} to show error pages for other formats than HTML, most notably txt.

Note that the status code will be 500 for all exceptions that do not implement HttpExceptionInterface.

Want to test with a custom exception?

Folks might want to display (part of) the exception even on error pages and thus need to work with generic (own) exceptions.

They could write an arbitrary controller and throw their exception there. By default, the ExceptionController would be used to handle this, only that it would not show error pages in kernel.debug mode.

Thus, a simple public setter to change the debug flag after construction could help. Do we want to add that as well?

If you want to test error pages with your own exceptions,

  • create a subclass of ExceptionController
  • set the protected debug flag to false
  • register it as twig.exception_controller in the config
  • throw your custom exception from any controller.

That should give you the error page also in kernel.debug mode.

To-Do
  • Update docs
  • Add route in symfony/symfony-default

@fabpot
Copy link
Member

fabpot commented Oct 2, 2014

@weaverryan Can you have a look at this PR. It would be great if we can accept it today or tomorrow for inclusion in 2.6. Thanks.

@mpdude
Copy link
Contributor Author

mpdude commented Oct 2, 2014

When do you need PRs for symfony/symfony-standard and or -docs at the latest?

@fabpot
Copy link
Member

fabpot commented Oct 2, 2014

@mpdude before the end of the week (Sunday.)

{
try {
throw new \Exception("Something has intentionally gone wrong.");
} catch (\Exception $exception) {
Copy link
Member

Choose a reason for hiding this comment

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

Was this on accident or am I missing the point of this try-catch block? Should we just have the return new Response part?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, nevermind, I see what you are doing. Why not just create the Exception and use it - I don't think we need to actually throw it here:

$exception = new \Exception('This is a generic exception being used to help test your error page');

return new Response(...);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was unsure whether this was needed to give the exception for example a "real" stack trace, just in case the error page is to display that...

Copy link
Member

Choose a reason for hiding this comment

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

The stack trace is created when the exception is created, so you can remove the try/catch block.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just did that :-)

@weaverryan
Copy link
Member

@mpdude Thanks for this!

First, everything works exactly as advertised and I only spotted one change I can see to make.

Second - about your to be discussed: I understand your point about custom exception messages. In this PR, we just create a generic \Exception with a generic message. So you're right that if someone is doing something in the template with their custom Exception class or message, then they can't really replicate that here.

Do we fix this? As you said, adding a setDebug method to ExceptionController (or maybe you call it ->forceProductErrorPage()), then you could call this in your controller temporarily to test your custom error message. But that seems hacky enough and enough of an edge case that I'm -1 on it.

Cheers!

@mpdude mpdude changed the title Add an action to show *error* pages in kernel.debug mode [WIP] Add an action to show *error* pages in kernel.debug mode Oct 3, 2014
@mpdude mpdude changed the title [WIP] Add an action to show *error* pages in kernel.debug mode Add an action to show *error* pages in kernel.debug mode Oct 3, 2014
@fabpot
Copy link
Member

fabpot commented Oct 3, 2014

👍 Looks good to me. Can you submit a PR on symfony/symfony-standard?

@mpdude
Copy link
Contributor Author

mpdude commented Oct 3, 2014

Thanks! See symfony/symfony-standard#720.

@lyrixx
Copy link
Member

lyrixx commented Oct 3, 2014

Yeah, this is very simple ! Nice contributions !

@mpdude
Copy link
Contributor Author

mpdude commented Oct 3, 2014

I will need another 24h for a docs PR. Can also write up something for the Symfony blog of you like.

*
* @return Response
*/
protected function createResponse(Request $request, FlattenException $exception, $debug, DebugLoggerInterface $logger = null, $currentContent = '')
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be private

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was in the meantime, but I reverted it back to protected.

Reason: The docs mention subclassing this controller in order to pass additional variables to the template. As this part of the code as moved from the showAction to this method, I'd like to keep the possibility to override it.

@stof agree?

@fabpot
Copy link
Member

fabpot commented Oct 4, 2014

@mpdude If you can write the blog post the the Symfony blog as well, that would be wonderful. Thanks.

mpdude added 2 commits October 4, 2014 10:31
The docs (http://symfony.com/doc/current/cookbook/controller/error_pages.html) explicitly
mentioned subclassing the controller and overriding the showAction method to pass
additional variables to the template.

The variables are now defined in createResponse(), so I'd like to keep the possibility
to change them. The new testErrorPageAction() method might even work in these cases.
@mpdude
Copy link
Contributor Author

mpdude commented Oct 4, 2014

Doc PR at symfony/symfony-docs#4293.

@mpdude
Copy link
Contributor Author

mpdude commented Oct 4, 2014

Here's a suggestion for the blog article: https://gist.github.com/mpdude/ffa505570858e674c62b.

@fabpot
Copy link
Member

fabpot commented Oct 5, 2014

Thank you @mpdude.

@fabpot fabpot closed this Oct 5, 2014
fabpot added a commit that referenced this pull request Oct 5, 2014
…de (mpdude)

This PR was squashed before being merged into the 2.6-dev branch (closes #12096).

Discussion
----------

Add an action to show *error* pages in kernel.debug mode

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #7446, #1486, #11327
| License       | MIT
| Doc PR        | symfony/symfony-docs#4293

See #7446 for the initial reasoning. In short, add to your `routing_development.yml` file the following

```yaml
_errors:
    resource: "@TwigBundle/Resources/config/routing/errors.xml"
    prefix:   /_error
```

Then you can use `http://localhost/app_dev.php/_error/xxx` to preview the HTML *error* page that the default `ExceptionController` (from `TwigBundle`) would pick for the XXX status code.

You can also use `http://localhost/app_dev.php/_error/xxx.{some_format}` to show error pages for other formats than HTML, most notably `txt`.

Note that the status code will be 500 for all exceptions [that do not implement `HttpExceptionInterface`](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Debug/Exception/FlattenException.php#L47).

##### Want to test with a custom exception?

~~Folks might want to display (part of) the exception even on error pages and thus need to work with generic (own) exceptions.~~

~~They could write an arbitrary controller and throw their exception there. By default, the `ExceptionController` would be used to handle this, only that it would not show *error* pages in `kernel.debug` mode.~~

~~Thus, a simple public setter to change the `debug` flag after construction could help. Do we want to add that as well?~~

If you want to test error pages with your own exceptions,
* create a subclass of `ExceptionController`
* set the protected `debug` flag to false
* register it as twig.exception_controller in the config
* throw your custom exception from any controller.

That should give you the error page also in `kernel.debug` mode.

##### To-Do
- [x] Update docs
- [x] Add route in symfony/symfony-default

Commits
-------

66ed177 Add an action to show *error* pages in kernel.debug mode
fabpot added a commit to symfony/symfony-standard that referenced this pull request Oct 5, 2014
This PR was merged into the 2.6-dev branch.

Discussion
----------

Add route to preview error pages in development

See symfony/symfony#12096 for details.

I think this is my first contribution to symfony-standard, does that earn me a badge :-)?

Commits
-------

c7d9f6a Add route to preview error pages in development (see Symfony issue #12096)
@mpdude mpdude deleted the test-error-pages branch October 6, 2014 11:24
weaverryan added a commit to symfony/symfony-docs that referenced this pull request Oct 19, 2014
This PR was merged into the master branch.

Discussion
----------

Document error page preview (Symfony ~2.6)

This adds documentation how to use the new preview feature from symfony/symfony#12096 (and hopefully the fix in symfony/symfony#12147).

Commits
-------

d02c7c4 Updates according to GH feedback
8e70373 Document error page preview in Symfony ~2.6 (#4293)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants