We determined in the last section that our code is breaking due to a 409
response. So, let's adjust the signup function in our classes/user.php
file to handle the exception using SagCouchException
.
public function signup($username, $password) {
...
try {
$bones->couch->put($this->_id, $this->to_json());
} catch(SagCouchException $e) {
if($e->getCode() == "409") {
$bones->set('error', 'A user with this name already exists.');
$bones->render('user/signup');
exit;
}
}
}
We used a try...catch
statement to resolve the duplicate document update conflict that was triggered. By casting it with (SagCouchException $e)
, we are telling it to only catch SagCouchExceptions
for now that come through. Once this exception is caught, we are checking to see what code was returned. If it's a code of 409
, we are setting an error
variable with an error message. Then we need to re-display the...