ManiaLib 1.0b1 API Reference

Source for file ErrorHandling.class.php

Documentation is available at ErrorHandling.class.php

  1. <?php
  2. /**
  3.  * Error handling features
  4.  * 
  5.  * @author Maxime Raoust
  6.  * @copyright 2009-2010 NADEO
  7.  * @package ManiaLib
  8.  * @subpackage ErrorHandling
  9.  */
  10.  
  11. /**
  12.  * Error handling
  13.  * Error handling features and handlers
  14.  * @todo doc explain error handling strategy
  15.  * @package ManiaLib
  16.  * @subpackage ErrorHandling
  17.  */
  18. abstract class ErrorHandling
  19. {
  20.     /**
  21.      * @ignore
  22.      */
  23.     protected static $messageConfigs array(
  24.         'default' => array(
  25.             'title'      => '%s',
  26.             'simpleLine' => '    %s',
  27.             'line'       => '    %s: %s'
  28.         ),
  29.         'debug' => array(
  30.             'title'      => '$<$ff0$o%s$>',
  31.             'simpleLine' => '    %s',
  32.             'line'       => '    $<$ff0%s$> :    %s'
  33.         ),
  34.     );
  35.     
  36.     /**
  37.      * Error handler
  38.      * Converts standard PHP errors into ErrorException
  39.      * Usage (loaded by default in the MVC framework):
  40.      * <code>
  41.      * set_error_handler(array('ErrorHandling', 'exceptionErrorHandler'));
  42.      * </code>
  43.      * @throws ErrorException
  44.      */
  45.     static function exceptionErrorHandler($errno$errstr$errfile$errline
  46.     {
  47.         throw new ErrorException($errstr0$errno$errfile$errline);
  48.     }
  49.     
  50.     /**
  51.      * Exception handler
  52.      * Used to cleanly catch exceptions, and is also used as uncaught exception handler.
  53.      * Prints a nice error message in manialink
  54.      * Usage for catching exceptions:
  55.      * <code>
  56.      * try
  57.      * {
  58.      *     //...
  59.      * }
  60.      * catch(Exception $exception)
  61.      * {
  62.      *     ErrorHandling::exceptionHandler($exception);
  63.      * }
  64.      * </code>
  65.      * Usage for default exception handling:
  66.      * <code>
  67.      * set_exception_handler(array('ErrorHandling', 'exceptionHandler'));
  68.      * </code>
  69.      * Note: the MVC framework uses both by default
  70.      */
  71.     static function exceptionHandler(Exception $exception)
  72.     {
  73.         $request call_user_func(array(APP_FRAMEWORK_REQUEST_ENGINE_CLASS'getInstance'));
  74.         $requestURI $request->createLink();
  75.         
  76.         if($exception instanceof UserException)
  77.         {
  78.             Debug::log(self::computeShortMessage($exception).'  '.$requestURIDebug::LOG_DATEAPP_USER_ERROR_LOG);
  79.             self::showErrorDialog($exception->getMessage());
  80.         }
  81.         else
  82.         {
  83.             $requestURILine sprintf(self::$messageConfigs['default']['line']'Request URI'$requestURI);            
  84.             $message self::computeMessage($exceptionself::$messageConfigs['default']array($requestURILine));
  85.             Debug::log($messageDebug::LOG_DATEAPP_ERROR_LOG);
  86.             
  87.             if(APP_DEBUG_LEVEL)
  88.             {
  89.                 $requestURILine sprintf(self::$messageConfigs['debug']['line']'Request URI'$requestURI);
  90.                 $message self::computeMessage($exceptionself::$messageConfigs['debug']array($requestURILine));
  91.                 self::showDebugDialog($message);
  92.             }
  93.             else
  94.             {
  95.                 self::showErrorDialog();
  96.             }
  97.         }
  98.     }
  99.     
  100.     /**
  101.      * Shows an error dialog to the user with the specified message
  102.      * @param The message to show, default is 'Fatal error'
  103.      * @ignore
  104.      */
  105.     static function showErrorDialog($message 'Fatal error')
  106.     {
  107.         $request call_user_func(array(APP_FRAMEWORK_REQUEST_ENGINE_CLASS'getInstance'));
  108.         $linkstr $request->getReferer();
  109.         
  110.         Manialink::load();
  111.         {
  112.             $ui new Panel(7035);
  113.             $ui->setAlign('center''center');
  114.             $ui->title->setStyle(Label::TextTitleError);
  115.             $ui->titleBg->setSubStyle(Bgs1::BgTitle2);
  116.             $ui->title->setText('Error');
  117.             $ui->save();
  118.  
  119.             $ui new Label(68);
  120.             $ui->enableAutonewline();
  121.             $ui->setAlign('center''center');
  122.             $ui->setPosition(002);
  123.             $ui->setText($message);
  124.             $ui->save();
  125.  
  126.             $ui new Button;
  127.             $ui->setText('Back');
  128.             
  129.             $ui->setManialink($linkstr);
  130.             $ui->setPosition(0-125);
  131.             $ui->setHalign('center');
  132.             $ui->save();
  133.         }
  134.         Manialink::render();
  135.         exit;
  136.     }
  137.     
  138.     /**
  139.      * Error dialog for debug, the panel is bigger to fit the whole exception log
  140.      * @param The message to show, default is 'Fatal error'
  141.      * @ignore
  142.      */
  143.     static function showDebugDialog($message 'Fatal error')
  144.     {
  145.         $request call_user_func(array(APP_FRAMEWORK_REQUEST_ENGINE_CLASS'getInstance'));
  146.         $linkstr $request->getReferer();
  147.         
  148.         Manialink::load();
  149.         {
  150.             $ui new Panel(12492);
  151.             $ui->setAlign('center''center');
  152.             $ui->title->setStyle(Label::TextTitleError);
  153.             $ui->titleBg->setSubStyle(Bgs1::BgTitle2);
  154.             $ui->title->setText('Error');
  155.             $ui->save();
  156.  
  157.             $ui new Label(122);
  158.             $ui->setAlign('left''top');
  159.             $ui->setPosition(-60382);
  160.             $ui->enableAutonewline();
  161.             $ui->setText(utf8_encode($message));
  162.             $ui->save();
  163.  
  164.             $ui new Button;
  165.             $ui->setText('Back');
  166.             
  167.             $ui->setManialink($linkstr);
  168.             $ui->setPosition(0-405);
  169.             $ui->setHalign('center');
  170.             $ui->save();
  171.         }
  172.         Manialink::render();
  173.         exit;
  174.     }
  175.                 
  176.     /**
  177.      * Computes a human readable log message from any exception
  178.      * @return string 
  179.      * @ignore
  180.      */
  181.     static protected function computeMessage(Exception $earray $stylesarray $additionalLines array())
  182.     {
  183.         $trace $e->getTraceAsString();
  184.         $trace explode("\n"$trace);
  185.         foreach ($trace as $key=>$value)
  186.         {
  187.             $trace[$keysprintf($styles['simpleLine']preg_replace('/#[0-9]*\s*/'''$value));
  188.         }
  189.         $file sprintf($styles['simpleLine']$e->getFile().' ('.$e->getLine().')');
  190.         
  191.         $lines[sprintf($styles['title']get_class($e));
  192.         $lines['';
  193.         $lines[sprintf($styles['line']'Message'print_r($e->getMessage()true));
  194.         $lines[sprintf($styles['line']'Code'$e->getCode());
  195.         $lines array_merge($lines$additionalLinesarray($file)$trace);
  196.         $lines['';
  197.         return implode("\n"$lines);
  198.     }
  199.     
  200.     /**
  201.      * Computes a short human readable log message from any exception
  202.      * @return string 
  203.      * @ignore
  204.      */
  205.     static protected function computeShortMessage(Exception $e)
  206.     {
  207.         $message get_class($e).'  '.$e->getMessage().'  ('.$e->getCode().')';
  208.         return $message;
  209.     }
  210.     
  211.     
  212. }
  213.  
  214. ?>