<commit_before><?php

/**
 * Pony.fm - A community for pony fan music.
 * Copyright (C) 2015 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Poniverse\Ponyfm\Http\Middleware;

use App;
use Closure;
use Cache;
use Config;
use DB;
use Log;
use Poniverse\Ponyfm\ProfileRequest;
use Symfony\Component\HttpFoundation\Response;

class Profiler
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Profiling magic time!
        if (Config::get('app.debug')) {
            DB::enableQueryLog();
            $profiler = ProfileRequest::create();

            try {
                $response = $next($request);

            } catch (\Exception $e) {
                $response = \Response::make(['exception' => $e->getTrace()], 500);
                $profiler->log('error', $e->__toString(), []);
            }

            $response = $this->processResponse($profiler, $response);

            Log::listen(function ($level, $message, $context) use ($profiler, $request) {
                $profiler->log($level, $message, $context);
            });

        } else {
            // Process the request the usual, boring way.
            $response = $next($request);
        }
<commit_msg>Return error message in profile exception handler response<commit_after><?php

/**
 * Pony.fm - A community for pony fan music.
 * Copyright (C) 2015 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Poniverse\Ponyfm\Http\Middleware;

use App;
use Closure;
use Cache;
use Config;
use DB;
use Log;
use Poniverse\Ponyfm\ProfileRequest;
use Symfony\Component\HttpFoundation\Response;

class Profiler
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Profiling magic time!
        if (Config::get('app.debug')) {
            DB::enableQueryLog();
            $profiler = ProfileRequest::create();

            try {
                $response = $next($request);
            } catch (\Exception $e) {
                $response = \Response::make([
                    'message' => $e->getMessage(),
                    'exception' => $e->getTrace()
                ], 500);
                $profiler->log('error', $e->__toString(), []);
            }

            $response = $this->processResponse($profiler, $response);

            Log::listen(function ($level, $message, $context) use ($profiler, $request) {
                $profiler->log($level, $message, $context);
            });

        } else {
            // Process the request the usual, boring way.
            $response = $next($request);
        }

{'fraction_non_alphanumeric': 0.11425804889857177, 'fraction_numerical': 0.0038731541999515854, 'mean_word_length': 2.3952341824157766, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2864185', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1106, 'n_words': 398}