Skip to content

Commit

Permalink
MDL-20699 more abstraction in default exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Nov 1, 2009
1 parent 4c1c917 commit 695c5ec
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/setuplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,29 +166,21 @@ function __construct($hint, $debuginfo=null) {
* @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
$DB->set_debug(0);
}

// 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)) {
Expand All @@ -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.
Expand Down

0 comments on commit 695c5ec

Please sign in to comment.