<commit_before><?php
/**
 * Slim Framework (http://slimframework.com)
 *
 * @link      https://github.com/slimphp/Slim
 * @copyright Copyright (c) 2011-2016 <NAME>
 * @license   https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
 */
namespace Slim\Handlers;

use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Http\Body;

/**
 * Default Slim application error handler
 *
 * It outputs the error message and diagnostic information in either JSON, XML,
 * or HTML based on the Accept header.
 */
class Error
{
    protected $displayErrorDetails;

    /**
     * Known handled content types
     *
     * @var array
     */
    protected $knownContentTypes = [
        'application/json',
        'application/xml',
        'text/xml',
        'text/html',
    ];

    /**
     * Constructor
     *
     * @param boolean $displayErrorDetails Set to true to display full details
     */
    public function __construct($displayErrorDetails = false)
    {
        $this->displayErrorDetails = (bool)$displayErrorDetails;
    }

    /**
     * Invoke error handler
     *
     * @param ServerRequestInterface $request   The most recent Request object
     * @param ResponseInterface      $response  The most recent Response object
     * @param Exception              $exception The caught Exception object
     *
     * @return ResponseInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
    {
        $contentType = $this->determineContentType($request);
        switch ($contentType) {
            case 'application/json':
                $output = $this->renderJsonErrorMessage($exception);
                break;

            case 'text/xml':
            case 'application/xml':
                $output = $this->renderXmlErrorMessage($exception);
                break;

            case 'text/html':
                $output = $this->renderHtmlErrorMessage($exception);
                break;
        }

        $this->writeToErrorLog($exception);

        $body = new Body(fopen('php://temp', 'r+'));
        $body->write($output);

        return $response
                ->withStatus(500)
                ->withHeader('Content-type', $contentType)
                ->withBody($body);
    }


    /**
     * Write to the error log if displayErrorDetails is false
     *
     * @param Exception $exception
     * @return void
     */
    protected function writeToErrorLog($exception)
    {
        if ($this->displayErrorDetails) {
            return;
        }

        $message = 'Slim Application Error:' . PHP_EOL;
        $message .= $this->renderTextException($exception);
        while ($exception = $exception->getPrevious()) {
            $message .= PHP_EOL . 'Previous exception:' . PHP_EOL;
            $message .= $this->renderTextException($exception);
        }

        $message .= 'View in rendered output by enabling the "displayErrrorDetails" setting.' . PHP_EOL;

        error_log($message);
    }

    /**
     * Render exception as Text.
     *
     * @param Exception $exception
     *
     * @return string
     */
    protected function renderTextException(Exception $exception)
    {
        $text = sprintf('Type: %s' . PHP_EOL, get_class($exception));

        if (($code = $exception->getCode())) {
            $text .= sprintf('Code: %s' . PHP_EOL, $code);<commit_msg>fix typo in error handler.<commit_after><?php
/**
 * Slim Framework (http://slimframework.com)
 *
 * @link      https://github.com/slimphp/Slim
 * @copyright Copyright (c) 2011-2016 <NAME>
 * @license   https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
 */
namespace Slim\Handlers;

use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Http\Body;

/**
 * Default Slim application error handler
 *
 * It outputs the error message and diagnostic information in either JSON, XML,
 * or HTML based on the Accept header.
 */
class Error
{
    protected $displayErrorDetails;

    /**
     * Known handled content types
     *
     * @var array
     */
    protected $knownContentTypes = [
        'application/json',
        'application/xml',
        'text/xml',
        'text/html',
    ];

    /**
     * Constructor
     *
     * @param boolean $displayErrorDetails Set to true to display full details
     */
    public function __construct($displayErrorDetails = false)
    {
        $this->displayErrorDetails = (bool)$displayErrorDetails;
    }

    /**
     * Invoke error handler
     *
     * @param ServerRequestInterface $request   The most recent Request object
     * @param ResponseInterface      $response  The most recent Response object
     * @param Exception              $exception The caught Exception object
     *
     * @return ResponseInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
    {
        $contentType = $this->determineContentType($request);
        switch ($contentType) {
            case 'application/json':
                $output = $this->renderJsonErrorMessage($exception);
                break;

            case 'text/xml':
            case 'application/xml':
                $output = $this->renderXmlErrorMessage($exception);
                break;

            case 'text/html':
                $output = $this->renderHtmlErrorMessage($exception);
                break;
        }

        $this->writeToErrorLog($exception);

        $body = new Body(fopen('php://temp', 'r+'));
        $body->write($output);

        return $response
                ->withStatus(500)
                ->withHeader('Content-type', $contentType)
                ->withBody($body);
    }


    /**
     * Write to the error log if displayErrorDetails is false
     *
     * @param Exception $exception
     * @return void
     */
    protected function writeToErrorLog($exception)
    {
        if ($this->displayErrorDetails) {
            return;
        }

        $message = 'Slim Application Error:' . PHP_EOL;
        $message .= $this->renderTextException($exception);
        while ($exception = $exception->getPrevious()) {
            $message .= PHP_EOL . 'Previous exception:' . PHP_EOL;
            $message .= $this->renderTextException($exception);
        }

        $message .= 'View in rendered output by enabling the "displayErrorDetails" setting.' . PHP_EOL;

        error_log($message);
    }

    /**
     * Render exception as Text.
     *
     * @param Exception $exception
     *
     * @return string
     */
    protected function renderTextException(Exception $exception)
    {
        $text = sprintf('Type: %s' . PHP_EOL, get_class($exception));

        if (($code = $exception->getCode())) {
            $text .= sprintf('Code: %s' . PHP_EOL, $code);
{'fraction_non_alphanumeric': 0.11928860613071139, 'fraction_numerical': 0.003470213996529786, 'mean_word_length': 2.208256029684601, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '6114295', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1824, 'n_words': 455}