]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-20699 more abstraction in default exception handler
authorPetr Skoda <skodak@moodle.org>
Sun, 1 Nov 2009 09:58:04 +0000 (09:58 +0000)
committerPetr Skoda <skodak@moodle.org>
Sun, 1 Nov 2009 09:58:04 +0000 (09:58 +0000)
lib/setuplib.php

index 2dcbdc3bca92dd1682cf1edbeae7502c0fc2dabe..0568ced68f4a8cbc99ea1c3bb2c9e24d498e2540 100644 (file)
@@ -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.