Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
[Improved] Wrapped all templates->render() calls in try/catch block…
Browse files Browse the repository at this point in the history
…s to more gracefully handle errors
  • Loading branch information
Andrew Welch committed Feb 12, 2017
1 parent 787efcd commit 027fd28
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [Fixed] Fixed a regression that could cause FieldTypes to not save properly
* [Improved] Moved the lattitude/longitude info down below the address information for on the Site Identity & Site Creator settings
* [Improved] Wrapped all `templates->render()` calls in try/catch blocks to more gracefully handle errors

## 1.1.41 -- 2017.02.10

Expand Down
3 changes: 2 additions & 1 deletion releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"date": "2017-02-12T11:00:00-11:00",
"notes": [
"[Fixed] Fixed a regression that could cause FieldTypes to not save properly",
"[Improved] Moved the lattitude/longitude info down below the address information for on the Site Identity & Site Creator settings"
"[Improved] Moved the lattitude/longitude info down below the address information for on the Site Identity & Site Creator settings",
"[Improved] Wrapped all `templates->render()` calls in try/catch blocks to more gracefully handle errors"
]
},
{
Expand Down
121 changes: 89 additions & 32 deletions services/SeomaticService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ public function renderSiteMeta($templatePath="", $metaVars=null, $locale)

/* -- If Minify is installed, minify all the things */

if (craft()->plugins->getPlugin('Minify'))
$htmlText = craft()->minify->htmlMin($this->render($templatePath, $metaVars));
else
$htmlText = $this->render($templatePath, $metaVars);
try
{
if (craft()->plugins->getPlugin('Minify'))
$htmlText = craft()->minify->htmlMin($this->render($templatePath, $metaVars));
else
$htmlText = $this->render($templatePath, $metaVars);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderSiteMeta()', LogLevel::Error);
}

if ($shouldCache)
craft()->cache->set($cacheKey, $htmlText, null);

Expand Down Expand Up @@ -106,7 +112,11 @@ public function render($templatePath="", $metaVars=null, $isPreview=false)

if ($templatePath)
{
$htmlText = craft()->templates->render($templatePath, $metaVars);
try {
$htmlText = craft()->templates->render($templatePath, $metaVars);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in render()', LogLevel::Error);
}
}
else
{
Expand All @@ -119,13 +129,16 @@ public function render($templatePath="", $metaVars=null, $isPreview=false)
$templateName = '_seo_meta';
if ($isPreview)
$templateName = $templateName . 'Preview';
if ($metaVars)
{
$this->sanitizeMetaVars($metaVars);
$htmlText = craft()->templates->render($templateName, $metaVars);
try {
if ($metaVars) {
$this->sanitizeMetaVars($metaVars);
$htmlText = craft()->templates->render($templateName, $metaVars);
}
else
$htmlText = craft()->templates->render($templateName);
else
$htmlText = craft()->templates->render($templateName);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in render()', LogLevel::Error);
}

method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($oldPath) : craft()->path->setTemplatesPath($oldPath);
}
Expand Down Expand Up @@ -176,10 +189,14 @@ public function renderJSONLD($object=array(), $isPreview=false)
/* -- Render the core template */

$templateName = 'json-ld/_json-ld';
if (craft()->plugins->getPlugin('Minify') && !$isPreview)
$htmlText = craft()->minify->jsMin($htmlText = craft()->templates->render($templateName, $vars));
else
$htmlText = craft()->templates->render($templateName, $vars);
try {
if (craft()->plugins->getPlugin('Minify') && !$isPreview)
$htmlText = craft()->minify->jsMin($htmlText = craft()->templates->render($templateName, $vars));
else
$htmlText = craft()->templates->render($templateName, $vars);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderJSONLD()', LogLevel::Error);
}

method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($oldPath) : craft()->path->setTemplatesPath($oldPath);

Expand All @@ -199,7 +216,11 @@ public function renderDisplayPreview($templateName="", $metaVars)
/* -- Render the SEOmatic display preview template */

$this->sanitizeMetaVars($metaVars);
$htmlText = craft()->templates->render($templateName, $metaVars);
try {
$htmlText = craft()->templates->render($templateName, $metaVars);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderDisplayPreview()', LogLevel::Error);
}

method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($oldPath) : craft()->path->setTemplatesPath($oldPath);

Expand Down Expand Up @@ -307,12 +328,16 @@ public function renderGoogleTagManager($metaVars, $locale, $isPreview=false)
/* -- Render the core template */

$templateName = '_googleTagManager';
if (craft()->plugins->getPlugin('Minify') && !$isPreview)
$htmlText = craft()->minify->jsMin($htmlText = craft()->templates->render($templateName, $metaVars));
else
$htmlText = craft()->templates->render($templateName, $metaVars);
try {
if (craft()->plugins->getPlugin('Minify') && !$isPreview)
$htmlText = craft()->minify->jsMin($htmlText = craft()->templates->render($templateName, $metaVars));
else
$htmlText = craft()->templates->render($templateName, $metaVars);

method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($oldPath) : craft()->path->setTemplatesPath($oldPath);
method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($oldPath) : craft()->path->setTemplatesPath($oldPath);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderGoogleTagManager()', LogLevel::Error);
}
}
return $htmlText;
} /* -- renderGoogleTagManager */
Expand All @@ -334,10 +359,14 @@ public function renderGoogleAnalytics($metaVars, $locale, $isPreview=false)
/* -- Render the core template */

$templateName = '_googleAnalytics';
if (craft()->plugins->getPlugin('Minify') && !$isPreview)
$htmlText = craft()->minify->jsMin($htmlText = craft()->templates->render($templateName, $metaVars));
else
$htmlText = craft()->templates->render($templateName, $metaVars);
try {
if (craft()->plugins->getPlugin('Minify') && !$isPreview)
$htmlText = craft()->minify->jsMin($htmlText = craft()->templates->render($templateName, $metaVars));
else
$htmlText = craft()->templates->render($templateName, $metaVars);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderGoogleAnalytics()', LogLevel::Error);
}

method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($oldPath) : craft()->path->setTemplatesPath($oldPath);
}
Expand Down Expand Up @@ -371,7 +400,11 @@ public function renderHumans($isPreview=false)

if ($templatePath)
{
$htmlText = craft()->templates->render($templatePath);
try {
$htmlText = craft()->templates->render($templatePath);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderHumans()', LogLevel::Error);
}
}
else
{
Expand All @@ -384,7 +417,11 @@ public function renderHumans($isPreview=false)
$templateName = '_humans';
if ($isPreview)
$templateName = $templateName . 'Preview';
$htmlText = craft()->templates->render($templateName, $metaVars);
try {
$htmlText = craft()->templates->render($templateName, $metaVars);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderHumans()', LogLevel::Error);
}

method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($oldPath) : craft()->path->setTemplatesPath($oldPath);
}
Expand All @@ -408,7 +445,11 @@ public function renderHumansTemplate()
/* -- Render the user-defined Humans.txt template */

$template = $creator['genericCreatorHumansTxt'];
$htmlText = craft()->templates->renderString($template, $metaVars);
try {
$htmlText = craft()->templates->renderString($template, $metaVars);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderHumansTemplate()', LogLevel::Error);
}

return $htmlText;
} /* -- renderHumansTemplate */
Expand All @@ -427,7 +468,11 @@ public function renderRobots($isPreview=false)

if ($templatePath)
{
$htmlText = craft()->templates->render($templatePath);
try {
$htmlText = craft()->templates->render($templatePath);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderRobots()', LogLevel::Error);
}
}
else
{
Expand All @@ -440,7 +485,11 @@ public function renderRobots($isPreview=false)
$templateName = '_robots';
if ($isPreview)
$templateName = $templateName . 'Preview';
$htmlText = craft()->templates->render($templateName, $metaVars);
try {
$htmlText = craft()->templates->render($templateName, $metaVars);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderRobots()', LogLevel::Error);
}

method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($oldPath) : craft()->path->setTemplatesPath($oldPath);
}
Expand All @@ -464,7 +513,11 @@ public function renderRobotsTemplate()
/* -- Render the user-defined robots.txt template */

$template = $siteMeta['siteRobotsTxt'];
$htmlText = craft()->templates->renderString($template, $metaVars);
try {
$htmlText = craft()->templates->renderString($template, $metaVars);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderRobotsTemplate()', LogLevel::Error);
}

return $htmlText;
} /* -- renderRobotsTemplate */
Expand Down Expand Up @@ -492,7 +545,11 @@ public function renderSeoMetrics()
'metricsActionUrl' => $metricsActionUrl,
);

$htmlText = craft()->templates->render('_seo_metrics_floater.twig', $vars);
try {
$htmlText = craft()->templates->render('_seo_metrics_floater.twig', $vars);
} catch (\Exception $e) {
SeomaticPlugin::log('Error rendering template in renderSeoMetrics()', LogLevel::Error);
}
craft()->templates->includeFootHtml($htmlText);

method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($oldPath) : craft()->path->setTemplatesPath($oldPath);
Expand Down

0 comments on commit 027fd28

Please sign in to comment.