Skip to content

[ErrorHandler] Allow override of the default non-debug template #37492

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

Merged
merged 1 commit into from
Jul 7, 2020

Conversation

PhilETaylor
Copy link
Contributor

@PhilETaylor PhilETaylor commented Jul 4, 2020

Q A
Branch? master
Bug fix? no
New feature? yes - a very tiny one
Deprecations? no
License MIT
Doc PR symfony/symfony-docs#...

Add new feature to allow the default hardcoded non-debug template to be overridden when symfony/error-handlerused as a component in other projects.

Currently, when used as a component, its not possible (that I could see) to override the standard default template of views/error.html.php therefore the look and feel, and text is hard coded. There is much written on how to customise the error pages when using the full symfony stack, but not when using individual symfony/error-handler component on its own.

This PR is related to the use of symfony/error-handler as a component in other projects - specifically the Joomla Content Management System, where, in non debug mode, a generic template needs to be translatable, and customisable even though most of the time when the error occurs will be at boot time and not much available to it. joomla/joomla-cms#29968

This PR allows the use of code such as

<?php

require 'vendor/autoload.php';

use Symfony\Component\ErrorHandler\ErrorHandler;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;

ErrorHandler::register();
HtmlErrorRenderer::setTemplate('/path/to/custom/mine.php');

throw new Exception('myException is this', 500);

and then for further customisation of the "look and feel" to be done in mine.php

Due to the unique way the error handler component is init'ed, the use of static calls has been employed rather than any DI approach, I hope this is ok.

@PhilETaylor
Copy link
Contributor Author

ready for review again :-) Thx.

Any idea how long 5.2.0 release will take? Just so we can plan for the integration with the Joomla 4 release that's in development, need to see if the work needed to translate/customise the Joomla error page - which relies on this PR - is worth doing for the Joomla 4 release or waiting for Joomla 4.1 - Thanks again,.

@yceruto
Copy link
Member

yceruto commented Jul 6, 2020

About the release of 5.2 this will be out in November 2020 (https://symfony.com/releases/5.2)

@PhilETaylor
Copy link
Contributor Author

I have allowed a relative or absolute path to custom non-debug template with another light touch, simply checking if the file exists, this then has no knock on effects when used in a full Symfony stack or when no custom template is set.

Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@PhilETaylor
Copy link
Contributor Author

Awesome - thanks!

Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

LGTM (minor comment)

@fabpot fabpot force-pushed the overridetemplate branch from 78579f0 to 6e1d16b Compare July 7, 2020 08:33
@fabpot
Copy link
Member

fabpot commented Jul 7, 2020

Thank you @PhilETaylor.

@dvdknaap
Copy link

@fabpot Can you merge this PR also to 4.4 ?

nicolas-grekas added a commit that referenced this pull request Mar 16, 2021
…r (stlrnz)

This PR was submitted for the 5.x branch but it was merged into the 5.2 branch instead.

Discussion
----------

[ErrorHandler] Fix error caused by `include` + open_basedir

| Q             | A
| ------------- | ---
| Branch?       | 5.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

#37492 introduced the option to specify your own error template using `HtmlErrorRenderer::setTemplate('...');`

However, the implementation using `file_exists(...)` in `include` can cause an error when not setting a custom template.
> php.WARNING: Warning: file_exists(): open_basedir restriction in effect. File(assets/css/error.css) is not within the allowed path(s): (...) {"exception":"[object] (ErrorException(code: 0): Warning: file_exists(): open_basedir restriction in effect. File(assets/css/error.css) is not within the allowed path(s): (...) at ...\\vendor\\symfony\\error-handler\\ErrorRenderer\\HtmlErrorRenderer.php:355)"} []

As you can see the error is caused by checking `file_exists(...)` using relative paths in environments with a restrictive `open_basedir` policy.

The proposed solution always uses absolute paths to include errors templates (and other files).

Commits
-------

9ad7832 [ErrorHandler] Fix error caused by `include` + open_basedir
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.

6 participants