error_page = $error500_page; if(!defined('LIMB_APP_MODE')) $this->mode = self :: MODE_DEVEL; else $this->mode = LIMB_APP_MODE; } function run($filter_chain) { $this->toolkit = lmbToolkit :: instance(); lmbErrorGuard :: registerFatalErrorHandler($this, 'handleFatalError'); lmbErrorGuard :: registerExceptionHandler($this, 'handleException'); $filter_chain->next(); } function handleFatalError($error) { $this->toolkit->getLog()->error($error['message']); $this->toolkit->getResponse()->reset(); if($this->mode == self :: MODE_DEVEL) $this->_echoErrorBacktrace($error); if($this->mode == self :: MODE_PRODUCTION) $this->_echoErrorPage(); exit(1); } function handleException($e) { if(function_exists('debugBreak')) debugBreak(); $this->toolkit->getLog()->exception($e); $this->toolkit->getResponse()->reset(); if($this->mode == self :: MODE_DEVEL) $this->_echoExceptionBacktrace($e); if($this->mode == self :: MODE_PRODUCTION) $this->_echoErrorPage(); exit(1); } function _echoErrorPage() { for($i=0; $i < ob_get_level(); $i++) ob_end_clean(); echo file_get_contents($this->error_page); } protected function _echoErrorBacktrace($error) { $message = $error['message']; $trace = ''; $file = $error['file']; $line = $error['line']; $context = htmlspecialchars($this->_getFileContext($file, $line)); $request = htmlspecialchars($this->toolkit->getRequest()->dump()); for($i=0; $i < ob_get_level(); $i++) ob_end_clean(); $session = htmlspecialchars($this->toolkit->getSession()->dump()); echo $this->_renderTemplate($message, $trace, $file, $line, $context, $request, $session); } protected function _echoExceptionBacktrace($e) { $error = htmlspecialchars($e->getMessage()); $trace = htmlspecialchars($e->getTraceAsString()); list($file, $line) = $this->_extractExceptionFileAndLine($e); $context = htmlspecialchars($this->_getFileContext($file, $line)); $request = htmlspecialchars($this->toolkit->getRequest()->dump()); $session = htmlspecialchars($this->toolkit->getSession()->dump()); for($i=0; $i < ob_get_level(); $i++) ob_end_clean(); echo $this->_renderTemplate($error, $trace, $file, $line, $context, $request, $session); } protected function _renderTemplate($error, $trace, $file, $line, $context, $request, $session) { $formatted_error = nl2br($error); $body = << {$error}

{$formatted_error}

Context | Call stack | Raw dump

Error in '{$file}' around line {$line}:

{$context}

Request

{$request}

Session

{$session}
EOD; return $body; } protected function _extractExceptionFileAndLine($e) { if($e instanceof WactException) { $params = $e->getParams(); if(isset($params['file'])) return array($params['file'], $params['line']); } return array($e->getFile(), $e->getLine()); } protected function _getFileContext($file, $line_number) { $context = array(); $i = 0; foreach(file($file) as $line) { $i++; if($i >= $line_number - self :: CONTEXT_RADIUS && $i <= $line_number + self :: CONTEXT_RADIUS) $context[] = $i . "\t" . $line; if($i > $line_number + self :: CONTEXT_RADIUS) break; } return "\n" . implode("", $context); } } ?>