From 9797836b2887d0edb70eb66f9c9d5bdb4bb49d15 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Thu, 28 May 2015 01:11:10 +0200 Subject: Handle nested Exceptions Signed-off-by: Florian Pritz --- index.php | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index d7d3715f5..b9bc09709 100644 --- a/index.php +++ b/index.php @@ -249,17 +249,28 @@ function getExceptionTraceAsString($exception) { return $rtn; } -function _log_exception($e) +function _log_exception($ex) { - $backtrace = getExceptionTraceAsString($e); - $log_heading = sprintf("Exception '%s' with message '%s' in %s:%d", get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()); - error_log($log_heading."\n".$backtrace); + $exceptions = array($ex); + while ($ex->getPrevious() !== null) { + $ex = $ex->getPrevious(); + $exceptions[] = $ex; + } + + foreach ($exceptions as $key => $e) { + $message = sprintf("Exception %d/%d '%s' with message '%s' in %s:%d\n", $key+1, count($exceptions), get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()); + if (method_exists($e, "get_data") && $e->get_data() !== NULL) { + $message .= 'Data: '.var_export($e->get_data(), true)."\n"; + } + $message .= "Backtrace:\n".getExceptionTraceAsString($e)."\n"; + error_log($message); + } } // The actual exception handler -function _actual_exception_handler($e) +function _actual_exception_handler($ex) { - _log_exception($e); + _log_exception($ex); $display_errors = in_array(strtolower(ini_get('display_errors')), array('1', 'on', 'true', 'stdout')); @@ -268,13 +279,25 @@ function _actual_exception_handler($e) $message = "

An unhandled error occured.

\n"; if ($display_errors) { - $backtrace = getExceptionTraceAsString($e); - $message .= '
'; - $message .= 'Fatal error: Uncaught exception '.get_class($e).'
'; - $message .= 'Message: '.$e->getMessage().'
'; - $message .= '
'.(str_replace(FCPATH, "./", $backtrace)).'
'; - $message .= 'thrown in '.$e->getFile().' on line '.$e->getLine().'
'; - $message .= '
'; + $exceptions = array($ex); + while ($ex->getPrevious() !== null) { + $ex = $ex->getPrevious(); + $exceptions[] = $ex; + } + + foreach ($exceptions as $key => $e) { + $backtrace = getExceptionTraceAsString($e); + $message .= '
'; + $message .= 'Exception '.($key+1).' of '.count($exceptions).'
'; + $message .= 'Fatal error: Uncaught exception '.htmlspecialchars(get_class($e)).'
'; + $message .= 'Message: '.htmlspecialchars($e->getMessage()).'
'; + if (method_exists($e, "get_data") && $e->get_data() !== NULL) { + $message .= 'Data:
'.htmlspecialchars(var_export($e->get_data(), true)).'

'; + } + $message .= 'Backtrace:
'.htmlspecialchars(str_replace(FCPATH, "./", $backtrace)).'
'; + $message .= 'thrown in '.htmlspecialchars($e->getFile()).' on line '.htmlspecialchars($e->getLine()).'
'; + $message .= '
'; + } } else { $message .="

More information can be found in the php error log or by enabling display_errors.

"; } -- cgit v1.2.3-24-g4f1b