由于继续

时间:2016-04-22 04:47:22

标签: php try-catch cloudant

我相信Cloudant最近改变了一些代码。最近,如果你在try / catch语句中执行了storedoc操作。 Cloudant将向框架返回“错误”:

未捕获的异常'couchException',消息'Continue

当然你可以在catch语句中处理它,但它确实应该在PHP-on-Couch库的Try语句中以“成功”的形式返回。

有人遇到这个或知道如何处理它?最大的问题是你不能在catch语句中获取ID和Rev,因为它出现了错误:

                try { // does not return here, goes to catch
                    $response = $client->storeDoc($doc);
                    $response_json['status'] = 'success';
                    $response_json['id'] = $response->id;
                    $response_json['rev'] = $response->rev;
                } catch (Exception $e) { // even though the doc is successfully storing

                    // check for accepted BEG
                    $error = '';
                    $error = $e->getMessage();
                    $err_pos = strpos($error,"Accepted");
                    $err_pos_2 = strpos($error,"Continue");
                    if($err_pos !== false OR $err_pos_2 !== false){ // success

                        $response_json['status'] = 'success';
                        $response_json['id'] = $response->id; // returns null
                        $response_json['rev'] = $response->rev; // returns null

                    } else { // truely an error

                        $response_json['status'] = 'fail';
                        $response_json['message'] = $e->getMessage();
                        $response_json['code'] = $e->getCode();

                    }
                    // check for accepted END


                }

1 个答案:

答案 0 :(得分:0)

我在CouchDB和Cloudant中测试过,行为是一样的。这就是我认为正在发生的事情。创建新的couchDocument时:

$doc = new couchDocument($client);

默认情况下,文档设置为autocommit。你可以在couchDocument.php中看到这个:

function __construct(couchClient $client) {
    $this->__couch_data = new stdClass();
    $this->__couch_data->client = $client;
    $this->__couch_data->fields = new stdClass();
    $this->__couch_data->autocommit = true;
}

只要在文档上设置属性:

$doc->set( array('name'=>'Smith','firstname'=>'John') );
立即调用

storeDoc。然后您尝试再次调用storeDoc并且couchDB返回错误。

有两种方法可以解决这个问题:

  1. 关闭自动提交:

    $doc = new couchDocument($client);
    $doc->setAutocommit(false);
    $doc->set( array('name'=>'Smith','firstname'=>'John') );
    try {
        $response = $client->storeDoc($doc);
        $response_json['status'] = 'success';
        $response_json['id'] = $response->id;
        $response_json['rev'] = $response->rev;
    
  2. 在设置属性后,保持自动提交并从$doc获取id和rev:

    $doc = new couchDocument($client);
    try {
        $doc->set( array('name'=>'Smith','firstname'=>'John') );
        $response_json['status'] = 'success';
        $response_json['id'] = $doc->_id;
        $response_json['rev'] = $doc->_rev;