2011-06-08 23:45:08 -05:00
< ? php namespace System ;
class Error {
/**
2011-08-02 10:52:02 -05:00
* Human - readable error levels and descriptions .
2011-06-08 23:45:08 -05:00
*
* @ var array
*/
public static $levels = array (
0 => 'Error' ,
E_ERROR => 'Error' ,
E_WARNING => 'Warning' ,
E_PARSE => 'Parsing Error' ,
E_NOTICE => 'Notice' ,
E_CORE_ERROR => 'Core Error' ,
E_CORE_WARNING => 'Core Warning' ,
E_COMPILE_ERROR => 'Compile Error' ,
E_COMPILE_WARNING => 'Compile Warning' ,
E_USER_ERROR => 'User Error' ,
E_USER_WARNING => 'User Warning' ,
E_USER_NOTICE => 'User Notice' ,
E_STRICT => 'Runtime Notice'
);
/**
* Handle an exception .
*
* @ param Exception $e
* @ return void
*/
public static function handle ( $e )
{
2011-08-02 10:52:02 -05:00
// Clear the output buffer so nothing is sent to the browser except the error
// message. This prevents any views that have already been rendered from being
// in an incomplete or erroneous state.
2011-06-08 23:45:08 -05:00
if ( ob_get_level () > 0 )
{
ob_clean ();
}
$severity = ( array_key_exists ( $e -> getCode (), static :: $levels )) ? static :: $levels [ $e -> getCode ()] : $e -> getCode ();
2011-08-02 10:52:02 -05:00
$message = rtrim ( $e -> getMessage (), '.' ) . ' in ' . str_replace ( array ( APP_PATH , SYS_PATH ), array ( 'APP_PATH/' , 'SYS_PATH/' ), $e -> getFile ()) . ' on line ' . $e -> getLine () . '.' ;
2011-06-08 23:45:08 -05:00
if ( Config :: get ( 'error.log' ))
{
2011-08-02 10:52:02 -05:00
call_user_func ( Config :: get ( 'error.logger' ), $severity , $message , $e -> getTraceAsString ());
2011-06-08 23:45:08 -05:00
}
2011-07-18 11:34:04 -07:00
static :: show ( $e , $severity , $message );
2011-07-11 07:47:23 -07:00
exit ( 1 );
}
/**
* Show the error view .
*
* @ param Exception $e
* @ param string $severity
* @ param string $message
* @ return void
*/
2011-07-18 11:34:04 -07:00
private static function show ( $e , $severity , $message )
2011-07-11 07:47:23 -07:00
{
2011-06-08 23:45:08 -05:00
if ( Config :: get ( 'error.detail' ))
{
2011-07-31 14:48:17 -05:00
$view = View :: make ( 'error/exception' )
2011-07-07 23:40:29 -05:00
-> bind ( 'severity' , $severity )
-> bind ( 'message' , $message )
-> bind ( 'line' , $e -> getLine ())
2011-07-11 08:38:49 -07:00
-> bind ( 'trace' , $e -> getTraceAsString ())
2011-07-18 11:34:04 -07:00
-> bind ( 'contexts' , static :: context ( $e -> getFile (), $e -> getLine ()));
2011-08-02 10:52:02 -05:00
2011-06-08 23:45:08 -05:00
Response :: make ( $view , 500 ) -> send ();
}
else
{
Response :: make ( View :: make ( 'error/500' ), 500 ) -> send ();
}
}
2011-07-11 08:38:49 -07:00
/**
* Get the code surrounding a given line in a file .
*
* @ param string $path
* @ param int $line
* @ param int $padding
* @ return string
*/
private static function context ( $path , $line , $padding = 5 )
{
if ( file_exists ( $path ))
{
$file = file ( $path , FILE_IGNORE_NEW_LINES );
array_unshift ( $file , '' );
2011-08-02 10:52:02 -05:00
if (( $start = $line - $padding ) < 0 ) $start = 0 ;
if (( $length = ( $line - $start ) + $padding + 1 ) < 0 ) $length = 0 ;
2011-07-11 08:38:49 -07:00
return array_slice ( $file , $start , $length , true );
}
return array ();
}
2011-06-08 23:45:08 -05:00
}