Last active
February 16, 2016 18:36
-
-
Save Bogdaan/0259f94f4e9c8c614eaf to your computer and use it in GitHub Desktop.
Yii-1.x hhvm error handler hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* How to prevent "white page" on CHttpException and other errors | |
* | |
* Instruction: | |
* 1. place Hhvm.php to protected.components | |
* 2. add to index.php lines: | |
* | |
* define('YII_DEBUG', false); | |
* define('YII_ENABLE_EXCEPTION_HANDLER', false); // disable yii exception handler | |
* define('YII_ENABLE_ERROR_HANDLER', false); // disable yii error handler | |
* | |
* | |
* ////// | |
* ///// <------ config and other code here | |
* ///// | |
* | |
* // init YII | |
* $app = Yii::createWebApplication($config); | |
* | |
* // add hhvm handlers | |
* Hhvm::initSystemHandlers(); | |
* $app->run(); | |
* | |
* 3. proffit | |
* | |
* | |
* @see Hhvm::initSystemHandlers | |
* @author hcbogdan | |
*/ | |
class Hhvm | |
{ | |
public static function handleException($exception) | |
{ | |
// disable error capturing to avoid recursive errors | |
restore_error_handler(); | |
restore_exception_handler(); | |
$category='exception.'.get_class($exception); | |
if($exception instanceof CHttpException) | |
$category.='.'.$exception->statusCode; | |
// php <5.2 doesn't support string conversion auto-magically | |
$message=$exception->__toString(); | |
if(isset($_SERVER['REQUEST_URI'])) | |
$message.="\nREQUEST_URI=".$_SERVER['REQUEST_URI']; | |
if(isset($_SERVER['HTTP_REFERER'])) | |
$message.="\nHTTP_REFERER=".$_SERVER['HTTP_REFERER']; | |
$message.="\n---"; | |
Yii::log($message,CLogger::LEVEL_ERROR,$category); | |
$event=new CExceptionEvent(app(),$exception); | |
app()->onException($event); | |
if(!$event->handled) | |
{ | |
// try an error handler | |
if(($handler=app()->getErrorHandler())!==null) | |
$handler->handle($event); | |
else | |
app()->displayException($exception); | |
} | |
} | |
public static function handleError($code,$message,$file,$line) | |
{ | |
if($code & error_reporting()) | |
{ | |
// disable error capturing to avoid recursive errors | |
restore_error_handler(); | |
restore_exception_handler(); | |
$log="$message ($file:$line)\nStack trace:\n"; | |
$trace=debug_backtrace(); | |
// skip the first 3 stacks as they do not tell the error position | |
if(count($trace)>3) | |
$trace=array_slice($trace,3); | |
foreach($trace as $i=>$t) | |
{ | |
if(!isset($t['file'])) | |
$t['file']='unknown'; | |
if(!isset($t['line'])) | |
$t['line']=0; | |
if(!isset($t['function'])) | |
$t['function']='unknown'; | |
$log.="#$i {$t['file']}({$t['line']}): "; | |
if(isset($t['object']) && is_object($t['object'])) | |
$log.=get_class($t['object']).'->'; | |
$log.="{$t['function']}()\n"; | |
} | |
if(isset($_SERVER['REQUEST_URI'])) | |
$log.='REQUEST_URI='.$_SERVER['REQUEST_URI']; | |
Yii::log($log,CLogger::LEVEL_ERROR,'php'); | |
Yii::import('CErrorEvent',true); | |
$event=new CErrorEvent(app(),$code,$message,$file,$line); | |
app()->onError($event); | |
if(!$event->handled) | |
{ | |
// try an error handler | |
if(($handler=app()->getErrorHandler())!==null) | |
$handler->handle($event); | |
else | |
app()->displayError($code,$message,$file,$line); | |
} | |
} | |
} | |
/** | |
* Initializes the error handlers. | |
*/ | |
public static function initSystemHandlers() | |
{ | |
set_exception_handler('Hhvm::handleException'); | |
if(YII_DEBUG) | |
set_error_handler('Hhvm::handleError', error_reporting()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment