src/Dispatch/Dispatcher.php
protected function prepareErrorResponse(HTTPError $error)
    {
        $writers = WriterFactory::getAvailableWriters();
        $mime_types = array_keys($writers);

        array_unshift($mime_types, 'text/plain');
        array_unshift($mime_types, 'text/html');
        $preferred_type = $this->request->accept->getBestResponseType($mime_types);

        if ($preferred_type === 'text/html')
        {
            $ex = $error->getPrevious() ?: $error;
            $template = $this->getTemplate();
            $template->assign('exception', $ex);

            $template->setExceptionTemplate($ex);
            
            $response = $template->renderReturn();

            if ($response instanceof HTTPError)
                $error->setResponse(new StringResponse(WF::str($response), "text/plain"));
            else
                $error->setResponse($response);
        }
        elseif ($preferred_type !== 'text/plain')
        {
            $ex = $error->getPrevious() ?: $error;
            $status = $error->getStatusCode();
            $status_msg = isset(StatusCode::$CODES[$status]) ? StatusCode::$CODES[$status] : "Internal Server Error";
            $exception_str = WF::str($ex);
            $exception_list = explode("\n", $exception_str);
            $data = array(
                'message' => $ex->getMessage(),
                'status_code' => $status,
                'exception' => $exception_list,
                'status' => $status_msg
            );

            $dict = new Dictionary($data);
            $error->setResponse(new DataResponse($dict));
        }
        // text/plain is handled by Error directly
    }
protected function prepareErrorResponse(HTTPError $error)
    {
        $writers = WriterFactory::getAvailableWriters();
        $mime_types = array_keys($writers);

        array_unshift($mime_types, 'text/plain');
        array_unshift($mime_types, 'text/html');
        $preferred_type = $this->request->accept->getBestResponseType($mime_types);

        if ($preferred_type === 'text/html')
        {
            $ex = $error->getPrevious() ?: $error;
            $template = $this->getTemplate();
            $template->assign('exception', $ex);

            $template->setExceptionTemplate($ex);
            
            $response = $template->renderReturn();

            if ($response instanceof HTTPError)
                $error->setResponse(new StringResponse(WF::str($response), "text/plain"));
            else
                $error->setResponse($response);
        }
        elseif ($preferred_type !== 'text/plain')
        {
            $ex = $error->getPrevious() ?: $error;
            $status = $error->getStatusCode();
            $status_msg = isset(StatusCode::$CODES[$status]) ? StatusCode::$CODES[$status] : "Internal Server Error";
            $exception_str = WF::str($ex);
            $exception_list = explode("\n", $exception_str);
            $data = array(
                'message' => $ex->getMessage(),
                'status_code' => $status,
                'exception' => $exception_list,
                'status' => $status_msg
            );

            $dict = new Dictionary($data);
            $error->setResponse(new DataResponse($dict));
        }
        // text/plain is handled by Error directly
    }
Prepare error response: to give a better error output,
a template must be executed or it must be wrapped in a
DataResponse to that it will be JSON or XML-formatted.

@param HTTPError $error The error to prepare