Fixed Templater->isKnownTemplate() throws error when template doesn't exist

mazzly

Well-known member
Affected version
2.2.7
Since version 2.2.7, Templater->isKnownTemplate() seems to throw errors during the check if the template doesn't exist, which seems counterproductive to the intended functionality.

It might also be something further down the chain like Templater->getTemplateData() that doesn't correctly respect the $errorOnUnknown-param?

We've gotten 2 similar reports recently for this problem:
Code:
Server error log
ErrorException: Template error: [E_WARNING] include(/home/caf/public_html/internal_data/code_cache/templates/l1/s109/public/_amp_media_site_embed_wikiloops.php): failed to open stream: No such file or directory src/XF/Template/Templater.php:313
Generated by: Unknown account Sep 14, 2021 at 1:30 PM
Stack trace
#0 src/XF/Template/Templater.php(313): XF\Template\Templater->handleTemplateError(2, '[E_WARNING] inc...', '/home/caf/publi...', 313, Array)
#1 src/XF/Template/Templater.php(313): include()
#2 src/XF/Template/Templater.php(684): XF\Template\Templater->getTemplateDataFromSource('public', '_amp_media_site...')
#3 src/XF/Template/Templater.php(1569): XF\Template\Templater->getTemplateData('public', '_amp_media_site...', false)
#4 src/addons/MaZ/AMP/XF/Template/XF22/Templater.php(39): XF\Template\Templater->isKnownTemplate('_amp_media_site...')

Code:
ErrorException: Template error: [E_WARNING] include(): Failed opening '/home/nginx/domains/omitted/public/internal_data/code_cache/templates/l4/s4/public/_amp_media_site_embed_speedtest.php' for inclusion (include_path='.:/usr/local/lib/php') src/XF/Template/Templater.php:313

#0 src/XF/Template/Templater.php(313): XF\Template\Templater->handleTemplateError(2, '[E_WARNING] inc...', '/home/nginx/dom...', 313)
#1 src/XF/Template/Templater.php(313): include()
#2 src/XF/Template/Templater.php(684): XF\Template\Templater->getTemplateDataFromSource('public', '_amp_media_site...')
#3 src/XF/Template/Templater.php(1569): XF\Template\Templater->getTemplateData('public', '_amp_media_site...', false)
#4 src/addons/MaZ/AMP/XF/Template/XF22/Templater.php(39): XF\Template\Templater->isKnownTemplate('_amp_media_site...')
 
This is not something I can reproduce.

Code:
try
{
   $file = include($path);
}
catch (\Throwable $e)
{
   return false;
}

This should prevent the issue you are reporting. Further, we didn't make any changes in this area for XF 2.2.7.

It's not really clear what you're running into but this doesn't look like a bug on the surface.
 
@Lukas W. mentioned running into the same error at some point after upgrading to 2.2.7. Prior to the upgrade the only thing that happened was a warning that Template public:name was rendered, after that same non existent template rendered 3 separate warnings.

X92l9X3.png


Obviously not really a big deal since the reference to the template shouldn't have existed in the first place if the template didn't, but it does seem like there was some change that caused the one warning to turn into 3 with 2.2.7
 
Hmm weird, both those 2 customers have been running our addon since January without problems, and after upgrading to version 2.2.7 they started seeing that error triggered in their logs..

I think in both cases the places where it is triggered is related to templates from "BB code media sites"..

I guess I'll just have to wrap the Templater->isKnownTemplate() call within a try/except then to be sure, just thought it was weird that errors are thrown when the isKnownTemplate-function explicitly calls $this->getTemplateData($type, $template, false); which should prevent errors from bubbling up..

Anyway what AMPXF does there is:
  • Check if there exists a custom version of _media_site_embed_FOO called _amp_media_site_embed_FOO
  • If exists: render it
  • If not: render original one, which will later be handled by converting whatever e.g. iframe into amp-iframe etc
 
@Lukas W. mentioned running into the same error at some point after upgrading to 2.2.7. Prior to the upgrade the only thing that happened was a warning that Template public:name was rendered, after that same non existent template rendered 3 separate warnings.

X92l9X3.png


Obviously not really a big deal since the reference to the template shouldn't have existed in the first place if the template didn't, but it does seem like there was some change that caused the one warning to turn into 3 with 2.2.7

I saw this error yesterday too when I tried to render a template I hadn't created yet and was sure it used to just display the third error and not the include errors.
 
I guess I'll just have to wrap the Templater->isKnownTemplate() call within a try/except then to be sure, just thought it was weird that errors are thrown when the isKnownTemplate-function explicitly calls $this->getTemplateData($type, $template, false); which should prevent errors from bubbling up..
Hmm I actually probably won't do that, because it seems that error should already be handled.. But still triggers the error for some reason..
This is not something I can reproduce.

Code:
try
{
   $file = include($path);
}
catch (\Throwable $e)
{
   return false;
}

This should prevent the issue you are reporting. Further, we didn't make any changes in this area for XF 2.2.7.

It's not really clear what you're running into but this doesn't look like a bug on the surface.
Looking at the XF source this is exactly what the core XF code does.. so why the error bubbles up with 2.2.7 now doesn't make sense

1. AMPXF calls Templater->isKnownTemplate('foo')
2. Templater->isKnownTemplate('foo'); goes into:
3. Templater->getTemplateData($type, $template, false); which does:
4. Templates->$data = $this->getTemplateDataFromSource($type, $template); which already has that exact try/except you wrote above..

So I don't think adding another try/except higher up in the chain would help much? 🤔
 
I've just been reminded that we did make some changes here:

We're looking into it.
 
The try / catch there won't work on a warning unless a custom handler is set. Can probably just prefix the include with @ to suppress that warning so the function returns false and limits it to just the one message saying the template doesn't exist
 
I think this is a php 7.x vs php 8 thing?

The try / catch there won't work on a warning unless a custom handler is set. Can probably just prefix the include with @ to suppress that warning so the function returns false and limits it to just the one message saying the template doesn't exist
This is very likely a production vs development mode thing; as XenForo turns these warnings into exceptions in development mode.
 
I think this is a php 7.x vs php 8 thing?


This is very likely a production vs development mode thing; as XenForo turns these warnings into exceptions in development mode.
The customers from above logs vere using 8.0.7 and 7.4.22 so that should rule out that.

Also it worked fine for them until upgrading to 2.2.7
 
Seeing similar (but not necessarily the same) errors with PHP 7.3.30 and the AMP addon is not installed.

 
The templater uses a different error handler during rendering processes which does not throw exceptions for errors, which seems to be the crux of this issue. I imagine we'll just silence this error (or handle it explicitly in the error handler) since it is possible to trigger it during rendering, but simply calling isKnownTemplate() itself shouldn't normally cause this unless it's being called in the middle of rendering.
 
Not sure if this is related to the issue discussed, but since upgrading to 2.2.7 Patch 1 I am now receiving this error in my server logs:

  • ErrorException: Template error: [E_WARNING] include(): Failed opening '/home/watchint/public_html/internal_data/code_cache/phrase_groups/l1/custom_bb_code_output.php' for inclusion (include_path='.:/opt/alt/php80/usr/share/pear:/opt/alt/php80/usr/share/php:/usr/share/pear:/usr/share/php')
  • src/XF/Language.php:287
  • Generated by: Unknown account
  • Oct 3, 2021 at 12:42 PM
 
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.8).

Change log:
Silence errors when a template doesn't exist
There may be a delay before changes are rolled out to the XenForo Community.
 
Top Bottom