Skip to content

Commit

Permalink
MDL-30147 do not rely on dml exception type outside of dml layer
Browse files Browse the repository at this point in the history
The trouble is that dml driver methods (insert, update, select) are not guaranteed to return the same exception class for various db problems and coding style issues. The recommended practice is to catch dml_exception only.
  • Loading branch information
skodak committed Nov 19, 2011
1 parent c04e80e commit 69ac5d4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ function add_to_log($courseid, $module, $action, $url='', $info='', $cm=0, $user

try {
$DB->insert_record_raw('log', $log, false);
} catch (dml_write_exception $e) {
} catch (dml_exception $e) {
debugging('Error: Could not insert a new entry to the Moodle log', DEBUG_ALL);
// MDL-11893, alert $CFG->supportemail if insert into log failed
if ($CFG->supportemail and empty($CFG->noemailever)) {
Expand Down
16 changes: 8 additions & 8 deletions lib/ddl/simpletest/testddl.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function test_table_exists() {
ob_start(); // hide debug warning
try {
$result = $DB->get_records('test_table0');
} catch (dml_read_exception $e) {
} catch (dml_exception $e) {
$result = false;
}
ob_end_clean();
Expand All @@ -203,7 +203,7 @@ public function test_table_exists() {
ob_start(); // hide debug warning
try {
$result = $DB->get_records('test_table0');
} catch (dml_read_exception $e) {
} catch (dml_exception $e) {
$result = false;
}
ob_end_clean();
Expand Down Expand Up @@ -337,14 +337,14 @@ public function test_drop_table() {

try { // columns cache must be empty, so sentence throw exception
$columns = $DB->get_columns('test_table0');
} catch (dml_read_exception $e) {
} catch (dml_exception $e) {
$columns = false;
}
$this->assertFalse($columns);

try { /// throw exception
$indexes = $DB->get_indexes('test_table0');
} catch (dml_read_exception $e) {
} catch (dml_exception $e) {
$indexes = false;
}
$this->assertFalse($indexes);
Expand Down Expand Up @@ -931,7 +931,7 @@ public function testChangeFieldNullability() {
ob_start(); // hide debug warning
try {
$result = $DB->insert_record('test_table_cust0', $record, false);
} catch (dml_write_exception $e) {
} catch (dml_exception $e) {
$result = false;
}
ob_end_clean();
Expand All @@ -953,7 +953,7 @@ public function testChangeFieldNullability() {
ob_start(); // hide debug warning
try {
$result = $DB->insert_record('test_table_cust0', $record, false);
} catch (dml_write_exception $e) {
} catch (dml_exception $e) {
$result = false;
}
ob_end_clean();
Expand Down Expand Up @@ -1022,7 +1022,7 @@ public function testAddUniqueIndex() {
ob_start(); // hide debug warning
try {
$result = $DB->insert_record('test_table_cust0', $record, false);
} catch (dml_write_exception $e) {
} catch (dml_exception $e) {
$result = false;;
}
ob_end_clean();
Expand Down Expand Up @@ -1644,7 +1644,7 @@ public function test_char_size_limit() {

$rec = $DB->get_record($tablename, array('id'=>$id));
$this->assertIdentical($rec->name, $maxstr);
} catch (dml_write_exception $e) {
} catch (dml_exception $e) {
if ($DB->get_dbfamily() === 'oracle') {
$this->fail('Oracle does not support text fields larger than 4000 bytes, this is not a big problem for mostly ascii based languages');
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/dml/simpletest/testdml.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ public function test_execute() {
$DB->execute($sql);
$this->fail("Expecting an exception, none occurred");
} catch (Exception $e) {
$this->assertTrue($e instanceof dml_write_exception);
$this->assertTrue($e instanceof dml_exception);
}

// update records
Expand Down Expand Up @@ -1785,7 +1785,7 @@ public function test_insert_record_raw() {
try {
$DB->insert_record_raw($tablename, array('xxxxx' => 3, 'onechar' => 'bb'));
$this->assertFail('Exception expected due to invalid column');
} catch (dml_write_exception $ex) {
} catch (dml_exception $ex) {
$this->assertTrue(true);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/dmllib.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function __construct() {
}

/**
* DML read exception - triggered by SQL syntax errors, missing tables, etc.
* DML read exception - triggered by some SQL syntax errors, etc.
*/
class dml_read_exception extends dml_exception {
/** @var string */
Expand Down Expand Up @@ -185,7 +185,7 @@ function __construct($tablename, $sql='', array $params=null) {
}

/**
* DML write exception - triggered by SQL syntax errors, missing tables, etc.
* DML write exception - triggered by some SQL syntax errors, etc.
*/
class dml_write_exception extends dml_exception {
/** @var string */
Expand Down
2 changes: 1 addition & 1 deletion lib/setuplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ function initialise_cfg() {
$CFG->{$name} = $value;
}
}
} catch (dml_read_exception $e) {
} catch (dml_exception $e) {
// most probably empty db, going to install soon
}
}
Expand Down

0 comments on commit 69ac5d4

Please sign in to comment.