<commit_before><?php
class Garp_ErrorHandler {
	const ERROR_REPORT_MAIL_ADDRESS_FALLBACK = '<EMAIL>';

	/**
 	 * Handles premature exceptions thrown before the MVC ErrorHandler is initialized.
 	 * Exceptions of that kind will result in a blank page if displayErrors is off, instead of
 	 * redirected to an error page (which would be the case for exceptions thrown by a controller,
 	 * for instance).
 	 */
	public static function handlePrematureException(Exception $e) {
		$request = Zend_Controller_Front::getInstance()->getRequest();
		$request->setModuleName('default');
		$request->setControllerName('error');
		$request->setActionName('error');

		$error = new Zend_Controller_Plugin_ErrorHandler();
		$error->type = Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER;
		$error->request = clone $request;
		$error->exception = $e;
		$request->setParam('error_handler', $error);
	}

	/**
 	 * Log that pesky error to a file
 	 * @param String $message
 	 * @return Void
 	 */
	public static function logErrorToFile(ArrayObject $errors) {
		$errorMessage = self::_composeFullErrorMessage($errors);

		$stream = fopen(APPLICATION_PATH.'/data/logs/errors.log', 'a');
		$writer = new Zend_Log_Writer_Stream($stream);
		$logger = new Zend_Log($writer);
		$logger->log($errorMessage, Zend_Log::ALERT);
	}

	/**
 	 * Log error to Slack.
 	 * Parameter is expected to be an ArrayObject as formatted by
 	 * Zend_Controller_Plugin_ErrorHandler::_handleError
 	 */
	public static function logErrorToSlack(ArrayObject $errors) {
		try {
			$slack = new Garp_Service_Slack();
		} catch (Exception $e) {
			return false;
		}

		$shortErrorMessage = self::_composeShortErrorMessage($errors);

		//	Add first occurrence and StackTrace as attachments
		$trace = self::_filterBasePath(
			str_replace('->', '::', $errors->exception->getTraceAsString())
		);
		$params['attachments'] = array(
			array(
				'title' => self::_getExceptionClass($errors),
				'text' => $slack->wrapCodeMarkup(
					$errors->exception->getMessage()
					. "\n"
					. self::_filterBasePath($errors->exception->getFile())
					. ': '
					. $errors->exception->getLine()
				),
				'color' => '#bb5555',
				'mrkdwn_in' => array('text'),
				'short' => true
			),
			array(
				'title' => 'StackTrace',
				'text' => $slack->wrapCodeMarkup($trace),
				'color' => '#6666ee',
				'mrkdwn_in' => array('text'),
				'short' => true
			)
		);

		$slack->postMessage($shortErrorMessage, $params);

		return true;
	}

	/**
 	 * Mail an error to an admin
 	 * @return Void
 	 */
	public static function mailErrorToAdmin(ArrayObject $errors) {
		$errorMessage = self::_composeFullErrorMessage($errors);
		$subjectPrefix = '';
		if (isset($_SERVER) && !empty($_SERVER['HTTP_HOST'])) {
			$subjectPrefix = '['.$_SERVER['HTTP_HOST'].'] ';
		}

		$ini = Zend_Registry::get('config');
		$to = (
			isset($ini->app) &&
			isset($ini->app->errorReportEmailAddress) &&
			$ini->app->errorReportEmailAddress
		)
			? $ini->app->errorReportEmailAddress
			: self::ERROR_REPORT_MAIL_ADDRESS_FALLBACK
		;

		$mailer = new Garp_Mailer();
		return $mailer->send(array(
			'to' => $to,
			'subject' => $subjectPrefix . 'An application error occurred'
			'message' => $errorMessage
		));
	}

	protected static function _composeShortErrorMessage(ArrayObject $errors) {
		$appName = self::_getApplicationName();

		$exceptionType = self::_getExceptionClass($errors);
		$article = self::_startsWithVowel($exceptionType) ? 'an' : 'a';
		$message = "Found {$article} `{$exceptionType}` in project `"
			. $appName . "` :neutral_face:\n";

		// Add user information
		$auth = Garp_Auth::getInstance();
		if ($auth->isLoggedIn()) {
			$userData = $auth->getUserData();
			$message .= 'Caused by '
				. $userData['role'] . ' '
				. $userData['name']
				. ' (' . $userData['email'] . ").\n"
			;
		}

		// Add environment and IP
		$message .= 'On `' . APPLICATION_ENV . '`';<commit_msg>fixed syntax error<commit_after><?php
class Garp_ErrorHandler {
	const ERROR_REPORT_MAIL_ADDRESS_FALLBACK = '<EMAIL>';

	/**
 	 * Handles premature exceptions thrown before the MVC ErrorHandler is initialized.
 	 * Exceptions of that kind will result in a blank page if displayErrors is off, instead of
 	 * redirected to an error page (which would be the case for exceptions thrown by a controller,
 	 * for instance).
 	 */
	public static function handlePrematureException(Exception $e) {
		$request = Zend_Controller_Front::getInstance()->getRequest();
		$request->setModuleName('default');
		$request->setControllerName('error');
		$request->setActionName('error');

		$error = new Zend_Controller_Plugin_ErrorHandler();
		$error->type = Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER;
		$error->request = clone $request;
		$error->exception = $e;
		$request->setParam('error_handler', $error);
	}

	/**
 	 * Log that pesky error to a file
 	 * @param String $message
 	 * @return Void
 	 */
	public static function logErrorToFile(ArrayObject $errors) {
		$errorMessage = self::_composeFullErrorMessage($errors);

		$stream = fopen(APPLICATION_PATH.'/data/logs/errors.log', 'a');
		$writer = new Zend_Log_Writer_Stream($stream);
		$logger = new Zend_Log($writer);
		$logger->log($errorMessage, Zend_Log::ALERT);
	}

	/**
 	 * Log error to Slack.
 	 * Parameter is expected to be an ArrayObject as formatted by
 	 * Zend_Controller_Plugin_ErrorHandler::_handleError
 	 */
	public static function logErrorToSlack(ArrayObject $errors) {
		try {
			$slack = new Garp_Service_Slack();
		} catch (Exception $e) {
			return false;
		}

		$shortErrorMessage = self::_composeShortErrorMessage($errors);

		//	Add first occurrence and StackTrace as attachments
		$trace = self::_filterBasePath(
			str_replace('->', '::', $errors->exception->getTraceAsString())
		);
		$params['attachments'] = array(
			array(
				'title' => self::_getExceptionClass($errors),
				'text' => $slack->wrapCodeMarkup(
					$errors->exception->getMessage()
					. "\n"
					. self::_filterBasePath($errors->exception->getFile())
					. ': '
					. $errors->exception->getLine()
				),
				'color' => '#bb5555',
				'mrkdwn_in' => array('text'),
				'short' => true
			),
			array(
				'title' => 'StackTrace',
				'text' => $slack->wrapCodeMarkup($trace),
				'color' => '#6666ee',
				'mrkdwn_in' => array('text'),
				'short' => true
			)
		);

		$slack->postMessage($shortErrorMessage, $params);

		return true;
	}

	/**
 	 * Mail an error to an admin
 	 * @return Void
 	 */
	public static function mailErrorToAdmin(ArrayObject $errors) {
		$errorMessage = self::_composeFullErrorMessage($errors);
		$subjectPrefix = '';
		if (isset($_SERVER) && !empty($_SERVER['HTTP_HOST'])) {
			$subjectPrefix = '['.$_SERVER['HTTP_HOST'].'] ';
		}

		$ini = Zend_Registry::get('config');
		$to = (
			isset($ini->app) &&
			isset($ini->app->errorReportEmailAddress) &&
			$ini->app->errorReportEmailAddress
		)
			? $ini->app->errorReportEmailAddress
			: self::ERROR_REPORT_MAIL_ADDRESS_FALLBACK
		;

		$mailer = new Garp_Mailer();
		return $mailer->send(array(
			'to' => $to,
			'subject' => $subjectPrefix . 'An application error occurred',
			'message' => $errorMessage
		));
	}

	protected static function _composeShortErrorMessage(ArrayObject $errors) {
		$appName = self::_getApplicationName();

		$exceptionType = self::_getExceptionClass($errors);
		$article = self::_startsWithVowel($exceptionType) ? 'an' : 'a';
		$message = "Found {$article} `{$exceptionType}` in project `"
			. $appName . "` :neutral_face:\n";

		// Add user information
		$auth = Garp_Auth::getInstance();
		if ($auth->isLoggedIn()) {
			$userData = $auth->getUserData();
			$message .= 'Caused by '
				. $userData['role'] . ' '
				. $userData['name']
				. ' (' . $userData['email'] . ").\n"
			;
		}

		// Add environment and IP
		$message .= 'On `' . APPLICATION_ENV . '`';
{'fraction_non_alphanumeric': 0.1614914736574192, 'fraction_numerical': 0.0020361415118350726, 'mean_word_length': 4.041051956382296, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 93, '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': '900110', 'n_tokens_mistral': 2911, 'n_tokens_neox': 2522, 'n_words': 530}