Skip to content

Commit

Permalink
Add logging for render exceptions
Browse files Browse the repository at this point in the history
(fix #1657)
  • Loading branch information
zerocrates committed Oct 21, 2020
1 parent 692dbab commit 4aeca55
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions application/src/Mvc/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@
use Laminas\View\Model\ViewModel;

/**
* MVC listener for handling specific exception types with "pretty" pages.
* MVC listener for handling dispatch and render exceptions.
*/
class ExceptionListener extends AbstractListenerAggregate
{
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'handleException'], -5000);
$this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, [$this, 'handleException'], -5000);
}

/**
* Listen for specific thrown exceptions and display the proper error page
* and code for each.
* Pass all exceptions that will display an error page to the logger.
*
* For dispatch exceptions, use custom error pages and status codes for
* 403/404-type exceptions.
*
* @param MvcEvent $e
*/
Expand All @@ -42,8 +45,22 @@ public function handleException(MvcEvent $e)
return;
}

// Only modify template and error code for dispatch errors
if ($e->getName() === MvcEvent::EVENT_DISPATCH_ERROR) {
$this->modifyResponse($e);
}

$exception = $e->getParam('exception');
$e->getApplication()->getServiceManager()->get('Omeka\Logger')->err((string) $exception);
}

/**
* Convert PermissionDenied and NotFound exceptions to 403/404 errors, with
* unique templates for each.
*/
private function modifyResponse(MvcEvent $e)
{
$exception = $e->getParam('exception');

if ($exception instanceof AclException\PermissionDeniedException) {
$template = 'error/403';
Expand Down

0 comments on commit 4aeca55

Please sign in to comment.