<commit_before><?php

/**
 * This file is part of tenside/core.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * This project is provided in good faith and hope to be usable by anyone.
 *
 * @package    tenside/core
 * @author     <NAME> <<EMAIL>>
 * @copyright  2015 <NAME> <<EMAIL>>
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
 * @link       https://github.com/tenside/core
 * @filesource
 */

namespace Tenside\CoreBundle\EventListener;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
 * This class converts exceptions into proper responses.
 */
class ExceptionListener
{
    /**
     * Maps known exceptions to HTTP exceptions.
     *
     * @param GetResponseForExceptionEvent $event The event object.
     *
     * @return void
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        $class     = get_class($exception);
        $response  = null;
        switch ($class) {
            case 'Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException':
                $response = $this->createNotFoundResponse($event->getRequest());
                break;
            case 'Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\UnauthorizedHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\InternalServerErrorHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\BadRequestHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\ServiceUnavailableHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\HttpException':
                /** @var HttpException $exception */
                $response = $this->createHttpExceptionResponse($exception);
                break;
            default:
                return;
        }

        if (null === $response) {
            $response = $this->createInternalServerError();
        }

        $event->setResponse($response);
    }

    /**
     * Create a 404 response.
     *
     * @param Request $request The http request.
     *
     * @return Response
     *
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
     */
    private function createNotFoundResponse($request)
    {
        return new Response(
            Response::$statusTexts[Response::HTTP_NOT_FOUND] . $request->getRequestUri(),
            Response::HTTP_NOT_FOUND
        );
    }

    /**
     * Create a 401 response.
     *
     * @param HttpException $exception The exception to create a response for.
     *
     * @return Response
     */
    private function createHttpExceptionResponse(HttpException $exception)
    {
        return new Response(
            Response::$statusTexts[$exception->getStatusCode()],
            $exception->getStatusCode(),
            $exception->getHeaders()
        );
    }

    /**
     * Create a 500 response.
     *
     * @return Response
     */
    private function createInternalServerError()
    {
        return new Response(
            Response::$statusTexts[Response::HTTP_INTERNAL_SERVER_ERROR],
            Response::HTTP_INTERNAL_SERVER_ERROR
        );
    }
}<commit_msg>Change exception rendering to json<commit_after><?php

/**
 * This file is part of tenside/core.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * This project is provided in good faith and hope to be usable by anyone.
 *
 * @package    tenside/core
 * @author     <NAME> <<EMAIL>>
 * @copyright  2015 <NAME> <<EMAIL>>
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
 * @link       https://github.com/tenside/core
 * @filesource
 */

namespace Tenside\CoreBundle\EventListener;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
 * This class converts exceptions into proper responses.
 */
class ExceptionListener
{
    /**
     * Maps known exceptions to HTTP exceptions.
     *
     * @param GetResponseForExceptionEvent $event The event object.
     *
     * @return void
     *
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        $class     = get_class($exception);
        $response  = null;
        switch ($class) {
            case 'Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException':
                $response = $this->createNotFoundResponse($event->getRequest());
                break;
            case 'Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\UnauthorizedHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\InternalServerErrorHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\BadRequestHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\ServiceUnavailableHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\NotAcceptableHttpException':
            case 'Symfony\\Component\\HttpKernel\\Exception\\HttpException':
                /** @var HttpException $exception */
                $response = $this->createHttpExceptionResponse($exception);
                break;
            default:
        }

        if (null === $response) {
            $response = $this->createInternalServerError();
        }

        $event->setResponse($response);
    }

    /**
     * Create a 404 response.
     *
     * @param Request $request The http request.
     *
     * @return JsonResponse
     *
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
     */
    private function createNotFoundResponse($request)
    {
        return new JsonResponse(
            [
                'status'  => 'ERROR',
                'message' => 'Uri ' . $request->getRequestUri() . ' could not be found'
            ],
            JsonResponse::HTTP_NOT_FOUND
        );
    }

    /**
     * Create a http response.
     *
     * @param HttpException $exception The exception to create a response for.
     *
     * @return JsonResponse
     */
    private function createHttpExceptionResponse(HttpException $exception)
    {
        return new JsonResponse(
            [
                'status'  => 'ERROR',
                'message' => $exception->getMessage()
            ],
            $exception->getStatusCode(),
            $exception->getHeaders()
        );
    }

    /**
     * Create a 500 response.
     *
     * @return JsonResponse
     */
    private function createInternalServerError()
    {
        return new JsonResponse(
            [
                'status'  => 'ERROR',
                'message' => JsonResponse::$statusTexts[JsonResponse::HTTP_INTERNAL_SERVER_ERROR]
            ],
            JsonResponse::HTTP_INTERNAL_SERVER_ERROR
        );
    }
}
{'fraction_non_alphanumeric': 0.11182450508293205, 'fraction_numerical': 0.0030765115034777956, 'mean_word_length': 2.3349687778768957, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 47, 'https://': 4, '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': '7144448', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1895, 'n_words': 411}