Skip to content

Commit

Permalink
#534 Make the debugging check before rendering with our own handler
Browse files Browse the repository at this point in the history
  • Loading branch information
amazeika committed Dec 8, 2021
1 parent 7bc51da commit 611f45b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,66 +33,87 @@ protected function _initialize(KObjectConfig $config)
*/
public function onException(KEvent $event)
{
$request = $this->getObject('request');
$exception = $event->exception;

//Make sure the output buffers are cleared
$level = ob_get_level();
while($level > 0) {
ob_end_clean();
$level--;
}

//If the error code does not correspond to a status message, use 500
$code = $exception->getCode();
if(!isset(KHttpResponse::$status_messages[$code])) {
$code = '500';
if(JDEBUG)
{
if (Koowa::isDebug()) {
$this->_renderKoowaError($event);
} else {
$this->_renderJoomlaError($event);
}
}
else $this->_renderJoomlaError($event);
}

$is_joomla4 = version_compare(JVERSION, 4, '>=');
protected function _renderKoowaError(KEvent $event)
{
$request = $this->getObject('request');
$exception = $event->exception;

//Render the exception
if(!JDEBUG)
//Render the exception backtrace if debug mode is enabled and format is html or json

if(in_array($request->getFormat(), array('json', 'html')))
{
//Render the Joomla error page if debug mode is disabled and format is html
if(!$is_joomla4 && class_exists('JErrorPage') && $request->getFormat() == 'html')
{
if(ini_get('display_errors')) {
$message = $exception->getMessage();
} else {
$message = KHttpResponse::$status_messages[$code];
}
$dispatcher = $this->getObject('com:koowa.dispatcher.http');

//Set status code (before rendering the error)
$dispatcher->getResponse()->setStatus($this->_getCode($exception));

$content = $this->getObject('com:koowa.controller.error', ['request' => $request])
->layout('default')
->render($exception);

//Set error in the response
$dispatcher->getResponse()->setContent($content);
$dispatcher->send();
}
}

$message = $this->getObject('translator')->translate($message);
protected function _renderJoomlaError(KEvent $event)
{
$is_joomla4 = version_compare(JVERSION, 4, '>=');
$request = $this->getObject('request');

$class = get_class($exception);
$error = new $class($message, $exception->getCode());
// Only render the Error ourselves if we are running Joomla 3 and format is HTML

JErrorPage::render($error);
if(!$is_joomla4 && class_exists('JErrorPage') && $request->getFormat() == 'html')
{
$exception = $event->exception;

JFactory::getApplication()->close(0);
if(ini_get('display_errors')) {
$message = $exception->getMessage();
} else {
$message = KHttpResponse::$status_messages[$this->_getCode($exception)];
}

return false;
$message = $this->getObject('translator')->translate($message);

$class = get_class($exception);

$error = new $class($message, $exception->getCode());

JErrorPage::render($error);

JFactory::getApplication()->close(0);
}
else
{
//Render the exception backtrace if debug mode is enabled and format is html or json
if(in_array($request->getFormat(), array('json', 'html')))
{
$dispatcher = $this->getObject('com:koowa.dispatcher.http');
}

//Set status code (before rendering the error)
$dispatcher->getResponse()->setStatus($code);
protected function _getCode(\Throwable $exception)

This comment has been minimized.

Copy link
@johanjanssens

johanjanssens Dec 10, 2021

Member

@amazeika I would rename this to _getErrorCode()

{
//If the error code does not correspond to a status message, use 500

$content = $this->getObject('com:koowa.controller.error', ['request' => $request])
->layout('default')
->render($exception);
$code = $exception->getCode();

//Set error in the response
$dispatcher->getResponse()->setContent($content);
$dispatcher->send();
}
if(!isset(KHttpResponse::$status_messages[$code])) {
$code = '500';
}

return $code;
}
}
12 changes: 3 additions & 9 deletions code/plugins/system/joomlatools/joomlatools.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ public function __construct($subject, $config = array())
}

//Bootstrap the Koowa Framework
if ($this->bootstrap())
{
if (version_compare(JVERSION, '4', '>=') && Koowa::isDebug()) {
$subject->addListener('onError', array($this, 'onJ4Error'), Joomla\Event\Priority::ABOVE_NORMAL);
}
}
$this->bootstrap();

$this->onAfterKoowaBootstrap();

Expand Down Expand Up @@ -107,6 +102,7 @@ public function bootstrap()
* Framework Bootstrapping
*/
Koowa::getInstance(array(
'debug' => false,
'cache' => false, //JFactory::getConfig()->get('caching')
'cache_namespace' => 'koowa-' . $application . '-' . md5(JFactory::getConfig()->get('secret')),
'root_path' => JPATH_ROOT,
Expand Down Expand Up @@ -244,10 +240,8 @@ public function onBeforeRender()
* @see: https://github.com/joomla/joomla-cms/blob/4.0-dev/libraries/src/Application/CMSApplication.php#L296
* @return void
*/
public function onJ4Error($event)
public function onError($exception)
{
$exception = $event->getError();

if ($exception instanceof \Throwable) {
$this->_proxyEvent('onException', ['exception' => $exception]);
}
Expand Down

0 comments on commit 611f45b

Please sign in to comment.