<commit_before><?php

namespace Elixir\Kernel\Middleware;

use Elixir\DI\ContainerAwareInterface;
use Elixir\DI\ContainerInterface;
use Elixir\HTTP\ResponseFactory;
use Elixir\HTTP\ResponseInterface;
use Elixir\Kernel\ApplicationEvent;
use Elixir\Kernel\ApplicationInterface;
use Elixir\Kernel\HTTPKernelInterface;
use Elixir\Kernel\Middleware\MiddlewareInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class ErrorMiddleware implements MiddlewareInterface, ContainerAwareInterface
{
    /**
     * @var string|array|callable
     */
    protected $failbackErrorController;
    
    /**
     * @var ContainerInterface
     */
    protected $container;
    
    /**
     * @var HTTPKernelInterface
     */
    protected $kernel;
    
    /**
     * @param string|array|callable $failbackErrorController
     */
    public function __construct($failbackErrorController = null) 
    {
        $this->failbackErrorController = null;
    }

    /**
     * {@inheritdoc}
     */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
        $this->kernel = $container->get('kernel');
    }
    
    /**
     * {@inheritdoc}
     */
    public function __invoke($request, $response, callable $next) 
    {
        try
        {
            $response = $next($request, $response);
        }
        catch(\Exception $e)
        {
            switch($e->getCode())
            {
                case 403:
                case 404:
                case 500:
                    $statusCode = $e->getCode();
                    $message = $e->getMessage();
                    break;
                default:
                    $statusCode = 500;
                    $message = 'Internal Server Error';
            }
            
            if ($this->kernel instanceof ApplicationInterface)
            {
                $event = new ApplicationEvent(ApplicationEvent::EXCEPTION, ['request' => $request, 'response' => $response, 'exception' => $e]);
                $this->kernel->dispatch($event);
                
                $response = $event->getRequest();
                $response = $event->getResponse();
                
                if ($response instanceof ResponseInterface)
                {
                    $response = $response->withStatus($statusCode);
                    return $response;
                }
            }
            
            if ($request->isMainRequest())
            {
                $controller = $this->failbackErrorController;
                
                if ($request->hasAttribute('_module'))
                {
                    $controller = 'error';
                }
                
                if (empty($controller))
                {<commit_msg>Work in progress<commit_after><?php

namespace Elixir\Kernel\Middleware;

use Elixir\DI\ContainerAwareInterface;
use Elixir\DI\ContainerInterface;
use Elixir\HTTP\ResponseFactory;
use Elixir\HTTP\ResponseInterface;
use Elixir\Kernel\ApplicationEvent;
use Elixir\Kernel\ApplicationInterface;
use Elixir\Kernel\HTTPKernelInterface;
use Elixir\Kernel\Middleware\MiddlewareInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class ErrorMiddleware implements MiddlewareInterface, ContainerAwareInterface
{
    /**
     * @var string|array|callable
     */
    protected $failbackErrorController;
    
    /**
     * @var ContainerInterface
     */
    protected $container;
    
    /**
     * @var HTTPKernelInterface
     */
    protected $kernel;
    
    /**
     * @param string|array|callable $failbackErrorController
     */
    public function __construct($failbackErrorController = null) 
    {
        $this->failbackErrorController = null;
    }

    /**
     * {@inheritdoc}
     */
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
        $this->kernel = $container->get('kernel');
    }
    
    /**
     * {@inheritdoc}
     */
    public function __invoke($request, $response, callable $next) 
    {
        try
        {
            $response = $next($request, $response);
        }
        catch(\Exception $e)
        {
            switch($e->getCode())
            {
                case 403:
                case 404:
                case 500:
                    $statusCode = $e->getCode();
                    $message = $e->getMessage();
                    break;
                default:
                    $statusCode = 500;
                    $message = 'Internal Server Error';
            }
            
            if ($this->kernel instanceof ApplicationInterface)
            {
                $event = new ApplicationEvent(ApplicationEvent::EXCEPTION, ['request' => $request, 'response' => $response, 'exception' => $e]);
                $this->kernel->dispatch($event);
                
                $request = $event->getRequest();
                $response = $event->getResponse();
                
                if ($response instanceof ResponseInterface)
                {
                    $response = $response->withStatus($statusCode);
                    return $response;
                }
            }
            
            if ($request->isMainRequest())
            {
                $controller = $this->failbackErrorController;
                
                if ($request->hasAttribute('_module'))
                {
                    $controller = 'error';
                }
                
                if (empty($controller))
                {
{'fraction_non_alphanumeric': 0.11291486291486291, 'fraction_numerical': 0.004329004329004329, 'mean_word_length': 1.544745296007343, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 47, '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': '6520907', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1352, 'n_words': 254}