O'Reilly logo

CouchDB and PHP Web Development Beginner's Guide by Tim Juravich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action — handling document update conflicts using SagCouchException

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;
}
}

}

What just happened?

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required