<commit_before><?php
/**
 * @package Habari
 *
 */

/**
 * Contains error-related functions and Habari's error handler.
 *
 **/
class Error extends Exception
{
	protected $message = '';
	protected $is_error = false;

	/**
	 * Constructor for the Error class
	 *
	 * @param string $message Exception to display
	 * @param integer $code Code of the exception
	 * @param boolean $is_error true if the exception represents an error handled by the Error class
	 */
	public function __construct($message = 'Generic Habari Error', $code = 0, $is_error = false)
	{
		parent::__construct($message, $code);
		$this->is_error = $is_error;
	}

	/**
	 * function handle_errors
	 *
	 * Configures the Error class to handle all errors.
	 */
	public static function handle_errors()
	{
		set_error_handler( array( 'Error', 'error_handler' ) );
		set_exception_handler( array( 'Error', 'exception_handler' ) );
	}

	/**
	 * Used to handle all uncaught exceptions.
	 */
	public static function exception_handler( $exception )
	{
		if(isset($exception->is_error) && $exception->is_error) {
			return;
		}
		printf(
			"<pre class=\"error\">\n<b>%s:</b> %s in %s line %s\n</pre>",
			get_class( $exception ),
			$exception->getMessage(),
			$exception->file,
			$exception->line
		);

		if ( DEBUG ) {
			self::print_backtrace( $exception->getTrace() );
		}

		if ( Options::get( 'log_backtraces' ) || DEBUG ) {
			$backtrace = print_r( $exception->getTrace(), true );
		}
		else {
			$backtrace = null;
		}

		EventLog::log( $exception->getMessage() . ' in ' . $exception->file . ':' . $exception->line, 'err', 'default', null, $backtrace );
	}

	/**
	 * Get the error text, file, and line number from the backtrace, which is more accurate
	 *
	 * @return string The contructed error string
	 */
	public function humane_error()
	{
		$trace = $this->getTrace();
		$trace1 = reset($trace);

		$file = isset( $trace1['file'] ) ? $trace1['file'] : $this->getFile();
		$line = isset( $trace1['line'] ) ? $trace1['line'] : $this->getLine();

		return sprintf(_t('%1$s in %2$s line %3$s on request of "%4$s"'), $this->getMessage(), $file, $line, $_SERVER['REQUEST_URI']);
	}

	/**
	 * Used to handle all PHP errors after Error::handle_errors() is called.
	 */
	public static function error_handler( $errno, $errstr, $errfile, $errline, $errcontext )
	{
		if ( ( $errno & error_reporting() ) === 0 ) {
			return;
		}
		
		// Don't be fooled, we can't actually handle most of these.
		$error_names = array(
			E_ERROR => _t( 'Error' ),
			E_WARNING => _t( 'Warning' ),
			E_PARSE => _t( 'Parse Error' ),
			E_NOTICE => _t( 'Notice' ),
			E_CORE_ERROR => _t( 'Core Error' ),
			E_CORE_WARNING => _t( 'Core Warning' ),
			E_COMPILE_ERROR => _t( 'Compile Error' ),
			E_COMPILE_WARNING => _t( 'Compile Warning' ),
			E_USER_ERROR => _t( 'User Error' ),
			E_USER_WARNING => _t( 'User Warning' ),
			E_USER_NOTICE => _t( 'User Notice' ),
			E_STRICT => _t( 'Strict Notice' ),
			E_RECOVERABLE_ERROR => _t( 'Recoverable Error' ),
			E_DEPRECATED => _t( 'Deprecated violation' ),
			E_USER_DEPRECATED => _t( 'User deprecated violation' ),
		);

		if ( strpos( $errfile, HABARI_PATH ) === 0 ) {
			$errfile = substr( $errfile, strlen( HABARI_PATH ) + 1 );
		}

		if ( ini_get('display_errors') || DEBUG ) {
			printf(
				"<pre class=\"error\">\n<b>%s:</b> %s in %s line %s\n</pre>",
				$error_names[$errno],
				$errstr,
				$errfile,
				$errline
			);<commit_msg>Let's try not causing cascading errors when an error is hit.<commit_after><?php
/**
 * @package Habari
 *
 */

/**
 * Contains error-related functions and Habari's error handler.
 *
 **/
class Error extends Exception
{
	protected $message = '';
	protected $is_error = false;

	/**
	 * Constructor for the Error class
	 *
	 * @param string $message Exception to display
	 * @param integer $code Code of the exception
	 * @param boolean $is_error true if the exception represents an error handled by the Error class
	 */
	public function __construct($message = 'Generic Habari Error', $code = 0, $is_error = false)
	{
		parent::__construct($message, $code);
		$this->is_error = $is_error;
	}

	/**
	 * function handle_errors
	 *
	 * Configures the Error class to handle all errors.
	 */
	public static function handle_errors()
	{
		set_error_handler( array( 'Error', 'error_handler' ) );
		set_exception_handler( array( 'Error', 'exception_handler' ) );
	}

	/**
	 * Used to handle all uncaught exceptions.
	 */
	public static function exception_handler( $exception )
	{
		if(isset($exception->is_error) && $exception->is_error) {
			return;
		}
		printf(
			"<pre class=\"error\">\n<b>%s:</b> %s in %s line %s\n</pre>",
			get_class( $exception ),
			$exception->getMessage(),
			$exception->file,
			$exception->line
		);

		if ( DEBUG ) {
			self::print_backtrace( $exception->getTrace() );
		}

		if ( Options::get( 'log_backtraces' ) || DEBUG ) {
			$backtrace = print_r( $exception->getTrace(), true );
		}
		else {
			$backtrace = null;
		}

		EventLog::log( $exception->getMessage() . ' in ' . $exception->file . ':' . $exception->line, 'err', 'default', null, $backtrace );
	}

	/**
	 * Get the error text, file, and line number from the backtrace, which is more accurate
	 *
	 * @return string The contructed error string
	 */
	public function humane_error()
	{
		$trace = $this->getTrace();
		$trace1 = reset($trace);

		$file = isset( $trace1['file'] ) ? $trace1['file'] : $this->getFile();
		$line = isset( $trace1['line'] ) ? $trace1['line'] : $this->getLine();

		return sprintf(_t('%1$s in %2$s line %3$s on request of "%4$s"'), $this->getMessage(), $file, $line, $_SERVER['REQUEST_URI']);
	}

	/**
	 * Used to handle all PHP errors after Error::handle_errors() is called.
	 */
	public static function error_handler( $errno, $errstr, $errfile, $errline, $errcontext )
	{
		if ( ( $errno & error_reporting() ) === 0 ) {
			return;
		}
		
		// Don't be fooled, we can't actually handle most of these.
		$error_names = array(
			E_ERROR => _t( 'Error' ),
			E_WARNING => _t( 'Warning' ),
			E_PARSE => _t( 'Parse Error' ),
			E_NOTICE => _t( 'Notice' ),
			E_CORE_ERROR => _t( 'Core Error' ),
			E_CORE_WARNING => _t( 'Core Warning' ),
			E_COMPILE_ERROR => _t( 'Compile Error' ),
			E_COMPILE_WARNING => _t( 'Compile Warning' ),
			E_USER_ERROR => _t( 'User Error' ),
			E_USER_WARNING => _t( 'User Warning' ),
			E_USER_NOTICE => _t( 'User Notice' ),
			E_STRICT => _t( 'Strict Notice' ),
			E_RECOVERABLE_ERROR => _t( 'Recoverable Error' ),
		);
		if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) {
			$error_names[E_DEPRECATED] = _t( 'Deprecated violation' );
			$error_names[E_USER_DEPRECATED] = _t( 'User deprecated violation' );
		}
		if ( strpos( $errfile, HABARI_PATH ) === 0 ) {
			$errfile = substr( $errfile, strlen( HABARI_PATH ) + 1 );
		}

		if ( ini_get('display_errors') || DEBUG ) {
			printf(
				"<pre class=\"error\">\n<b>%s:</b> %s in %s line %s\n</pre>",
				$error_names[$errno],
				$errstr,
				$errfile,
				$errline
			);
{'fraction_non_alphanumeric': 0.17069335239456754, 'fraction_numerical': 0.004145818441744103, 'mean_word_length': 3.5755395683453237, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 78, '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': '1532431', 'n_tokens_mistral': 2706, 'n_tokens_neox': 2428, 'n_words': 636}