简体   繁体   English

Codeigniter 3:无法使用 try catch 块捕获数据库错误

[英]Codeigniter 3: Can't catch database error using try catch block

I'm working on an api, it handles the requests which comes from clients, then gets the response from server(developed using codeigniter 3) and forwards that back to client.我正在开发一个 api,它处理来自客户端的请求,然后从服务器获取响应(使用 codeigniter 3 开发)并将其转发回客户端。

But, in case of any database errors, like duplicate id, or null values, the model class cannot handle that error to display a proper error message.但是,如果出现任何数据库错误,例如重复 ID 或空值,模型类无法处理该错误以显示正确的错误消息。 I've tried the try catch block but not succeeded yet.我已经尝试过 try catch 块,但还没有成功。 Here's the model:这是模型:

public function add() {
    try {
        $this->db->trans_start(FALSE);
        $this->db->insert('users', $preparedData);
        $this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            throw new Exception("Database error:");
            return false;
        }
        return TRUE;
    } catch (Exception $e) {
        log_message('error: ',$e->getMessage());
        return;
    }
}

One thing to mention, I've set db_debug to FALSE.需要提及的一件事是,我已将 db_debug 设置为 FALSE。

Any help would be appreciated.任何帮助,将不胜感激。

As for CI 3, below code gets database error code and error message .至于 CI 3,下面的代码获取数据库错误代码错误消息 db_debug is set to FALSE. db_debug 设置为 FALSE。

public function add() {
    try {
        $this->db->trans_start(FALSE);
        $this->db->insert('users', $preparedData);
        $this->db->trans_complete();

        // documentation at
        // https://www.codeigniter.com/userguide3/database/queries.html#handling-errors
        // says; "the error() method will return an array containing its code and message"
        $db_error = $this->db->error();
        if (!empty($db_error)) {
            throw new Exception('Database error! Error Code [' . $db_error['code'] . '] Error: ' . $db_error['message']);
            return false; // unreachable retrun statement !!!
        }
        return TRUE;
    } catch (Exception $e) {
        // this will not catch DB related errors. But it will include them, because this is more general. 
        log_message('error: ',$e->getMessage());
        return;
    }
}

Refer to documentation at https://www.codeigniter.com/userguide3/database/queries.html#handling-errors请参阅https://www.codeigniter.com/userguide3/database/queries.html#handling-errors上的文档

saying

If you need to get the last error that has occurred, the error() method will return an array containing its code and message .如果您需要获取发生的最后一个错误,则 error() 方法将返回一个包含其codemessage的数组

It is a bit incomplete in my opinion because it does not show error code and error message in the example code.我认为它有点不完整,因为它没有在示例代码中显示错误代码和错误消息。

Suggestion in above code上面代码中的建议

  • Remove line $this->db->trans_complete();删除行$this->db->trans_complete();

  • If we see $this->db->error() after completing transaction it will be always empty如果我们在完成事务后看到$this->db->error()它将始终为空

  • Remove semicolon - log_message('error :',$e->getMessage()); return;删除分号 - log_message('error :',$e->getMessage()); return; log_message('error :',$e->getMessage()); return;

    public function add()
    {
    try {
        $this->db->trans_start(FALSE);
        $this->db->insert('users', $preparedData);


        // documentation at
        // https://www.codeigniter.com/userguide3/database/queries.html#handling-errors
        // says; "the error() method will return an array containing its code and message"
        $db_error = $this->db->error();
        if (!empty($db_error)) {
            throw new Exception('Database error! Error Code [' . $db_error['code'] . '] Error: ' . $db_error['message']);
            return false; // unreachable return statement !!!`enter code here`
        }
        return TRUE;
    } catch (Exception $e) {
        // this will not catch DB related `enter code here`errors. But it will include them, because this is more general. 
        log_message('error ',$e->getMessage());
        return;
    }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM