Skip to content

Commit

Permalink
Merge pull request #139 from joomlatools/feature/138-error
Browse files Browse the repository at this point in the history
Custom error pages
  • Loading branch information
johanjanssens authored Jun 20, 2019
2 parents c2ce663 + 5173bcf commit 10caf0d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
8 changes: 1 addition & 7 deletions code/site/components/com_pages/controller/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ protected function _initialize(KObjectConfig $config)

public function getFormats()
{
if($this->getObject('dispatcher')->getRouter()->resolve()) {
$formats = array($this->getRequest()->getFormat());
} else {
$formats = array();
}

return $formats;
return array($this->getRequest()->getFormat());
}

protected function _afterRender(KControllerContextInterface $context)
Expand Down
35 changes: 35 additions & 0 deletions code/site/components/com_pages/dispatcher/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,41 @@ protected function _actionDispatch(KDispatcherContextInterface $context)
KDispatcherAbstract::_actionDispatch($context);
}

protected function _renderError(KDispatcherContextInterface $context)
{
if(!JDEBUG && $this->getObject('request')->getFormat() == 'html')
{
//Get the exception object
if($context->param instanceof KEventException) {
$exception = $context->param->getException();
} else {
$exception = $context->param;
}

foreach([(int) $exception->getCode(), '500'] as $code)
{
if($page = $this->getObject('page.registry')->getPage($code))
{
//Set the controller
$this->setController('page', ['view' => $page->getType()]);

//Set page in model
$this->getController()->getModel()->setPage($page, $context->request->query->toArray());

//Render the error
$content = $this->getController()->render($exception);

//Set error in the response
$context->response->setContent($content);

return true;
}
}
}

return parent::_renderError($context);
}

public function getContext()
{
$context = new ComPagesDispatcherContext();
Expand Down
32 changes: 32 additions & 0 deletions code/site/components/com_pages/event/subscriber/errorhandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Joomlatools Pages
*
* @copyright Copyright (C) 2018 Johan Janssens and Timble CVBA. (http://www.timble.net)
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository
*/

class ComPagesEventSubscriberErrorhandler extends KEventSubscriberAbstract
{
public function __construct( KObjectConfig $config)
{
parent::__construct($config);

//Exception Handling
$this->getObject('event.publisher')->addListener('onException', array($this, 'onException'));
}

public function onException(KEventException $event)
{
if(!JDEBUG && $this->getObject('request')->getFormat() == 'html')
{
if($this->getObject('com://site/pages.dispatcher.http')->fail($event)) {
return true;
}
}

return false;
}
}

7 changes: 5 additions & 2 deletions code/site/components/com_pages/page/registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public function loadCache($basedir, $refresh = true)
}

//Set the route
if (!$page->route) {
if (!$page->route && $page->route !== false) {
$page->route = $route;
}

Expand Down Expand Up @@ -347,8 +347,11 @@ public function loadCache($basedir, $refresh = true)
//Page
$pages[$file] = $page->toArray();


//Route
$routes[$route] = (array) KObjectConfig::unbox($page->route);
if($page->route !== false) {
$routes[$route] = (array) KObjectConfig::unbox($page->route);
}

//File (do not include index pages)
if(strpos($file, '/index') === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'event.subscriber.factory' => [
'subscribers' => [
'com:pages.event.subscriber.pagedecorator',
'com:pages.event.subscriber.errorhandler',
]
],
'lib:template.engine.markdown' => [
Expand Down

0 comments on commit 10caf0d

Please sign in to comment.