From 9ea78213f8e505b5fde7372106adc1947d1f7de2 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Tue, 3 Feb 2015 11:14:29 +0100 Subject: Improve general exception handling Signed-off-by: Florian Pritz --- application/controllers/api.php | 3 +++ index.php | 14 ++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/application/controllers/api.php b/application/controllers/api.php index dc31f47d2..3297f0614 100644 --- a/application/controllers/api.php +++ b/application/controllers/api.php @@ -51,6 +51,9 @@ class Api extends MY_Controller { return send_json_reply($c->$function()); } catch (\exceptions\PublicApiException $e) { return send_json_error_reply($e->get_error_id(), $e->getMessage(), $e->get_data()); + } catch (\Exception $e) { + _log_exception($e); + return send_json_error_reply("internal-error", "An unhandled internal server error occured"); } } } diff --git a/index.php b/index.php index 35fb64313..21d3ffc04 100644 --- a/index.php +++ b/index.php @@ -249,17 +249,26 @@ function getExceptionTraceAsString($exception) { return $rtn; } +function _log_exception($e) +{ + $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); +} + // The actual exception handler function _actual_exception_handler($e) { + _log_exception($e); + $display_errors = in_array(strtolower(ini_get('display_errors')), array('1', 'on', 'true', 'stdout')); $GLOBALS["is_error_page"] = true; $heading = "Internal Server Error"; $message = "

An unhandled error occured.

\n"; - $backtrace = getExceptionTraceAsString($e); if ($display_errors) { + $backtrace = getExceptionTraceAsString($e); $message .= '
'; $message .= 'Fatal error: Uncaught exception '.get_class($e).'
'; $message .= 'Message: '.$e->getMessage().'
'; @@ -270,9 +279,6 @@ function _actual_exception_handler($e) $message .="

More information can be found in syslog or by enabling display_errors.

"; } - $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); - $message = "$message"; include APPPATH."/errors/error_general.php"; } -- cgit v1.2.3-24-g4f1b