From 695c5ec4cdbaf001197d031f78574cfd6b3d655b Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 1 Nov 2009 09:58:04 +0000 Subject: [PATCH] MDL-20699 more abstraction in default exception handler --- lib/setuplib.php | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/setuplib.php b/lib/setuplib.php index 2dcbdc3bca..0568ced68f 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -166,7 +166,7 @@ class invalid_state_exception extends moodle_exception { * @return void -does not return. Terminates execution! */ function default_exception_handler($ex) { - global $CFG, $DB, $OUTPUT, $SCRIPT; + global $DB, $OUTPUT; if ($DB) { // If you enable db debugging and exception is thrown, the print footer prints a lot of rubbish @@ -174,21 +174,13 @@ function default_exception_handler($ex) { } // detect active db transactions, rollback and log as error - if ($DB && $DB->is_transaction_started()) { - error_log('Database transaction aborted by exception in ' . $CFG->dirroot . $SCRIPT); - try { - // note: transaction blocks should never change current $_SESSION - $DB->rollback_sql(); - } catch (Exception $ignored) { - // default exception handler MUST not throw any exceptions!! - } - } + abort_all_db_transactions(); $info = get_exception_info($ex); if (debugging('', DEBUG_MINIMAL)) { $logerrmsg = "Default exception handler: ".$info->message.' Debug: '.$info->debuginfo."\n".format_backtrace($info->backtrace, true); - error_log($logerrmsg, 0); + error_log($logerrmsg); } if (is_early_init($info->backtrace)) { @@ -209,6 +201,25 @@ function default_exception_handler($ex) { exit(1); // General error code } +/** + * Unconditionally abort all database transactions, this function + * should be called from exception handlers only. + * @return void + */ +function abort_all_db_transactions() { + global $CFG, $DB, $SCRIPT; + + if ($DB && $DB->is_transaction_started()) { + error_log('Database transaction aborted automatically in ' . $CFG->dirroot . $SCRIPT); + try { + // note: transaction blocks should never change current $_SESSION + $DB->rollback_sql(); + } catch (Exception $ignored) { + // default exception handler MUST not throw any exceptions!! + } + } +} + /** * This function encapsulates the tests for whether an exception was thrown in * early init -- either during setup.php or during init of $OUTPUT. -- 2.39.5