Managed_DataObject->updateWithKeys throws its own exception

This commit is contained in:
Mikael Nordfeldth 2015-01-25 12:45:26 +01:00
parent 998afe1844
commit 12058c30b4
10 changed files with 37 additions and 68 deletions

View File

@ -108,13 +108,8 @@ class ConfirmaddressAction extends Action
$cur->smsemail = $carrier->toEmailAddress($cur->sms); $cur->smsemail = $carrier->toEmailAddress($cur->sms);
} }
$result = $cur->updateWithKeys($orig_user); // Throws exception on failure.
$cur->updateWithKeys($orig_user);
if (!$result) {
common_log_db_error($cur, 'UPDATE', __FILE__);
// TRANS: Server error displayed when confirming an e-mail address or IM address fails.
$this->serverError(_('Could not update user.'));
}
if ($type == 'email') { if ($type == 'email') {
$cur->emailChanged(); $cur->emailChanged();

View File

@ -478,20 +478,12 @@ class EmailsettingsAction extends SettingsAction
return; return;
} }
$user->query('BEGIN');
$original = clone($user); $original = clone($user);
$user->email = null; $user->email = null;
$result = $user->updateWithKeys($original); // Throws exception on failure. Also performs it within a transaction.
$user->updateWithKeys($original);
if (!$result) {
common_log_db_error($user, 'UPDATE', __FILE__);
// TRANS: Server error thrown on database error removing a registered e-mail address.
$this->serverError(_('Could not update user.'));
}
$user->query('COMMIT');
// TRANS: Message given after successfully removing a registered e-mail address. // TRANS: Message given after successfully removing a registered e-mail address.
$this->showForm(_('The email address was removed.'), true); $this->showForm(_('The email address was removed.'), true);
@ -517,11 +509,8 @@ class EmailsettingsAction extends SettingsAction
$user->incomingemail = null; $user->incomingemail = null;
$user->emailpost = 0; $user->emailpost = 0;
if (!$user->updateWithKeys($orig)) { // Throws exception on failure. Also performs it within a transaction.
common_log_db_error($user, 'UPDATE', __FILE__); $user->updateWithKeys($orig);
// TRANS: Server error thrown on database error removing incoming e-mail address.
$this->serverError(_('Could not update user record.'));
}
// TRANS: Message given after successfully removing an incoming e-mail address. // TRANS: Message given after successfully removing an incoming e-mail address.
$this->showForm(_('Incoming email address removed.'), true); $this->showForm(_('Incoming email address removed.'), true);
@ -541,11 +530,8 @@ class EmailsettingsAction extends SettingsAction
$user->incomingemail = mail_new_incoming_address(); $user->incomingemail = mail_new_incoming_address();
$user->emailpost = 1; $user->emailpost = 1;
if (!$user->updateWithKeys($orig)) { // Throws exception on failure. Also performs it within a transaction.
common_log_db_error($user, 'UPDATE', __FILE__); $user->updateWithKeys($orig);
// TRANS: Server error thrown on database error adding incoming e-mail address.
$this->serverError(_('Could not update user record.'));
}
// TRANS: Message given after successfully adding an incoming e-mail address. // TRANS: Message given after successfully adding an incoming e-mail address.
$this->showForm(_('New incoming email address added.'), true); $this->showForm(_('New incoming email address added.'), true);

View File

@ -105,12 +105,8 @@ class RecoverpasswordAction extends Action
if (!$user->email) { if (!$user->email) {
$orig = clone($user); $orig = clone($user);
$user->email = $email; $user->email = $email;
$result = $user->updateWithKeys($orig); // Throws exception on failure.
if (!$result) { $user->updateWithKeys($orig);
common_log_db_error($user, 'UPDATE', __FILE__);
// TRANS: Server error displayed when updating a user's e-mail address in the database fails while recovering a password.
$this->serverError(_('Could not update user with confirmed email address.'));
}
} }
// Success! // Success!

View File

@ -436,21 +436,14 @@ class SmssettingsAction extends SettingsAction
return; return;
} }
$user->query('BEGIN');
$original = clone($user); $original = clone($user);
$user->sms = null; $user->sms = null;
$user->carrier = null; $user->carrier = null;
$user->smsemail = null; $user->smsemail = null;
$result = $user->updateWithKeys($original); // Throws exception on failure. Also performs it within a transaction.
if (!$result) { $user->updateWithKeys($original);
common_log_db_error($user, 'UPDATE', __FILE__);
// TRANS: Server error thrown on database error removing a registered SMS phone number.
$this->serverError(_('Could not update user.'));
}
$user->query('COMMIT');
// TRANS: Message given after successfully removing a registered SMS phone number. // TRANS: Message given after successfully removing a registered SMS phone number.
$this->showForm(_('The SMS phone number was removed.'), true); $this->showForm(_('The SMS phone number was removed.'), true);
@ -556,11 +549,8 @@ class SmssettingsAction extends SettingsAction
$user->incomingemail = null; $user->incomingemail = null;
if (!$user->updateWithKeys($orig)) { // Throws exception on failure. Also performs it within a transaction.
common_log_db_error($user, 'UPDATE', __FILE__); $user->updateWithKeys($orig);
// TRANS: Server error displayed when the user could not be updated in SMS settings.
$this->serverError(_('Could not update user record.'));
}
// TRANS: Confirmation text after updating SMS settings. // TRANS: Confirmation text after updating SMS settings.
$this->showForm(_('Incoming email address removed.'), true); $this->showForm(_('Incoming email address removed.'), true);
@ -581,11 +571,8 @@ class SmssettingsAction extends SettingsAction
$user->incomingemail = mail_new_incoming_address(); $user->incomingemail = mail_new_incoming_address();
if (!$user->updateWithKeys($orig)) { // Throws exception on failure. Also performs it within a transaction.
common_log_db_error($user, 'UPDATE', __FILE__); $user->updateWithKeys($orig);
// TRANS: Server error displayed when the user could not be updated in SMS settings.
$this->serverError(_('Could not update user record.'));
}
// TRANS: Confirmation text after updating SMS settings. // TRANS: Confirmation text after updating SMS settings.
$this->showForm(_('New incoming email address added.'), true); $this->showForm(_('New incoming email address added.'), true);

View File

@ -339,7 +339,13 @@ abstract class Managed_DataObject extends Memcached_DataObject
} }
} }
if (count($parts) == 0) { if (count($parts) == 0) {
// No changes, unless made in the ->update call // No changes to keys, it's safe to run ->update(...)
if ($this->update($orig) === false) {
common_log_db_error($this, 'UPDATE', __FILE__);
// rollback as something bad occurred
$this->query('ROLLBACK');
throw new ServerException("Could not UPDATE non-keys for {$this->__table}");
}
return true; return true;
} }
$toupdate = implode(', ', $parts); $toupdate = implode(', ', $parts);
@ -357,6 +363,8 @@ abstract class Managed_DataObject extends Memcached_DataObject
} }
// Update non-keys too, if the previous endeavour worked. // Update non-keys too, if the previous endeavour worked.
// The ->update call uses "$this" values for keys, that's why we can't do this until
// the keys are updated (because they might differ from $orig and update the wrong entries).
if ($this->update($orig) === false) { if ($this->update($orig) === false) {
common_log_db_error($this, 'UPDATE', __FILE__); common_log_db_error($this, 'UPDATE', __FILE__);
// rollback as something bad occurred // rollback as something bad occurred

View File

@ -838,12 +838,8 @@ class Profile extends Managed_DataObject
common_debug("Updating User ({$this->id}) nickname from {$dataObject->nickname} to {$this->nickname}"); common_debug("Updating User ({$this->id}) nickname from {$dataObject->nickname} to {$this->nickname}");
$origuser = clone($local); $origuser = clone($local);
$local->nickname = $this->nickname; $local->nickname = $this->nickname;
$result = $local->updateWithKeys($origuser); // updateWithKeys throws exception on failure.
if ($result === false) { $local->updateWithKeys($origuser);
common_log_db_error($local, 'UPDATE', __FILE__);
// TRANS: Server error thrown when user profile settings could not be updated.
throw new ServerException(_('Could not update user nickname.'));
}
// Clear the site owner, in case nickname changed // Clear the site owner, in case nickname changed
if ($local->hasRole(Profile_role::OWNER)) { if ($local->hasRole(Profile_role::OWNER)) {

View File

@ -288,10 +288,8 @@ class OembedPlugin extends Plugin
$thumbnail->filename = $filename; $thumbnail->filename = $filename;
$thumbnail->width = $info[0]; // array indexes documented on php.net: $thumbnail->width = $info[0]; // array indexes documented on php.net:
$thumbnail->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php $thumbnail->height = $info[1]; // https://php.net/manual/en/function.getimagesize.php
if (!$thumbnail->update($orig)) { // Throws exception on failure.
unlink($fullpath); // delete the file if database failed to write $thumbnail->updateWithKeys($orig);
throw new ServerException(_('Failed to update remotely downloaded file info in database.'));
}
} }
public function onPluginVersion(array &$versions) public function onPluginVersion(array &$versions)

View File

@ -150,6 +150,7 @@ class ConfirmfirstemailAction extends Action
$this->user->email = $this->confirm->address; $this->user->email = $this->confirm->address;
// Throws exception on failure.
$this->user->updateWithKeys($orig); $this->user->updateWithKeys($orig);
$this->user->emailChanged(); $this->user->emailChanged();

View File

@ -75,7 +75,11 @@ function clear_jabber($id)
} else { } else {
$original = clone($user); $original = clone($user);
$user->jabber = null; $user->jabber = null;
$result = $user->updateWithKeys($original); try {
$user->updateWithKeys($original);
} catch (Exception $e) {
echo "WARNING: user update failed (setting jabber to null): ".$e->getMessage()."\n";
}
} }
echo "\n"; echo "\n";
} else if (!$user) { } else if (!$user) {

View File

@ -69,10 +69,8 @@ try {
$user->email = $email; $user->email = $email;
if (!$user->updateWithKeys($orig)) { // Throws exception on failure.
print "Failed!\n"; $user->updateWithKeys($orig);
throw new Exception("Can't update email address.");
}
} }
} catch (Exception $e) { } catch (Exception $e) {