首页 文章

为什么我的PHP错误处理程序不回应所有错误?

提问于
浏览
1

我有代码导致2个错误,1来自php函数pg_query_params,因为我输入了一个无效的查询,以及当该函数的结果为false时抛出的Exception:

if (!$res = pg_query_params($this->sql, $this->args)) {
        // note pg_last_error seems to often not return anything
        $msg = pg_last_error() . " " . $this->sql . PHP_EOL . " Args: " . var_export($this->args, true);
        throw new \Exception("Query Execution Failure: $msg");
    }

然后我有错误处理程序代码,它记录错误,并应该回应它们 . 记录这两个错误,但只回显最后一个(抛出的异常) . 我想两个都回应,因为第一个包含有用的调试信息 . 我不明白为什么两者都没有,因为我已经做了一些调试,并且两个错误都调用了echo . 它是与输出缓冲或并发问题有关的东西吗?

这是我的错误处理程序代码的缩短版本 . throwableHandler方法在set_exception_handler()中注册,phpErrorHandler方法在set_error_handler()中注册 . 我没有包含generateMessageBodyCommon(),但它只是将错误信息添加到邮件正文:

private function handleError(string $messageBody, int $errno)
{
    // echo
    if ($this->echoErrors) {
        $messageBody .= 'inside echo'; // this goes into the log file for both errors
        echo nl2br($messageBody, false);
    }

    // log
    @error_log($messageBody, 3, $this->logPath);
}

public function throwableHandler(\Throwable $e)
{
    $message = $this->generateMessageBodyCommon($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
    $message .= PHP_EOL . "Stack Trace:" . PHP_EOL . $e->getTraceAsString();
    $this->handleError($message, $e->getCode(), $exitPage);
}

public function phpErrorHandler(int $errno, string $errstr, string $errfile = null, string $errline = null)
{
    $message = $this->generateMessageBodyCommon($errno, $errstr, $errfile, $errline) . PHP_EOL . "Stack Trace:". PHP_EOL . $this->getDebugBacktraceString();
    $this->handleError($message, $errno, false);
}

1 回答

相关问题