PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/framework/boot/error/CliErrorHandler.php

http://zoop.googlecode.com/
PHP | 122 lines | 102 code | 11 blank | 9 comment | 7 complexity | 299fb858beed7ca7ab671ffbfb340a20 MD5 | raw file
  1<?php
  2class CliErrorHandler
  3{
  4	static function throwException($errno, $errstr, $errfile, $errline, $context, $backtrace = NULL)
  5	{
  6		// maybe we should use this here: http://us3.php.net/manual/en/class.errorexception.php
  7		$e = new Exception($errstr, $errno);
  8		// print_r($e);
  9		// die();
 10		// $e->setFile($errfile);
 11		// $e->setLine($errline);
 12		throw $e;
 13	}
 14	
 15	static function handleError($errno, $errstr, $errfile, $errline, $context, $backtrace = NULL)
 16	{
 17		if(!defined('app_status'))
 18			define('app_status', 'dev');
 19	
 20		if((error_reporting() & $errno) !== $errno)
 21			return true;
 22		
 23		switch(app_status)
 24		{
 25			case 'dev':
 26				self::handleDevError($errno, $errstr, $errfile, $errline, $context, $backtrace);
 27				break;
 28			
 29			case 'test':
 30				trigger_error('status not handled:' . app_status);
 31				break;
 32			
 33			case 'live':
 34				trigger_error('status not handled:' . app_status);
 35				break;
 36			
 37			default:
 38				trigger_error('status not handled:' . app_status);
 39				break;
 40		}
 41	}
 42	
 43	function handleDevError($errno, $errstr, $errfile, $errline, $context, $backtrace)
 44	{
 45		$errorLine = self::formatErrorLine($errno, $errstr, $errfile, $errline, $context, $backtrace);
 46		echo $errorLine . "\n";
 47		$backtrace = $backtrace ? $backtrace : debug_backtrace();
 48		// array_shift($backtrace);
 49		// FormatBacktraceCli($backtrace);
 50		$backtraceView = new BacktraceViewCli($backtrace);
 51		$backtraceView->display();
 52	}
 53	
 54	function formatErrorLine($errno, $errstr, $errfile, $errline, $context)
 55	{
 56		$line = "\n";
 57		switch ($errno)
 58		{
 59			case E_ERROR:
 60			case E_PARSE:
 61			case E_CORE_ERROR:
 62			case E_COMPILE_ERROR:
 63				die('this should never happen');
 64				break;
 65			case E_CORE_WARNING:
 66			case E_COMPILE_WARNING:
 67			case E_STRICT:
 68			case E_RECOVERABLE_ERROR:
 69				$line .= 'Error type not yet handled: ' . $errno . '';
 70				break;
 71			case E_WARNING:
 72				$line .= 'Warning:';
 73				break;
 74			case E_NOTICE:
 75				$line .= 'Notice:';
 76				break;
 77			case E_DEPRECATED:
 78				$line .= 'Deprecated:';
 79				break;
 80			case E_USER_ERROR:
 81				$line .= 'User Error:';
 82				break;
 83			case E_USER_WARNING:
 84				$line .= 'User Warning:';
 85				break;
 86			case E_USER_NOTICE:
 87				$line .= 'User Notice:';
 88				break;
 89			case E_USER_DEPRECATED:
 90				$line .= 'User Deprecated:';
 91				break;
 92			case 0:
 93				$line .= 'Exception:';
 94				break;
 95			default:
 96				$line .= 'Undefined error type: ' . $errno . '';
 97			break;
 98		}
 99		
100		$line .= ' ' . $errstr . "";
101		$line .= ' in file ' . $errfile;
102		$line .= ' ( on line  ' . $errline . ')';
103		
104		return $line;
105	}
106	
107	function exceptionHandler($exception)
108	{
109//		print_r($exception->getCode());die();
110		$backtrace = $exception->getTrace();
111		$file = $exception->getFile();
112		$line = $exception->getLine();
113		if(isset($backtrace[0]['args']) && is_array($backtrace[0]['args']))
114			$backtrace[0]['args'] = array();
115		if(isset($backtrace[0]['file']))
116			$file = $backtrace[0]['file'];
117		if(isset($backtrace[0]['line']))
118			$line = $backtrace[0]['line'];
119//		print_r($backtrace[0]);
120		self::handleError($exception->getCode(), $exception->getMessage(), $file, $line, NULL, $backtrace);
121	}
122}