dtzk85937 2018-07-10 14:03
浏览 62

Php异常没有被捕获

I'm trying to make an error handler to convert errors to JSON for my ajax requests. I've got a DB class which handles all MySql queries and which calls the following function when errors happens:

private function halt($msg)
{
    $this->Error = @mysqli_error($this->Link_ID);
    $this->Errno = @mysqli_errno($this->Link_ID);

    if ($this->throw = true) {
        throw new \Exception("Mysql Error: $msg. Error: $this->Errno ($this->Error)");
    } else if ($this->Halt_On_Error == "no") {
        return;
    }

    $this->haltmsg($msg);

    if ($this->Halt_On_Error != "report") {
        die("Session halted.");
    }
}

In my ajax script, I've got some functions like this one:

private function add()
{
    $this->changeErrorCatcher(ON);
    try {
        $obj = $this->mgr->getEmptyObject();
        foreach ($this->fields as $field) {
            if (!$this->form->vide($field) && $field != "id") {
                $name = "set" . ucfirst($field);
                $obj->$name($this->form->get($field));
            }
        }
        $this->mgr->create($obj);
        Ajax::Response(AJX_ACC, "Ok", $obj->getId());
    } catch (\Throwable $e) {
        Ajax::Response(AJX_ERR, $e->getMessage(), $e);
    }
}

changeErrorCatcher function:

private function changeErrorCatcher(bool $onOff)
{
    if ($onOff) {
        set_error_handler(function ($errno, $errstr) {
            throw new \Exception($errstr, $errno);
        }, E_ALL | E_ERROR | E_STRICT | E_USER_ERROR);
    } else {
        restore_error_handler();
    }
}

My problem is that the try/catch block in the add() function is not working: the line "$this->mgr->create($obj);" calls my DB class, so the halt() function is called when an error occurs and an Exception is thrown, but the catch block is never launched, and the text of the Exception is sent as ajax result (Making my JS fail parsing JSON)

I already tried solutions proposed here but nothing works (\Exceptions instead of Exception (even \Throwable does not work), disabling XDEBUG, changing error handler)

EDIT: I tried changing this in halt function:

if ($this->throw = true) {
    throw new \Exception("Mysql Error: $msg. Error: $this->Errno ($this->Error)");
} 

Into:

if ($this->throw = true) {
        try {
            throw new \Exception("Mysql Error: $msg. Error: $this->Errno ($this->Error)");
        } catch (\Exception $e) {
            throw new \Exception("Mysql Error: $msg. Error: $this->Errno ($this->Error)  ".$e->getTraceAsString());
        }
    }

Now I've got the stack trace in my ajax result, so the first exception is well catched but not the second... The stack trace shows me it is the line "$this->mgr->create($obj);" which throws exception.

  • 写回答

1条回答 默认 最新

  • dongma7725 2018-07-10 14:18
    关注

    Your code is failing because mysqli is returning an error instead of an exception. Set the following in your connection constructor prior to running any queries to tell it to throw exceptions instead of errors and your code should work:

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    

    As your query is returning a duplicate id error, you might also consider updating the SQL statement to use INSERT IGNORE or ON DUPLICATE KEY UPDATE ... instead to handle the error at the data layer level.

    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口