<commit_before><?php namespace Illuminate\Exception;

use Closure;
use ReflectionFunction;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class Handler {

	/**
	 * All of the register exception handlers.
	 *
	 * @var array
	 */
	protected $handlers = array();

	/**
	 * Handle a console exception.
	 *
	 * @param  Exception  $exception
	 * @return void
	 */
	public function handleConsole($exception)
	{
		return $this->handle($exception, true);
	}

	/**
	 * Handle the given exception.
	 *
	 * @param  Exception  $exception
	 * @param  bool  $fromConsole
	 * @return void
	 */
	public function handle($exception, $fromConsole = false)
	{
		foreach ($this->handlers as $handler)
		{
			// If this exception handler does not handle the given exception, we will
			// just go the next one. A Handler may type-hint the exception that it
			// will handle, allowing for more granularity on the error handling.
			if ( ! $this->handlesException($handler, $exception))
			{
				continue;
			}

			if ($exception instanceof HttpExceptionInterface)
			{
				$code = $exception->getStatusCode();
			}

			// If the exception doesn't implement the HttpExceptionInterface we will
			// just use the generic 500 error code for a server side error. If it
			// implements the Http interface we'll grab the error code from it.
			else
			{
				$code = 500;
			}

			$response = $handler($exception, $code, $fromConsole);

			// If the handler returns a "non-null" response, we will return it so it
			// will get sent back to the browsers. Once a handler returns a valid
			// response we will cease iterating and calling the other handlers.
			if ( ! is_null($response))
			{
				return $response;
			}
		}
	}

	/**
	 * Determine if the given handler handles this exception.
	 *
	 * @param  Closure    $handler
	 * @param  Exception  $exception
	 * @return bool
	 */
	protected function handlesException(Closure $handler, $exception)
	{
		$reflection = new ReflectionFunction($handler);

		return $reflection->getNumberOfParameters() == 0 or $this->hints($reflection, $exception);
	}

	/**
	 * Determine if the given handler type hints the exception.
	 *
	 * @param  ReflectionFunction  $reflection
	 * @param  Exception  $exception
	 * @return bool
	 */
	protected function hints(ReflectionFunction $reflection, $exception)
	{
		$parameters = $reflection->getParameters();

		$expected = $parameters[0];

		return ! $expected->getClass() or $expected->getClass()->isInstance($exception);
	}

	/**
	 * Register an application error handler.
	 *
	 * @param  Closure  $callback
	 * @return void
	 */
	public function error(Closure $callback)
	{
		$this->handlers[] = $callback;
	}

}<commit_msg>prepend handlers instead of appending.<commit_after><?php namespace Illuminate\Exception;

use Closure;
use ReflectionFunction;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class Handler {

	/**
	 * All of the register exception handlers.
	 *
	 * @var array
	 */
	protected $handlers = array();

	/**
	 * Handle a console exception.
	 *
	 * @param  Exception  $exception
	 * @return void
	 */
	public function handleConsole($exception)
	{
		return $this->handle($exception, true);
	}

	/**
	 * Handle the given exception.
	 *
	 * @param  Exception  $exception
	 * @param  bool  $fromConsole
	 * @return void
	 */
	public function handle($exception, $fromConsole = false)
	{
		foreach ($this->handlers as $handler)
		{
			// If this exception handler does not handle the given exception, we will
			// just go the next one. A Handler may type-hint the exception that it
			// will handle, allowing for more granularity on the error handling.
			if ( ! $this->handlesException($handler, $exception))
			{
				continue;
			}

			if ($exception instanceof HttpExceptionInterface)
			{
				$code = $exception->getStatusCode();
			}

			// If the exception doesn't implement the HttpExceptionInterface we will
			// just use the generic 500 error code for a server side error. If it
			// implements the Http interface we'll grab the error code from it.
			else
			{
				$code = 500;
			}

			$response = $handler($exception, $code, $fromConsole);

			// If the handler returns a "non-null" response, we will return it so it
			// will get sent back to the browsers. Once a handler returns a valid
			// response we will cease iterating and calling the other handlers.
			if ( ! is_null($response))
			{
				return $response;
			}
		}
	}

	/**
	 * Determine if the given handler handles this exception.
	 *
	 * @param  Closure    $handler
	 * @param  Exception  $exception
	 * @return bool
	 */
	protected function handlesException(Closure $handler, $exception)
	{
		$reflection = new ReflectionFunction($handler);

		return $reflection->getNumberOfParameters() == 0 or $this->hints($reflection, $exception);
	}

	/**
	 * Determine if the given handler type hints the exception.
	 *
	 * @param  ReflectionFunction  $reflection
	 * @param  Exception  $exception
	 * @return bool
	 */
	protected function hints(ReflectionFunction $reflection, $exception)
	{
		$parameters = $reflection->getParameters();

		$expected = $parameters[0];

		return ! $expected->getClass() or $expected->getClass()->isInstance($exception);
	}

	/**
	 * Register an application error handler.
	 *
	 * @param  Closure  $callback
	 * @return void
	 */
	public function error(Closure $callback)
	{
		array_unshift($this->handlers, $callback);
	}

}
{'fraction_non_alphanumeric': 0.11049825335539622, 'fraction_numerical': 0.0029417172274315132, 'mean_word_length': 3.3346613545816735, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '2960575', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1506, 'n_words': 524}