/** * Class constructor (initializes gettext) * @param string $forcedLocale The locale that has to be used instead of the configured one */ public function __construct($locale = TranslationTool::DEFAULT_LOCALE) { if ($locale == NULL && !TranslationTool::isTranslationAllowed()) { throw new TranslationException('Unable to set locale in an other context than web'); } // TODO: check locale from broswer, session, registration params... $this->setLocale(TranslationTool::getLocaleFromLanguage($locale)); }
/** * Exception handler * @param Exception $exception The exception raised */ function exceptionHandler($exception) { try { $exceptionMessage = NULL; // If a transaction is in progress, rollback it if (DatabaseFactory::isTransactionInProgress()) { DatabaseFactory::rollback(); } // logs exception before doing anything else, for logs to be filled even if an exception will occur during the exception treatment $logInstance = LogTool::getInstance(); // debug exception in web context if ($exception instanceof GenericWarningException) { // logs warning $logInstance->logException($exception, LogTool::WARNING_LEVEL); } else { // logs error $logInstance->logException($exception); } $exceptionClass = get_class($exception); if ($exception instanceof GenericException) { // Displays message if (TranslationTool::isTranslationAllowed()) { $exceptionMessage = $exception->getUserLocalizedMessage(); } else { $exceptionMessage = $exception->getMessage(); } } else { // If error is not managed, displays generic message $exceptionMessage = 'GENERIC_EXCEPTION'; } if (SystemTool::isAjaxContext()) { echo json_encode(array('error' => $exceptionMessage), true); } else { echo $exceptionMessage; } } catch (Exception $e) { echo $e->getMessage(); try { LogTool::getInstance()->logException($e); echo $exceptionClass . ' raised : ' . $exceptionMessage; echo '+ ' . get_class($e) . ' while handling exception : ' . $e->getMessage(); exit(LogTool::KO_RETURN_CODE); } catch (Exception $e2) { try { LogTool::getInstance()->logException($e2); echo $exceptionClass . ' raised (unable to display exception details)'; exit(LogTool::KO_RETURN_CODE); } catch (Exception $e3) { echo 'unable to display exception details'; exit; } } } }
public static function getLoginErrorMessage() { return TranslationTool::getInstance()->translate('Login ou mot de passe incorrect'); }
/** * Returns a localized error message corresponding to current exception * @return string The localized exception message */ public function getUserLocalizedMessage() { return TranslationTool::getInstance()->translate(StringTool::upper(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', get_class($this)))); }
<?php // Starts session before header is sent session_start(); header('Content-type:application/json'); require_once 'globals/globals.php'; // Gets class_loader require_once 'class_loader.inc.php'; // Gets error handler require_once 'error_handler.inc.php'; $language = 'fr'; $t = TranslationTool::getInstance($language); SystemTool::$context = SystemTool::CONTEXT_AJAX; // starts transaction DatabaseFactory::startTransaction(); $authenticationInstance = AuthenticationTool::getInstance(); if (!$authenticationInstance->isPlayerConnected()) { throw new PlayerNotConnectedException(); } if (!RequestTool::isAjaxRequest()) { throw new Exception(); }