Advertisement
fernandezekiel

Untitled

Jan 13th, 2014
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. /**
  2. this is from Antonio Ramirez
  3.      * Error handler, when there is an error this will fire
  4.      * @param CEvent $event
  5.      */
  6.     public function apiErrorHandler(CEvent $event)
  7.     {
  8.         $event->handled = true;
  9.         $debug = Yii::app()->params['yii.debug'];
  10.         $response = array();
  11.         if ($event instanceof CExceptionEvent)
  12.         {
  13.             if ($event->exception instanceof EApiError)
  14.             {
  15.                 $response['code'] = $event->exception->statusCode;
  16.                 $response['message'] = $event->exception->getMessage();
  17.             } else
  18.             {
  19.                 $response['code'] = isset($event->exception->statusCode)
  20.                     ? $event->exception->statusCode
  21.                     : $event->exception->getCode();
  22.                 $response['message'] = $event->exception->getMessage();
  23.                 if ($debug)
  24.                     $response['traceback'] = $event->exception->getTrace();
  25.             }
  26.         } else if ($event instanceof CErrorEvent)
  27.         {
  28.             $response['code'] = 500;
  29.             $response['message'] = $event->message;
  30.  
  31.             if ($debug)
  32.                 $response['traceback'] = debug_backtrace();
  33.         }
  34.  
  35.         if (!$response['code'])
  36.             $response['code'] = 500;
  37.  
  38.         try
  39.         {
  40.             HHttp::sendHttpResponseCode($response['code']);
  41.         } catch (exception $e)
  42.         {
  43.             $response['code'] = 500;
  44.             HHttp::sendHttpResponseCode($response['code']);
  45.         }
  46.  
  47.         $this->renderJson($response);
  48.  
  49.         Yii::app()->end();
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement