<commit_before><?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
 * @license   MIT
 */

/** ErrorLoggerListener.php */
namespace Core\Listener;

use Zend\EventManager\ListenerAggregateInterface;
use Zend\Mvc\MvcEvent;
use Zend\Log\LoggerInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Application;
use Zend\Log\Logger;

/**
 * Class ErrorHandlerListener
 * @package Core\Listener
 */
class ErrorHandlerListener implements ListenerAggregateInterface
{
    
    
    /**
     * @var \Zend\Stdlib\CallbackHandler[]
     */
    protected $listeners = array();

    /**
     * @var
     */
    protected $log;

    /**
     * @var
     */
    protected $redirect;

    /**
     * @param LoggerInterface $log
     * @param null $redirect
     */
    public function __construct(LoggerInterface $log, $redirect = null)
    {
        $this->log = $log;
        if ($redirect) {
            $this->redirect = $redirect;
            register_shutdown_function(array($this, 'handleFatalError'));
        }
    }

    /**
     * @return LoggerInterface
     */
    public function getLog()
    {
        return $this->log;
    }
    
    /**
     * Attach to an event manager
     *
     * @param  EventManagerInterface $events
     * @param  integer $priority
    */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'handleError'), $priority);
        $this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'handleError'), $priority);
    }
    
    /**
     * Detach all our listeners from the event manager
     *
     * @param  EventManagerInterface $events
     * @return void
     */
    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $index => $listener) {
            if ($events->detach($listener)) {
                unset($this->listeners[$index]);
            }
        }
    }

    /**
     * @param MvcEvent $event
     */
    public function handleError(MvcEvent $event)
    {
        // Do nothing if no error in the event
        $error = $event->getError();
        if (empty($error)) {
            return;
        }
        
        switch ($error) {
            case Application::ERROR_CONTROLLER_NOT_FOUND:
            case Application::ERROR_CONTROLLER_INVALID:
            case Application::ERROR_ROUTER_NO_MATCH:
                // Specifically not handling these
                return;
        
            case Application::ERROR_EXCEPTION:
            default:
                $exception = $event->getParam('exception');
                $logMessages = array();
            
                do {
                    $priority = Logger::ERR;
                    
                    $extra = array(
                        'file'  => $exception->getFile(),
                        'line'  => $exception->getLine(),
                        'trace' => $exception->getTrace(),
                    );
                    if (isset($exception->xdebug_message)) {
                        $extra['xdebug'] = $exception->xdebug_message;
                    }
            
                    $logMessages[] = array(
                        'priority' => $priority,
                        'message'  => $exception->getMessage(),<commit_msg>[Core] Fixes gh-208: Non-existant URL returns HTTP 500<commit_after><?php
/**
 * YAWIK
 *
 * @filesource
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
 * @license   MIT
 */

/** ErrorLoggerListener.php */
namespace Core\Listener;

use Zend\EventManager\ListenerAggregateInterface;
use Zend\Mvc\MvcEvent;
use Zend\Log\LoggerInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Application;
use Zend\Log\Logger;
use Zend\Mvc\Router;

/**
 * Class ErrorHandlerListener
 * @package Core\Listener
 */
class ErrorHandlerListener implements ListenerAggregateInterface
{
    
    
    /**
     * @var \Zend\Stdlib\CallbackHandler[]
     */
    protected $listeners = array();

    /**
     * @var
     */
    protected $log;

    /**
     * @var
     */
    protected $redirect;

    /**
     * @param LoggerInterface $log
     * @param null $redirect
     */
    public function __construct(LoggerInterface $log, $redirect = null)
    {
        $this->log = $log;
        if ($redirect) {
            $this->redirect = $redirect;
            register_shutdown_function(array($this, 'handleFatalError'));
        }
    }

    /**
     * @return LoggerInterface
     */
    public function getLog()
    {
        return $this->log;
    }
    
    /**
     * Attach to an event manager
     *
     * @param  EventManagerInterface $events
     * @param  integer $priority
    */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'handleError'), $priority);
        $this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, array($this, 'handleError'), $priority);
    }
    
    /**
     * Detach all our listeners from the event manager
     *
     * @param  EventManagerInterface $events
     * @return void
     */
    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $index => $listener) {
            if ($events->detach($listener)) {
                unset($this->listeners[$index]);
            }
        }
    }

    /**
     * @param MvcEvent $event
     */
    public function handleError(MvcEvent $event)
    {
        // Do nothing if no error in the event
        $error = $event->getError();
        if (empty($error)) {
            return;
        }
        
        switch ($error) {
            case Application::ERROR_CONTROLLER_NOT_FOUND:
            case Application::ERROR_CONTROLLER_INVALID:
                // Specifically not handling these
                return;
                break;
            case Application::ERROR_ROUTER_NO_MATCH:
                if ($event->getName() == MvcEvent::EVENT_DISPATCH_ERROR) {
                    // add dummy 'no-route' route to silent routeMatch errors
                    $noRoute = 'no-route';
                    $event->getRouter()
                        ->addRoute($noRoute, Router\Http\Literal::factory(['route' => '']));
                    $event->setRouteMatch((new Router\RouteMatch([]))->setMatchedRouteName($noRoute));
                }
                break;
        
            case Application::ERROR_EXCEPTION:
            default:
                $exception = $event->getParam('exception');
                $logMessages = array();
            
                do {
                    $priority = Logger::ERR;
                    
                    $extra = array(
                        'file'  => $exception->getFile(),
                        'line'  => $exception->getLine(),
                        'trace' => $exception->getTrace(),
                    );
                    if (isset($exception->xdebug_message)) {
                        $extra['xdebug'] = $exception->xdebug_message;
                    }
            
                    $logMessages[] = array(
                        'priority' => $priority,
                        'message'  => $exception->getMessage(),
{'fraction_non_alphanumeric': 0.12381724790483914, 'fraction_numerical': 0.0032441200324412004, 'mean_word_length': 1.719220874678427, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6884822', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1950, 'n_words': 400}