<commit_before><?php
use \Entity\Block;

class ErrorController extends \DF\Controller\Action
{
    public function errorAction()
    {
        // Grab the error object from the request
        $errors = $this->_getParam('error_handler');
        
        // 404 error -- controller or action not found
        if (in_array($errors->type, array(\Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER, \Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION)))
        {
            $uri = $errors->request->getRequestUri();
            $uri = trim($uri, '/');

            $block = Block::getRepository()->findOneBy(array('url' => $uri));
            if ($block instanceof Block)
            {
                $id = $block->id;
                $this->_forward('index', 'page', 'default', array('id' => $block->id));
                return;
            }
            else
            {
                $this->_helper->viewRenderer('error/pagenotfound', null, true);
            
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->message = 'Page not found';
            }
        }
        else if ($errors->exception instanceof \DF\Exception\DisplayOnly)
        {
            $this->_helper->viewRenderer('error/displayonly', NULL, TRUE);
        }
        else if ($errors->exception instanceof \DF\Exception\NotLoggedIn)
        {
            // $this->_helper->viewRenderer('error/notloggedin', NULL, TRUE);
            // $this->view->message = 'Login Required to Access This Page';

            $this->redirectToRoute(array('module' => 'default', 'controller' => 'account', 'action' => 'login'));<commit_msg>Add special handling inside 404 code to return JSON-encoded error for speed and computer-readability.<commit_after><?php
use \Entity\Block;

class ErrorController extends \DF\Controller\Action
{
    public function errorAction()
    {
        // Grab the error object from the request
        $errors = $this->_getParam('error_handler');
        
        // 404 error -- controller or action not found
        if (in_array($errors->type, array(\Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER, \Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION)))
        {
            $uri = $errors->request->getRequestUri();
            $uri = trim($uri, '/');

            $block = Block::getRepository()->findOneBy(array('url' => $uri));
            if ($block instanceof Block)
            {
                $id = $block->id;
                $this->_forward('index', 'page', 'default', array('id' => $block->id));
                return;
            }
            elseif (substr($uri, 0, 3) == 'api')
            {
                // Return a JSON-encoded error for
                $this->doNotRender();

                header('Content-Type: application/json; charset=utf-8');
                echo json_encode(array(
                    'status'    => 'error',
                    'error'     => 'API function not found.',
                ), JSON_UNESCAPED_SLASHES);
                return;
            }
            else
            {
                $this->_helper->viewRenderer('error/pagenotfound', null, true);
            
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->message = 'Page not found';
            }
        }
        else if ($errors->exception instanceof \DF\Exception\DisplayOnly)
        {
            $this->_helper->viewRenderer('error/displayonly', NULL, TRUE);
        }
        else if ($errors->exception instanceof \DF\Exception\NotLoggedIn)
        {
            // $this->_helper->viewRenderer('error/notloggedin', NULL, TRUE);
            // $this->view->message = 'Login Required to Access This Page';

            $this->redirectToRoute(array('module' => 'default', 'controller' => 'account', 'action' => 'login'));
{'fraction_non_alphanumeric': 0.14108527131782947, 'fraction_numerical': 0.004651162790697674, 'mean_word_length': 1.9482102056359483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 62, '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': '6433955', 'n_tokens_mistral': 1039, 'n_tokens_neox': 995, 'n_words': 211}